Skip to content

Commit

Permalink
feat: add internal error wrapper (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
josestg authored May 20, 2023
1 parent 32a45d0 commit 8d929d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
15 changes: 15 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,18 @@ func stringifyJSON(m json.Marshaler) string {
}
return string(b)
}

// InternalError is an error wrapper to indicate an internal error.
// If goval got this type of error, it will not include in Errors and KeyError.
type InternalError struct {
Err error
}

// NewInternalError creates a new InternalError.
func NewInternalError(err error) *InternalError {
return &InternalError{Err: err}
}

func (e *InternalError) Error() string { return e.String() }
func (e *InternalError) Unwrap() error { return e.Err }
func (e *InternalError) String() string { return "goval.InternalError: " + e.Err.Error() }
7 changes: 6 additions & 1 deletion goval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goval

import (
"context"
"errors"
"github.com/pkg-id/goval/funcs"
"regexp"
)
Expand Down Expand Up @@ -107,6 +108,10 @@ func execChain[T any, Func FunctionValidatorConstraint[T]](ctx context.Context,
func Named[T any, F RuleValidator[T]](name string, value T, validator F) Validator {
return ValidatorFunc(func(ctx context.Context) error {
if err := validator.Validate(ctx, value); err != nil {
var ie *InternalError
if errors.As(err, &ie) {
return ie
}
return NewKeyError(name, err)
}
return nil
Expand Down Expand Up @@ -160,7 +165,7 @@ func validatorReducer(ctx context.Context, internalError chan error) func(errs E
err := validator.Validate(ctx)
if err != nil {
switch err.(type) {
default:
default: // *InternalError or something else.
internalError <- err
case *RuleError, *KeyError, Errors:
errs = append(errs, err)
Expand Down
33 changes: 33 additions & 0 deletions goval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,39 @@ func TestNamed(t *testing.T) {
t.Fatalf("expect not error")
}
})

t.Run("internal error", func(t *testing.T) {
ctx := context.Background()

retErr := errors.New("internal error")
custom := func(ctx context.Context, value string) error {
return goval.NewInternalError(retErr)
}

err := goval.Named("field-name", "a", goval.String().With(custom)).Validate(ctx)
if !errors.Is(err, retErr) {
t.Fatalf("expect error is internal error")
}
})

t.Run("without internal error", func(t *testing.T) {
ctx := context.Background()

retErr := errors.New("internal error")
custom := func(ctx context.Context, value string) error {
return retErr
}

err := goval.Named("field-name", "a", goval.String().With(custom)).Validate(ctx)
var kErr *goval.KeyError
if !errors.As(err, &kErr) {
t.Fatalf("expect error type: %T; got error type: %T", kErr, err)
}

if !errors.Is(kErr.Err, retErr) {
t.Fatalf("expect error is internal error")
}
})
}

func TestEach(t *testing.T) {
Expand Down

0 comments on commit 8d929d2

Please sign in to comment.