refactor: 移除全局单例、修复分层违规、统一错误与响应
This commit is contained in:
@@ -125,23 +125,9 @@ func NewInternalError(message string, err error) *AppError {
|
||||
return NewAppError(500, message, err)
|
||||
}
|
||||
|
||||
// Is 检查错误是否匹配
|
||||
func Is(err, target error) bool {
|
||||
return errors.Is(err, target)
|
||||
}
|
||||
// 注意:原此处的 Is/As/Wrap 函数(透传标准库)已删除。
|
||||
// 请直接使用标准库 errors.Is / errors.As / fmt.Errorf。
|
||||
|
||||
// As 尝试将错误转换为指定类型
|
||||
func As(err error, target interface{}) bool {
|
||||
return errors.As(err, target)
|
||||
}
|
||||
|
||||
// Wrap 包装错误
|
||||
func Wrap(err error, message string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s: %w", message, err)
|
||||
}
|
||||
|
||||
// YggdrasilErrorResponse Yggdrasil协议标准错误响应格式
|
||||
type YggdrasilErrorResponse struct {
|
||||
|
||||
@@ -15,24 +15,12 @@ func TestAppErrorBasics(t *testing.T) {
|
||||
if got := appErr.Error(); got != "bad: root" {
|
||||
t.Fatalf("unexpected Error(): %s", got)
|
||||
}
|
||||
if !Is(appErr, root) {
|
||||
// 使用标准库 errors.Is/As(包级 Is/As 已移除)
|
||||
if !errors.Is(appErr, root) {
|
||||
t.Fatalf("Is should match wrapped error")
|
||||
}
|
||||
var target *AppError
|
||||
if !As(appErr, &target) {
|
||||
if !errors.As(appErr, &target) {
|
||||
t.Fatalf("As should succeed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrap(t *testing.T) {
|
||||
if Wrap(nil, "msg") != nil {
|
||||
t.Fatalf("Wrap nil should return nil")
|
||||
}
|
||||
err := errors.New("base")
|
||||
wrapped := Wrap(err, "ctx")
|
||||
if wrapped.Error() != "ctx: base" {
|
||||
t.Fatalf("wrap message mismatch: %v", wrapped)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user