adjust errors and logger (#10)

This commit is contained in:
Sarv
2025-04-01 19:41:40 +08:00
committed by GitHub
parent f31953c42b
commit 167a9ca873
49 changed files with 553 additions and 981 deletions

View File

@@ -10,51 +10,29 @@ import (
"github.com/gin-gonic/gin"
)
// 定义错误类型常量
const (
ErrTypeDatabase = "database"
ErrTypeWeChat = "wechat"
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
Stack []string `json:"-"` // 错误堆栈
RequestID string `json:"request_id,omitempty"` // 请求ID用于跟踪
type Error struct {
Message string `json:"message"` // 错误消息
Cause error `json:"-"` // 原始错误
Code int `json:"-"` // HTTP Code
Stack []string `json:"-"` // 错误堆栈
}
// Error 实现 error 接口
func (e *AppError) Error() string {
func (e *Error) Error() string {
if e.Cause != nil {
return fmt.Sprintf("%s: %s: %v", e.Type, e.Message, e.Cause)
return fmt.Sprintf("%s: %v", e.Message, e.Cause)
}
return fmt.Sprintf("%s: %s", e.Type, e.Message)
return fmt.Sprintf("%s", e.Message)
}
// String 返回错误的字符串表示
func (e *AppError) String() string {
func (e *Error) String() string {
return e.Error()
}
// Unwrap 实现 errors.Unwrap 接口,用于错误链
func (e *AppError) Unwrap() error {
func (e *Error) Unwrap() error {
return e.Cause
}
// WithStack 添加堆栈信息到错误
func (e *AppError) WithStack() *AppError {
func (e *Error) WithStack() *Error {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(2, pcs[:])
@@ -75,32 +53,29 @@ func (e *AppError) WithStack() *AppError {
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{
Type: errType,
func New(cause error, code int, message string) *Error {
return &Error{
Message: message,
Cause: cause,
Code: code,
}
}
// Wrap 包装现有错误为 AppError
func Wrap(err error, errType, message string, code int) *AppError {
func Newf(cause error, code int, format string, args ...interface{}) *Error {
return &Error{
Message: fmt.Sprintf(format, args...),
Cause: cause,
Code: http.StatusInternalServerError,
}
}
func Wrap(err error, message string, code int) *Error {
if err == nil {
return nil
}
// 如果已经是 AppError保留原始类型但更新消息
if appErr, ok := err.(*AppError); ok {
return &AppError{
Type: appErr.Type,
if appErr, ok := err.(*Error); ok {
return &Error{
Message: message,
Cause: appErr.Cause,
Code: appErr.Code,
@@ -108,44 +83,15 @@ func Wrap(err error, errType, message string, code int) *AppError {
}
}
return New(errType, message, err, code)
return New(err, code, message)
}
// 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
var appErr *Error
if errors.As(err, &appErr) {
return appErr.Code
}
@@ -153,7 +99,6 @@ func GetCode(err error) int {
return http.StatusInternalServerError
}
// RootCause 获取错误链中的根本原因
func RootCause(err error) error {
for err != nil {
unwrapped := errors.Unwrap(err)
@@ -165,81 +110,11 @@ func RootCause(err error) error {
return err
}
// ErrInvalidArg 无效参数错误
func ErrInvalidArg(param string) *AppError {
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).WithStack()
}
// WeChat 创建微信相关错误
func WeChat(message string, cause error) *AppError {
return New(ErrTypeWeChat, message, cause, http.StatusInternalServerError).WithStack()
}
// HTTP 创建HTTP服务错误
func HTTP(message string, cause error) *AppError {
return New(ErrTypeHTTP, message, cause, http.StatusInternalServerError).WithStack()
}
// Config 创建配置错误
func Config(message string, cause error) *AppError {
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
}
if appErr, ok := err.(*Error); ok {
c.JSON(appErr.Code, appErr)
return
}
// 未知错误
unknownErr := &AppError{
Type: "unknown",
Message: err.Error(),
Code: http.StatusInternalServerError,
RequestID: requestID,
}
c.JSON(http.StatusInternalServerError, unknownErr)
c.JSON(http.StatusInternalServerError, err.Error())
}