x
This commit is contained in:
151
internal/errors/domain_errors.go
Normal file
151
internal/errors/domain_errors.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 微信相关错误
|
||||
|
||||
// WeChatProcessNotFound 创建微信进程未找到错误
|
||||
func WeChatProcessNotFound() *AppError {
|
||||
return New(ErrTypeWeChat, "wechat process not found", nil, http.StatusNotFound).WithStack()
|
||||
}
|
||||
|
||||
// WeChatKeyExtractFailed 创建微信密钥提取失败错误
|
||||
func WeChatKeyExtractFailed(cause error) *AppError {
|
||||
return New(ErrTypeWeChat, "failed to extract wechat key", cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// WeChatDecryptFailed 创建微信解密失败错误
|
||||
func WeChatDecryptFailed(cause error) *AppError {
|
||||
return New(ErrTypeWeChat, "failed to decrypt wechat database", cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// WeChatAccountNotSelected 创建未选择微信账号错误
|
||||
func WeChatAccountNotSelected() *AppError {
|
||||
return New(ErrTypeWeChat, "no wechat account selected", nil, http.StatusBadRequest).WithStack()
|
||||
}
|
||||
|
||||
// 数据库相关错误
|
||||
|
||||
// DBConnectionFailed 创建数据库连接失败错误
|
||||
func DBConnectionFailed(cause error) *AppError {
|
||||
return New(ErrTypeDatabase, "database connection failed", cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// DBQueryFailed 创建数据库查询失败错误
|
||||
func DBQueryFailed(operation string, cause error) *AppError {
|
||||
return New(ErrTypeDatabase, fmt.Sprintf("database query failed: %s", operation), cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// DBRecordNotFound 创建数据库记录未找到错误
|
||||
func DBRecordNotFound(resource string) *AppError {
|
||||
return New(ErrTypeNotFound, fmt.Sprintf("record not found: %s", resource), nil, http.StatusNotFound).WithStack()
|
||||
}
|
||||
|
||||
// 配置相关错误
|
||||
|
||||
// ConfigInvalid 创建配置无效错误
|
||||
func ConfigInvalid(field string, cause error) *AppError {
|
||||
return New(ErrTypeConfig, fmt.Sprintf("invalid configuration: %s", field), cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// ConfigMissing 创建配置缺失错误
|
||||
func ConfigMissing(field string) *AppError {
|
||||
return New(ErrTypeConfig, fmt.Sprintf("missing configuration: %s", field), nil, http.StatusBadRequest).WithStack()
|
||||
}
|
||||
|
||||
// 平台相关错误
|
||||
|
||||
// PlatformUnsupported 创建不支持的平台错误
|
||||
func PlatformUnsupported(platform string, version int) *AppError {
|
||||
return New(ErrTypeInvalidArg, fmt.Sprintf("unsupported platform: %s v%d", platform, version), nil, http.StatusBadRequest).WithStack()
|
||||
}
|
||||
|
||||
// 文件系统错误
|
||||
|
||||
// FileNotFound 创建文件未找到错误
|
||||
func FileNotFound(path string) *AppError {
|
||||
return New(ErrTypeNotFound, fmt.Sprintf("file not found: %s", path), nil, http.StatusNotFound).WithStack()
|
||||
}
|
||||
|
||||
// FileReadFailed 创建文件读取失败错误
|
||||
func FileReadFailed(path string, cause error) *AppError {
|
||||
return New(ErrTypeInternal, fmt.Sprintf("failed to read file: %s", path), cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// FileWriteFailed 创建文件写入失败错误
|
||||
func FileWriteFailed(path string, cause error) *AppError {
|
||||
return New(ErrTypeInternal, fmt.Sprintf("failed to write file: %s", path), cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// 参数验证错误
|
||||
|
||||
// RequiredParam 创建必需参数缺失错误
|
||||
func RequiredParam(param string) *AppError {
|
||||
return New(ErrTypeInvalidArg, fmt.Sprintf("required parameter missing: %s", param), nil, http.StatusBadRequest).WithStack()
|
||||
}
|
||||
|
||||
// InvalidParam 创建参数无效错误
|
||||
func InvalidParam(param string, reason string) *AppError {
|
||||
message := fmt.Sprintf("invalid parameter: %s", param)
|
||||
if reason != "" {
|
||||
message = fmt.Sprintf("%s (%s)", message, reason)
|
||||
}
|
||||
return New(ErrTypeInvalidArg, message, nil, http.StatusBadRequest).WithStack()
|
||||
}
|
||||
|
||||
// 解密相关错误
|
||||
|
||||
// DecryptInvalidKey 创建无效密钥格式错误
|
||||
func DecryptInvalidKey(cause error) *AppError {
|
||||
return New(ErrTypeWeChat, "invalid key format", cause, http.StatusBadRequest).
|
||||
WithStack()
|
||||
}
|
||||
|
||||
// DecryptCreateCipherFailed 创建无法创建加密器错误
|
||||
func DecryptCreateCipherFailed(cause error) *AppError {
|
||||
return New(ErrTypeWeChat, "failed to create cipher", cause, http.StatusInternalServerError).
|
||||
WithStack()
|
||||
}
|
||||
|
||||
// DecryptDecodeKeyFailed 创建无法解码十六进制密钥错误
|
||||
func DecryptDecodeKeyFailed(cause error) *AppError {
|
||||
return New(ErrTypeWeChat, "failed to decode hex key", cause, http.StatusBadRequest).
|
||||
WithStack()
|
||||
}
|
||||
|
||||
// DecryptWriteOutputFailed 创建无法写入输出错误
|
||||
func DecryptWriteOutputFailed(cause error) *AppError {
|
||||
return New(ErrTypeWeChat, "failed to write decryption output", cause, http.StatusInternalServerError).
|
||||
WithStack()
|
||||
}
|
||||
|
||||
// DecryptOperationCanceled 创建解密操作被取消错误
|
||||
func DecryptOperationCanceled() *AppError {
|
||||
return New(ErrTypeWeChat, "decryption operation was canceled", nil, http.StatusBadRequest).
|
||||
WithStack()
|
||||
}
|
||||
|
||||
// DecryptOpenFileFailed 创建无法打开数据库文件错误
|
||||
func DecryptOpenFileFailed(path string, cause error) *AppError {
|
||||
return New(ErrTypeWeChat, fmt.Sprintf("failed to open database file: %s", path), cause, http.StatusInternalServerError).
|
||||
WithStack()
|
||||
}
|
||||
|
||||
// DecryptReadFileFailed 创建无法读取数据库文件错误
|
||||
func DecryptReadFileFailed(path string, cause error) *AppError {
|
||||
return New(ErrTypeWeChat, fmt.Sprintf("failed to read database file: %s", path), cause, http.StatusInternalServerError).
|
||||
WithStack()
|
||||
}
|
||||
|
||||
// DecryptIncompleteRead 创建不完整的头部读取错误
|
||||
func DecryptIncompleteRead(cause error) *AppError {
|
||||
return New(ErrTypeWeChat, "incomplete header read during decryption", cause, http.StatusInternalServerError).
|
||||
WithStack()
|
||||
}
|
||||
|
||||
var ErrAlreadyDecrypted = New(ErrTypeWeChat, "database file is already decrypted", nil, http.StatusBadRequest)
|
||||
var ErrDecryptHashVerificationFailed = New(ErrTypeWeChat, "hash verification failed during decryption", nil, http.StatusBadRequest)
|
||||
var ErrDecryptIncorrectKey = New(ErrTypeWeChat, "incorrect decryption key", nil, http.StatusBadRequest)
|
||||
@@ -1,8 +1,11 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -14,16 +17,25 @@ const (
|
||||
ErrTypeHTTP = "http"
|
||||
ErrTypeConfig = "config"
|
||||
ErrTypeInvalidArg = "invalid_argument"
|
||||
ErrTypeAuth = "authentication"
|
||||
ErrTypePermission = "permission"
|
||||
ErrTypeNotFound = "not_found"
|
||||
ErrTypeValidation = "validation"
|
||||
ErrTypeRateLimit = "rate_limit"
|
||||
ErrTypeInternal = "internal"
|
||||
)
|
||||
|
||||
// AppError 表示应用程序错误
|
||||
type AppError struct {
|
||||
Type string `json:"type"` // 错误类型
|
||||
Message string `json:"message"` // 错误消息
|
||||
Cause error `json:"-"` // 原始错误
|
||||
Code int `json:"-"` // HTTP Code
|
||||
Type string `json:"type"` // 错误类型
|
||||
Message string `json:"message"` // 错误消息
|
||||
Cause error `json:"-"` // 原始错误
|
||||
Code int `json:"-"` // HTTP Code
|
||||
Stack []string `json:"-"` // 错误堆栈
|
||||
RequestID string `json:"request_id,omitempty"` // 请求ID,用于跟踪
|
||||
}
|
||||
|
||||
// Error 实现 error 接口
|
||||
func (e *AppError) Error() string {
|
||||
if e.Cause != nil {
|
||||
return fmt.Sprintf("%s: %s: %v", e.Type, e.Message, e.Cause)
|
||||
@@ -31,10 +43,44 @@ func (e *AppError) Error() string {
|
||||
return fmt.Sprintf("%s: %s", e.Type, e.Message)
|
||||
}
|
||||
|
||||
// String 返回错误的字符串表示
|
||||
func (e *AppError) String() string {
|
||||
return e.Error()
|
||||
}
|
||||
|
||||
// Unwrap 实现 errors.Unwrap 接口,用于错误链
|
||||
func (e *AppError) Unwrap() error {
|
||||
return e.Cause
|
||||
}
|
||||
|
||||
// WithStack 添加堆栈信息到错误
|
||||
func (e *AppError) WithStack() *AppError {
|
||||
const depth = 32
|
||||
var pcs [depth]uintptr
|
||||
n := runtime.Callers(2, pcs[:])
|
||||
frames := runtime.CallersFrames(pcs[:n])
|
||||
|
||||
stack := make([]string, 0, n)
|
||||
for {
|
||||
frame, more := frames.Next()
|
||||
if !strings.Contains(frame.File, "runtime/") {
|
||||
stack = append(stack, fmt.Sprintf("%s:%d %s", frame.File, frame.Line, frame.Function))
|
||||
}
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
e.Stack = stack
|
||||
return e
|
||||
}
|
||||
|
||||
// WithRequestID 添加请求ID到错误
|
||||
func (e *AppError) WithRequestID(requestID string) *AppError {
|
||||
e.RequestID = requestID
|
||||
return e
|
||||
}
|
||||
|
||||
// New 创建新的应用错误
|
||||
func New(errType, message string, cause error, code int) *AppError {
|
||||
return &AppError{
|
||||
@@ -45,41 +91,155 @@ func New(errType, message string, cause error, code int) *AppError {
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap 包装现有错误为 AppError
|
||||
func Wrap(err error, errType, message string, code int) *AppError {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果已经是 AppError,保留原始类型但更新消息
|
||||
if appErr, ok := err.(*AppError); ok {
|
||||
return &AppError{
|
||||
Type: appErr.Type,
|
||||
Message: message,
|
||||
Cause: appErr.Cause,
|
||||
Code: appErr.Code,
|
||||
Stack: appErr.Stack,
|
||||
}
|
||||
}
|
||||
|
||||
return New(errType, message, err, code)
|
||||
}
|
||||
|
||||
// Is 检查错误是否为特定类型
|
||||
func Is(err error, errType string) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var appErr *AppError
|
||||
if errors.As(err, &appErr) {
|
||||
return appErr.Type == errType
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// GetType 获取错误类型
|
||||
func GetType(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var appErr *AppError
|
||||
if errors.As(err, &appErr) {
|
||||
return appErr.Type
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// GetCode 获取错误的 HTTP 状态码
|
||||
func GetCode(err error) int {
|
||||
if err == nil {
|
||||
return http.StatusOK
|
||||
}
|
||||
|
||||
var appErr *AppError
|
||||
if errors.As(err, &appErr) {
|
||||
return appErr.Code
|
||||
}
|
||||
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
|
||||
// RootCause 获取错误链中的根本原因
|
||||
func RootCause(err error) error {
|
||||
for err != nil {
|
||||
unwrapped := errors.Unwrap(err)
|
||||
if unwrapped == nil {
|
||||
return err
|
||||
}
|
||||
err = unwrapped
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// ErrInvalidArg 无效参数错误
|
||||
func ErrInvalidArg(param string) *AppError {
|
||||
return New(ErrTypeInvalidArg, fmt.Sprintf("invalid arg: %s", param), nil, http.StatusBadRequest)
|
||||
return New(ErrTypeInvalidArg, fmt.Sprintf("invalid arg: %s", param), nil, http.StatusBadRequest).WithStack()
|
||||
}
|
||||
|
||||
// Database 创建数据库错误
|
||||
func Database(message string, cause error) *AppError {
|
||||
return New(ErrTypeDatabase, message, cause, http.StatusInternalServerError)
|
||||
return New(ErrTypeDatabase, message, cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// WeChat 创建微信相关错误
|
||||
func WeChat(message string, cause error) *AppError {
|
||||
return New(ErrTypeWeChat, message, cause, http.StatusInternalServerError)
|
||||
return New(ErrTypeWeChat, message, cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// HTTP 创建HTTP服务错误
|
||||
func HTTP(message string, cause error) *AppError {
|
||||
return New(ErrTypeHTTP, message, cause, http.StatusInternalServerError)
|
||||
return New(ErrTypeHTTP, message, cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// Config 创建配置错误
|
||||
func Config(message string, cause error) *AppError {
|
||||
return New(ErrTypeConfig, message, cause, http.StatusInternalServerError)
|
||||
return New(ErrTypeConfig, message, cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// NotFound 创建资源不存在错误
|
||||
func NotFound(resource string, cause error) *AppError {
|
||||
message := fmt.Sprintf("resource not found: %s", resource)
|
||||
return New(ErrTypeNotFound, message, cause, http.StatusNotFound).WithStack()
|
||||
}
|
||||
|
||||
// Unauthorized 创建未授权错误
|
||||
func Unauthorized(message string, cause error) *AppError {
|
||||
return New(ErrTypeAuth, message, cause, http.StatusUnauthorized).WithStack()
|
||||
}
|
||||
|
||||
// Forbidden 创建权限不足错误
|
||||
func Forbidden(message string, cause error) *AppError {
|
||||
return New(ErrTypePermission, message, cause, http.StatusForbidden).WithStack()
|
||||
}
|
||||
|
||||
// Validation 创建数据验证错误
|
||||
func Validation(message string, cause error) *AppError {
|
||||
return New(ErrTypeValidation, message, cause, http.StatusBadRequest).WithStack()
|
||||
}
|
||||
|
||||
// RateLimit 创建请求频率限制错误
|
||||
func RateLimit(message string, cause error) *AppError {
|
||||
return New(ErrTypeRateLimit, message, cause, http.StatusTooManyRequests).WithStack()
|
||||
}
|
||||
|
||||
// Internal 创建内部服务器错误
|
||||
func Internal(message string, cause error) *AppError {
|
||||
return New(ErrTypeInternal, message, cause, http.StatusInternalServerError).WithStack()
|
||||
}
|
||||
|
||||
// Err 在HTTP响应中返回错误
|
||||
func Err(c *gin.Context, err error) {
|
||||
// 获取请求ID(如果有)
|
||||
requestID := c.GetString("RequestID")
|
||||
|
||||
if appErr, ok := err.(*AppError); ok {
|
||||
if requestID != "" {
|
||||
appErr.RequestID = requestID
|
||||
}
|
||||
c.JSON(appErr.Code, appErr)
|
||||
return
|
||||
}
|
||||
|
||||
// 未知错误
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"type": "unknown",
|
||||
"message": err.Error(),
|
||||
})
|
||||
unknownErr := &AppError{
|
||||
Type: "unknown",
|
||||
Message: err.Error(),
|
||||
Code: http.StatusInternalServerError,
|
||||
RequestID: requestID,
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, unknownErr)
|
||||
}
|
||||
|
||||
165
internal/errors/errors_test.go
Normal file
165
internal/errors/errors_test.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestErrorCreation(t *testing.T) {
|
||||
// 测试创建基本错误
|
||||
err := New("test", "test message", nil, http.StatusBadRequest)
|
||||
if err.Type != "test" || err.Message != "test message" || err.Code != http.StatusBadRequest {
|
||||
t.Errorf("New() created incorrect error: %v", err)
|
||||
}
|
||||
|
||||
// 测试创建带原因的错误
|
||||
cause := fmt.Errorf("original error")
|
||||
err = New("test", "test with cause", cause, http.StatusInternalServerError)
|
||||
if err.Cause != cause {
|
||||
t.Errorf("New() did not set cause correctly: %v", err)
|
||||
}
|
||||
|
||||
// 测试错误消息格式
|
||||
expected := "test: test with cause: original error"
|
||||
if err.Error() != expected {
|
||||
t.Errorf("Error() = %q, want %q", err.Error(), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWrapping(t *testing.T) {
|
||||
// 测试包装普通错误
|
||||
original := fmt.Errorf("original error")
|
||||
wrapped := Wrap(original, "wrapped", "wrapped message", http.StatusBadRequest)
|
||||
|
||||
if wrapped.Type != "wrapped" || wrapped.Message != "wrapped message" {
|
||||
t.Errorf("Wrap() created incorrect error: %v", wrapped)
|
||||
}
|
||||
|
||||
if wrapped.Cause != original {
|
||||
t.Errorf("Wrap() did not set cause correctly")
|
||||
}
|
||||
|
||||
// 测试包装 AppError
|
||||
appErr := New("app", "app error", nil, http.StatusNotFound)
|
||||
rewrapped := Wrap(appErr, "ignored", "new message", http.StatusBadRequest)
|
||||
|
||||
if rewrapped.Type != "app" {
|
||||
t.Errorf("Wrap() did not preserve original AppError type: got %s, want %s",
|
||||
rewrapped.Type, appErr.Type)
|
||||
}
|
||||
|
||||
if rewrapped.Message != "new message" {
|
||||
t.Errorf("Wrap() did not update message: got %s, want %s",
|
||||
rewrapped.Message, "new message")
|
||||
}
|
||||
|
||||
if rewrapped.Code != appErr.Code {
|
||||
t.Errorf("Wrap() did not preserve original status code: got %d, want %d",
|
||||
rewrapped.Code, appErr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorTypeChecking(t *testing.T) {
|
||||
// 创建不同类型的错误
|
||||
dbErr := Database("db error", nil)
|
||||
httpErr := HTTP("http error", nil)
|
||||
|
||||
// 测试 Is 函数
|
||||
if !Is(dbErr, ErrTypeDatabase) {
|
||||
t.Errorf("Is() failed to identify database error")
|
||||
}
|
||||
|
||||
if Is(dbErr, ErrTypeHTTP) {
|
||||
t.Errorf("Is() incorrectly identified database error as HTTP error")
|
||||
}
|
||||
|
||||
if !Is(httpErr, ErrTypeHTTP) {
|
||||
t.Errorf("Is() failed to identify HTTP error")
|
||||
}
|
||||
|
||||
// 测试 GetType 函数
|
||||
if GetType(dbErr) != ErrTypeDatabase {
|
||||
t.Errorf("GetType() returned incorrect type: got %s, want %s",
|
||||
GetType(dbErr), ErrTypeDatabase)
|
||||
}
|
||||
|
||||
if GetType(httpErr) != ErrTypeHTTP {
|
||||
t.Errorf("GetType() returned incorrect type: got %s, want %s",
|
||||
GetType(httpErr), ErrTypeHTTP)
|
||||
}
|
||||
|
||||
// 测试普通错误
|
||||
stdErr := fmt.Errorf("standard error")
|
||||
if GetType(stdErr) != "unknown" {
|
||||
t.Errorf("GetType() for standard error should return 'unknown', got %s",
|
||||
GetType(stdErr))
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorUnwrapping(t *testing.T) {
|
||||
// 创建嵌套错误
|
||||
innermost := fmt.Errorf("innermost error")
|
||||
inner := Wrap(innermost, "inner", "inner error", http.StatusBadRequest)
|
||||
outer := Wrap(inner, "outer", "outer error", http.StatusInternalServerError)
|
||||
|
||||
// 测试 Unwrap
|
||||
if unwrapped := outer.Unwrap(); unwrapped != inner.Cause {
|
||||
t.Errorf("Unwrap() did not return correct inner error")
|
||||
}
|
||||
|
||||
// 测试 RootCause
|
||||
if root := RootCause(outer); root != innermost {
|
||||
t.Errorf("RootCause() did not return innermost error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorHelperFunctions(t *testing.T) {
|
||||
// 测试辅助函数
|
||||
invalidArg := ErrInvalidArg("username")
|
||||
if invalidArg.Type != ErrTypeInvalidArg {
|
||||
t.Errorf("ErrInvalidArg() created error with wrong type: %s", invalidArg.Type)
|
||||
}
|
||||
|
||||
dbErr := Database("query failed", nil)
|
||||
if dbErr.Type != ErrTypeDatabase {
|
||||
t.Errorf("Database() created error with wrong type: %s", dbErr.Type)
|
||||
}
|
||||
|
||||
notFound := NotFound("user", nil)
|
||||
if notFound.Type != ErrTypeNotFound || notFound.Code != http.StatusNotFound {
|
||||
t.Errorf("NotFound() created error with wrong type or code: %s, %d",
|
||||
notFound.Type, notFound.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorUtilityFunctions(t *testing.T) {
|
||||
// 测试 JoinErrors
|
||||
err1 := fmt.Errorf("error 1")
|
||||
err2 := fmt.Errorf("error 2")
|
||||
|
||||
// 单个错误
|
||||
if joined := JoinErrors(err1); joined != err1 {
|
||||
t.Errorf("JoinErrors() with single error should return that error")
|
||||
}
|
||||
|
||||
// 多个错误
|
||||
joined := JoinErrors(err1, err2)
|
||||
if joined == nil {
|
||||
t.Errorf("JoinErrors() returned nil for multiple errors")
|
||||
}
|
||||
|
||||
// nil 错误
|
||||
if joined := JoinErrors(nil, nil); joined != nil {
|
||||
t.Errorf("JoinErrors() with all nil should return nil")
|
||||
}
|
||||
|
||||
// 测试 WrapIfErr
|
||||
if wrapped := WrapIfErr(nil, "test", "message", http.StatusOK); wrapped != nil {
|
||||
t.Errorf("WrapIfErr() with nil should return nil")
|
||||
}
|
||||
|
||||
if wrapped := WrapIfErr(err1, "test", "message", http.StatusBadRequest); wrapped == nil {
|
||||
t.Errorf("WrapIfErr() with non-nil error should return non-nil")
|
||||
}
|
||||
}
|
||||
66
internal/errors/middleware.go
Normal file
66
internal/errors/middleware.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// ErrorHandlerMiddleware 是一个 Gin 中间件,用于统一处理请求过程中的错误
|
||||
// 它会为每个请求生成一个唯一的请求 ID,并在错误发生时将其添加到错误响应中
|
||||
func ErrorHandlerMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 生成请求 ID
|
||||
requestID := uuid.New().String()
|
||||
c.Set("RequestID", requestID)
|
||||
c.Header("X-Request-ID", requestID)
|
||||
|
||||
// 处理请求
|
||||
c.Next()
|
||||
|
||||
// 检查是否有错误
|
||||
if len(c.Errors) > 0 {
|
||||
// 获取第一个错误
|
||||
err := c.Errors[0].Err
|
||||
|
||||
// 使用 Err 函数处理错误响应
|
||||
Err(c, err)
|
||||
|
||||
// 已经处理过错误,不需要继续
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RecoveryMiddleware 是一个 Gin 中间件,用于从 panic 恢复并返回 500 错误
|
||||
func RecoveryMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
// 获取请求 ID
|
||||
requestID, _ := c.Get("RequestID")
|
||||
requestIDStr, _ := requestID.(string)
|
||||
|
||||
// 创建内部服务器错误
|
||||
var err *AppError
|
||||
switch v := r.(type) {
|
||||
case error:
|
||||
err = Internal("panic recovered", v).WithRequestID(requestIDStr)
|
||||
default:
|
||||
err = Internal(fmt.Sprintf("panic recovered: %v", r), nil).WithRequestID(requestIDStr)
|
||||
}
|
||||
|
||||
// 记录错误日志
|
||||
fmt.Printf("PANIC RECOVERED: %v\n", err)
|
||||
|
||||
// 返回 500 错误
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
131
internal/errors/utils.go
Normal file
131
internal/errors/utils.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// WrapIfErr 如果 err 不为 nil,则包装错误并返回,否则返回 nil
|
||||
func WrapIfErr(err error, errType, message string, code int) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return Wrap(err, errType, message, code)
|
||||
}
|
||||
|
||||
// JoinErrors 将多个错误合并为一个错误
|
||||
// 如果只有一个错误不为 nil,则返回该错误
|
||||
// 如果有多个错误不为 nil,则创建一个包含所有错误信息的新错误
|
||||
func JoinErrors(errs ...error) error {
|
||||
var nonNilErrs []error
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
nonNilErrs = append(nonNilErrs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(nonNilErrs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(nonNilErrs) == 1 {
|
||||
return nonNilErrs[0]
|
||||
}
|
||||
|
||||
// 合并多个错误
|
||||
var messages []string
|
||||
for _, err := range nonNilErrs {
|
||||
messages = append(messages, err.Error())
|
||||
}
|
||||
|
||||
return Internal(
|
||||
fmt.Sprintf("multiple errors occurred: %s", strings.Join(messages, "; ")),
|
||||
nonNilErrs[0],
|
||||
)
|
||||
}
|
||||
|
||||
// IsNil 检查错误是否为 nil
|
||||
func IsNil(err error) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// IsNotNil 检查错误是否不为 nil
|
||||
func IsNotNil(err error) bool {
|
||||
return err != nil
|
||||
}
|
||||
|
||||
// IsType 检查错误是否为指定类型
|
||||
func IsType(err error, errType string) bool {
|
||||
return Is(err, errType)
|
||||
}
|
||||
|
||||
// HasCause 检查错误是否包含指定的原因
|
||||
func HasCause(err error, cause error) bool {
|
||||
if err == nil || cause == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var appErr *AppError
|
||||
if stderrors.As(err, &appErr) {
|
||||
if appErr.Cause == cause {
|
||||
return true
|
||||
}
|
||||
return HasCause(appErr.Cause, cause)
|
||||
}
|
||||
|
||||
return err == cause
|
||||
}
|
||||
|
||||
// AsAppError 将错误转换为 AppError 类型
|
||||
func AsAppError(err error) (*AppError, bool) {
|
||||
var appErr *AppError
|
||||
if stderrors.As(err, &appErr) {
|
||||
return appErr, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// FormatErrorChain 格式化错误链,便于调试
|
||||
func FormatErrorChain(err error) string {
|
||||
if err == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
|
||||
var result strings.Builder
|
||||
result.WriteString(err.Error())
|
||||
|
||||
// 获取 AppError 类型的堆栈信息
|
||||
var appErr *AppError
|
||||
if stderrors.As(err, &appErr) && len(appErr.Stack) > 0 {
|
||||
result.WriteString("\nStack Trace:\n")
|
||||
for _, frame := range appErr.Stack {
|
||||
result.WriteString(" ")
|
||||
result.WriteString(frame)
|
||||
result.WriteString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
// 递归处理错误链
|
||||
cause := stderrors.Unwrap(err)
|
||||
if cause != nil {
|
||||
result.WriteString("\nCaused by: ")
|
||||
result.WriteString(FormatErrorChain(cause))
|
||||
}
|
||||
|
||||
return result.String()
|
||||
}
|
||||
|
||||
// GetErrorDetails 返回错误的详细信息,包括类型、消息、HTTP状态码和请求ID
|
||||
func GetErrorDetails(err error) (errType string, message string, code int, requestID string) {
|
||||
if err == nil {
|
||||
return "", "", 0, ""
|
||||
}
|
||||
|
||||
var appErr *AppError
|
||||
if stderrors.As(err, &appErr) {
|
||||
return appErr.Type, appErr.Message, appErr.Code, appErr.RequestID
|
||||
}
|
||||
|
||||
return "unknown", err.Error(), 500, ""
|
||||
}
|
||||
Reference in New Issue
Block a user