Documentation
¶
Overview ¶
Package errors 提供统一的错误处理机制
Index ¶
- Constants
- func As(err error, target interface{}) bool
- func DefaultErrorHandler(w ResponseWriter, err error)
- func GetDefaultMessage(code int) string
- func GetDefaultStatus(code int) int
- func GinCustomError(c *gin.Context, code int, message string)
- func GinDefaultErrorMiddleware() gin.HandlerFunc
- func GinError(c *gin.Context, err *Error)
- func GinErrorMiddleware(filters ...ErrorFilter) gin.HandlerFunc
- func GinForbidden(c *gin.Context, reason string)
- func GinInternal(c *gin.Context, err error)
- func GinInvalidParam(c *gin.Context, paramName string)
- func GinMissingParam(c *gin.Context, paramName string)
- func GinNotFound(c *gin.Context, resource string)
- func GinUnauthorized(c *gin.Context, reason string)
- func HandleGinError(c *gin.Context, err error)
- func Is(err error, code int) bool
- func RecoveryMiddleware(next http.Handler) http.Handler
- type Error
- func Forbidden(reason string) *Error
- func Internal(err error) *Error
- func InvalidParam(paramName string) *Error
- func MissingParam(paramName string) *Error
- func New(code int) *Error
- func NotFound(resource string) *Error
- func SensitiveDataFilter(err *Error) *Error
- func Unauthorized(reason string) *Error
- func Wrap(err error, code int) *Error
- func WrapWithMessage(err error, code int, message string) *Error
- type ErrorFilter
- type ErrorHandler
- type ErrorHandlerFunc
- type ErrorResponse
- type ResponseWriter
Constants ¶
View Source
const ( // 通用错误码范围 1-999 ErrCodeUnknown = 1 // 未知错误 ErrCodeInternal = 2 // 内部错误 ErrCodeInvalidParam = 100 // 无效参数 ErrCodeForbidden = 403 // 禁止访问 ErrCodeNotFound = 404 // 资源不存在 ErrCodeTimeout = 408 // 超时 ErrCodeTooManyRequest = 429 // 请求过多 // 业务错误码范围 1000-1999 ErrCodeBusinessBase = 1000 ErrCodeInvalidToken = 1001 // 无效令牌 ErrCodeTokenExpired = 1002 // 令牌过期 // 数据库错误码范围 2000-2999 ErrCodeDBBase = 2000 ErrCodeDBConnection = 2001 // 数据库连接失败 ErrCodeDBQuery = 2002 // 数据库查询失败 ErrCodeDBInsert = 2003 // 数据库插入失败 ErrCodeDBUpdate = 2004 // 数据库更新失败 ErrCodeDBDelete = 2005 // 数据库删除失败 // 外部服务错误码范围 3000-3999 ErrCodeServiceBase = 3000 ErrCodeServiceTimeout = 3001 // 服务调用超时 ErrCodeServiceDown = 3002 // 服务不可用 )
预定义错误码
Variables ¶
This section is empty.
Functions ¶
func DefaultErrorHandler ¶
func DefaultErrorHandler(w ResponseWriter, err error)
DefaultErrorHandler 是默认的错误处理函数
func GinCustomError ¶
GinCustomError 在 gin 上下文中返回一个自定义错误
func GinDefaultErrorMiddleware ¶
func GinDefaultErrorMiddleware() gin.HandlerFunc
GinDefaultErrorMiddleware 创建一个默认的 gin 错误处理中间件
func GinErrorMiddleware ¶
func GinErrorMiddleware(filters ...ErrorFilter) gin.HandlerFunc
GinErrorMiddleware 创建一个 gin 错误处理中间件
func GinForbidden ¶
GinForbidden 在 gin 上下文中返回一个禁止访问错误
func GinInvalidParam ¶
GinInvalidParam 在 gin 上下文中返回一个无效参数错误
func GinMissingParam ¶
GinMissingParam 在 gin 上下文中返回一个缺少参数错误
func GinNotFound ¶
GinNotFound 在 gin 上下文中返回一个资源不存在错误
func GinUnauthorized ¶
GinUnauthorized 在 gin 上下文中返回一个未授权错误
Types ¶
type Error ¶
type Error struct {
// Code 错误码
Code int `json:"code"`
// Message 错误信息
Message string `json:"message"`
// Data 附加信息
Data interface{} `json:"data,omitempty"`
// Status HTTP状态码
Status int `json:"-"`
// Cause 原始错误
Cause error `json:"-"`
// File 发生错误的文件
File string `json:"-"`
// Line 发生错误的行号
Line int `json:"-"`
// Stack 调用栈
Stack string `json:"-"`
}
Error 表示一个应用错误
func WrapWithMessage ¶
WrapWithMessage 包装错误并设置自定义消息
type ErrorHandler ¶
type ErrorHandler interface {
HandleError(ctx types.RequestContext, err error)
}
ErrorHandler 错误处理器接口
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(w ResponseWriter, err error)
ErrorHandlerFunc 表示一个错误处理函数
type ErrorResponse ¶
type ErrorResponse struct {
Code int `json:"code"` // 错误码
Message string `json:"message"` // 错误消息
Data interface{} `json:"data,omitempty"`
}
ErrorResponse 表示一个错误响应
type ResponseWriter ¶
type ResponseWriter interface {
http.ResponseWriter
JSON(code int, obj interface{})
}
ResponseWriter 接口定义了一个可以写入 HTTP 响应的对象
Click to show internal directories.
Click to hide internal directories.