1 parent
d2a9033695
commit
734d389a7c
5 files changed
+91
-13
No files matched your search
@@ -0,0 +1,33 @@
|
||||
package failure
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/ginframework/fault"
|
||||
)
|
||||
|
||||
type numericBusinessError struct{}
|
||||
|
||||
func (*numericBusinessError) Error() string { return "private business message" }
|
||||
func (*numericBusinessError) Code() int { return 1001 }
|
||||
|
||||
type flexibleBusinessError struct{ code any }
|
||||
|
||||
func (*flexibleBusinessError) Error() string { return "private business message" }
|
||||
func (e *flexibleBusinessError) Code() any { return e.code }
|
||||
|
||||
func TestClassifiedExtensionErrorsPreserveIdentity(t *testing.T) {
|
||||
for _, cause := range []error{&numericBusinessError{}, &flexibleBusinessError{code: 1002}, fmt.Errorf("callback: %w", &numericBusinessError{}), fmt.Errorf("callback: %w", &flexibleBusinessError{code: 1003})} {
|
||||
if got := Wrap(901106, "read database record failed", cause); got != cause {
|
||||
t.Fatal("classified application error was wrapped")
|
||||
}
|
||||
}
|
||||
for _, cause := range []error{&flexibleBusinessError{code: "1002"}, &flexibleBusinessError{}, errors.New("raw driver failure")} {
|
||||
got := Wrap(901106, "read database record failed", cause)
|
||||
if !fault.Internal(got) || !errors.Is(got, cause) {
|
||||
t.Fatal("unclassified cause lost source classification")
|
||||
}
|
||||
}
|
||||
}
|
||||