errors

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package errors 提供统一的错误处理机制

Index

Constants

View Source
const (
	// 通用错误码范围 1-999
	ErrCodeUnknown        = 1   // 未知错误
	ErrCodeInternal       = 2   // 内部错误
	ErrCodeInvalidParam   = 100 // 无效参数
	ErrCodeUnauthorized   = 401 // 未授权
	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 As

func As(err error, target interface{}) bool

As 将错误转换为指定类型

func DefaultErrorHandler

func DefaultErrorHandler(w ResponseWriter, err error)

DefaultErrorHandler 是默认的错误处理函数

func GetDefaultMessage

func GetDefaultMessage(code int) string

GetDefaultMessage 获取错误码对应的默认错误信息

func GetDefaultStatus

func GetDefaultStatus(code int) int

GetDefaultStatus 获取错误码对应的默认HTTP状态码

func GinCustomError

func GinCustomError(c *gin.Context, code int, message string)

GinCustomError 在 gin 上下文中返回一个自定义错误

func GinDefaultErrorMiddleware

func GinDefaultErrorMiddleware() gin.HandlerFunc

GinDefaultErrorMiddleware 创建一个默认的 gin 错误处理中间件

func GinError

func GinError(c *gin.Context, err *Error)

GinError 在 gin 上下文中返回一个错误

func GinErrorMiddleware

func GinErrorMiddleware(filters ...ErrorFilter) gin.HandlerFunc

GinErrorMiddleware 创建一个 gin 错误处理中间件

func GinForbidden

func GinForbidden(c *gin.Context, reason string)

GinForbidden 在 gin 上下文中返回一个禁止访问错误

func GinInternal

func GinInternal(c *gin.Context, err error)

GinInternal 在 gin 上下文中返回一个内部错误

func GinInvalidParam

func GinInvalidParam(c *gin.Context, paramName string)

GinInvalidParam 在 gin 上下文中返回一个无效参数错误

func GinMissingParam

func GinMissingParam(c *gin.Context, paramName string)

GinMissingParam 在 gin 上下文中返回一个缺少参数错误

func GinNotFound

func GinNotFound(c *gin.Context, resource string)

GinNotFound 在 gin 上下文中返回一个资源不存在错误

func GinUnauthorized

func GinUnauthorized(c *gin.Context, reason string)

GinUnauthorized 在 gin 上下文中返回一个未授权错误

func HandleGinError

func HandleGinError(c *gin.Context, err error)

为 gin.Context 提供的错误处理函数

func Is

func Is(err error, code int) bool

Is 检查错误码是否匹配

func RecoveryMiddleware

func RecoveryMiddleware(next http.Handler) http.Handler

RecoveryMiddleware 处理 panic 并将其转换为错误响应

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 Forbidden

func Forbidden(reason string) *Error

Forbidden 创建禁止访问错误

func Internal

func Internal(err error) *Error

Internal 创建内部错误

func InvalidParam

func InvalidParam(paramName string) *Error

InvalidParam 创建无效参数错误

func MissingParam

func MissingParam(paramName string) *Error

MissingParam 创建缺少参数错误

func New

func New(code int) *Error

New 创建一个新的错误

func NotFound

func NotFound(resource string) *Error

NotFound 创建资源不存在错误

func SensitiveDataFilter

func SensitiveDataFilter(err *Error) *Error

SensitiveDataFilter 隐藏敏感错误信息

func Unauthorized

func Unauthorized(reason string) *Error

Unauthorized 创建未授权错误

func Wrap

func Wrap(err error, code int) *Error

Wrap 包装一个已有的错误

func WrapWithMessage

func WrapWithMessage(err error, code int, message string) *Error

WrapWithMessage 包装错误并设置自定义消息

func (*Error) Error

func (e *Error) Error() string

Error 实现error接口

func (*Error) GetStatus

func (e *Error) GetStatus() int

GetStatus 获取HTTP状态码

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap 获取底层错误

func (*Error) WithCause

func (e *Error) WithCause(cause error) *Error

WithCause 设置原始错误

func (*Error) WithData

func (e *Error) WithData(data interface{}) *Error

WithData 设置附加数据

func (*Error) WithMessage

func (e *Error) WithMessage(message string) *Error

WithMessage 设置错误信息

func (*Error) WithStatus

func (e *Error) WithStatus(status int) *Error

WithStatus 设置HTTP状态码

type ErrorFilter

type ErrorFilter func(err *Error) *Error

ErrorFilter 是一个错误过滤器,用于隐藏或修改特定错误信息

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 响应的对象

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL