2025-12-24 16:03:46 +08:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
2026-06-15 16:40:36 +08:00
|
|
|
|
// 使用标准库 errors.Is/As(包级 Is/As 已移除)
|
|
|
|
|
|
if !errors.Is(appErr, root) {
|
2025-12-24 16:03:46 +08:00
|
|
|
|
t.Fatalf("Is should match wrapped error")
|
|
|
|
|
|
}
|
|
|
|
|
|
var target *AppError
|
2026-06-15 16:40:36 +08:00
|
|
|
|
if !errors.As(appErr, &target) {
|
2025-12-24 16:03:46 +08:00
|
|
|
|
t.Fatalf("As should succeed")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|