You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package errors
|
|
|
|
// Tests for domain error formatting, wrapping, and code matching.
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestDomainError_ErrorFormatting verifies error strings with and without wrapped errors.
|
|
func TestDomainError_ErrorFormatting(t *testing.T) {
|
|
base := New(CodeNotFound, "missing")
|
|
require.Equal(t, "[NOT_FOUND] missing", base.Error())
|
|
|
|
wrapped := Wrap(CodeInvalidInput, "bad", errors.New("details"))
|
|
require.Equal(t, "[INVALID_INPUT] bad: details", wrapped.Error())
|
|
}
|
|
|
|
// TestDomainError_Is ensures errors.Is matches on error code.
|
|
func TestDomainError_Is(t *testing.T) {
|
|
err := Wrap(CodeForbidden, "nope", nil)
|
|
target := New(CodeForbidden, "other")
|
|
require.True(t, errors.Is(err, target))
|
|
}
|
|
|
|
// TestDomainError_NewAndWrapFields verifies fields are set for New and Wrap.
|
|
func TestDomainError_NewAndWrapFields(t *testing.T) {
|
|
created := New(CodeConflict, "conflict")
|
|
require.Equal(t, CodeConflict, created.Code)
|
|
require.Equal(t, "conflict", created.Message)
|
|
require.Nil(t, created.Err)
|
|
|
|
root := errors.New("root")
|
|
wrapped := Wrap(CodeInternal, "internal", root)
|
|
require.Equal(t, CodeInternal, wrapped.Code)
|
|
require.Equal(t, "internal", wrapped.Message)
|
|
require.Equal(t, root, wrapped.Unwrap())
|
|
}
|