27 lines
588 B
Go
27 lines
588 B
Go
package errors
|
||
|
||
import (
|
||
"errors"
|
||
"testing"
|
||
)
|
||
|
||
func TestAppErrorBasics(t *testing.T) {
|
||
root := errors.New("root")
|
||
appErr := NewBadRequest("bad", root)
|
||
|
||
if appErr.Code != 400 || appErr.Message != "bad" {
|
||
t.Fatalf("unexpected appErr fields: %+v", appErr)
|
||
}
|
||
if got := appErr.Error(); got != "bad: root" {
|
||
t.Fatalf("unexpected Error(): %s", got)
|
||
}
|
||
// 使用标准库 errors.Is/As(包级 Is/As 已移除)
|
||
if !errors.Is(appErr, root) {
|
||
t.Fatalf("Is should match wrapped error")
|
||
}
|
||
var target *AppError
|
||
if !errors.As(appErr, &target) {
|
||
t.Fatalf("As should succeed")
|
||
}
|
||
}
|