Skip to content

Commit

Permalink
feat: add PassThrough method to pass on the code and properties (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gogomoe authored Aug 19, 2024
1 parent 78185bb commit 32ddf1e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
27 changes: 27 additions & 0 deletions eris.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ func Wrapf(err error, format string, args ...any) error {
return wrap(err, fmt.Sprintf(format, args...), DEFAULT_ERROR_CODE_WRAP)
}

// PassThrough adds additional context to all error types while maintaining the type of the original error.
//
// This method behaves like Wrap but will copy the code and properties from underlying error.
func PassThrough(err error, msg string) error {
return PassThroughf(err, fmt.Sprint(msg))
}

// PassThroughf adds additional context to all error types while maintaining the type of the original error.
//
// This is a convenience method for wrapping errors with formatted messages and is otherwise the same as PassThrough.
func PassThroughf(err error, format string, args ...any) error {
if err == nil {
return nil
}
newErr := wrap(err, fmt.Sprintf(format, args...), DEFAULT_ERROR_CODE_WRAP)

code := GetCode(err)
if code != CodeUnknown {
newErr = WithCode(newErr, code)
}
kvs := GetKVs(err)
for k, v := range kvs {
newErr = WithProperty(newErr, k, v)
}
return newErr
}

func wrap(err error, msg string, code Code) error {
if err == nil {
return nil
Expand Down
61 changes: 61 additions & 0 deletions eris_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,67 @@ func TestExternalErrorWrapping(t *testing.T) {
}
}

func TestErrorPassThrough(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output string // expected output
}{
"nil root error": {
cause: nil,
input: []string{"additional context"},
},
"external error passing": {
cause: fmt.Errorf("external error"),
input: []string{"additional context"},
output: "code(internal) additional context: external error",
},
"standard error passing with a global root cause": {
cause: globalErr,
input: []string{"additional context", "even more context"},
output: "code(internal) even more context: code(internal) additional context: code(unknown) global error",
},
"standard error passing with a local root cause": {
cause: eris.New("root error").WithCode(eris.CodeInvalidArgument),
input: []string{"additional context", "even more context"},
output: "code(invalid argument) even more context: code(invalid argument) additional context: code(invalid argument) root error",
},
"standard error passing with a local root cause (eris.Errorf)": {
cause: eris.Errorf("%v root error", "formatted").WithCode(eris.CodeInvalidArgument),
input: []string{"additional context", "even more context"},
output: "code(invalid argument) even more context: code(invalid argument) additional context: code(invalid argument) formatted root error",
},
"standard error passing with a local root cause property": {
cause: eris.New("root error").WithProperty("key1", "val1"),
input: []string{"additional context", "even more context"},
output: "code(internal) KVs(map[key1:val1]) even more context: code(internal) KVs(map[key1:val1]) additional context: code(unknown) KVs(map[key1:val1]) root error",
},
"standard error passing with a local root cause property (eris.Errorf)": {
cause: eris.Errorf("%v root error", "formatted").WithProperty("key1", "val1"),
input: []string{"additional context", "even more context"},
output: "code(internal) KVs(map[key1:val1]) even more context: code(internal) KVs(map[key1:val1]) additional context: code(unknown) KVs(map[key1:val1]) formatted root error",
},
"no error passing with a local root cause (eris.Errorf)": {
cause: eris.Errorf("%v root error", "formatted").WithCode(eris.CodeUnknown),
output: "code(unknown) formatted root error",
},
}

for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := tc.cause
for _, str := range tc.input {
err = eris.PassThrough(err, str)
}
if err != nil && tc.cause == nil {
t.Errorf("%v: wrapping nil errors should return nil but got { %v }", desc, err)
} else if err != nil && tc.output != err.Error() {
t.Errorf("%v: expected { %v } got { %v }", desc, tc.output, err)
}
})
}
}

func TestErrorUnwrap(t *testing.T) {
tests := map[string]struct {
cause error // root error
Expand Down

0 comments on commit 32ddf1e

Please sign in to comment.