chore: initial import
This commit is contained in:
45
pkg/errors/errors_test.go
Normal file
45
pkg/errors/errors_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppErrorErrorIncludesCodeAndMessage(t *testing.T) {
|
||||
err := &AppError{Code: "OPS_TEST", Message: "failed"}
|
||||
if got := err.Error(); got != "[OPS_TEST] failed" {
|
||||
t.Fatalf("unexpected error string: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithDetailReturnsCopyWithoutMutatingBase(t *testing.T) {
|
||||
detail := map[string]any{"field": "name"}
|
||||
err := ErrBadRequest.WithDetail(detail)
|
||||
|
||||
if err == ErrBadRequest {
|
||||
t.Fatal("expected a copy, got original pointer")
|
||||
}
|
||||
if err.Code != ErrBadRequest.Code || err.HTTPStatus != ErrBadRequest.HTTPStatus || err.Message != ErrBadRequest.Message {
|
||||
t.Fatalf("metadata not preserved: %+v", err)
|
||||
}
|
||||
if err.Detail["field"] != "name" {
|
||||
t.Fatalf("detail not attached: %+v", err.Detail)
|
||||
}
|
||||
if ErrBadRequest.Detail != nil {
|
||||
t.Fatalf("base error was mutated: %+v", ErrBadRequest.Detail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrap(t *testing.T) {
|
||||
if Wrap(nil, ErrInternal) != nil {
|
||||
t.Fatal("nil input should return nil")
|
||||
}
|
||||
|
||||
wrapped := Wrap(errors.New("boom"), ErrInternal)
|
||||
if wrapped.Code != ErrInternal.Code || wrapped.HTTPStatus != ErrInternal.HTTPStatus {
|
||||
t.Fatalf("metadata not preserved: %+v", wrapped)
|
||||
}
|
||||
if wrapped.Message != "内部服务错误: boom" {
|
||||
t.Fatalf("unexpected message: %s", wrapped.Message)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user