errors

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 4xxx 客户端错误
	ErrCodeBindFailed        = 4001 // 参数绑定错误
	ErrCodeValidationFailed  = 4002 // 数据验证失败
	ErrCodeNotFound          = 4003 // 资源不存在
	ErrCodeRouteNotFound     = 4004 // 路由不存在
	ErrCodeForbidden         = 4005 // 权限不足
	ErrCodeUnauthorized      = 4006 // 认证失败
	ErrCodeDuplicate         = 4007 // 资源已存在
	ErrCodeRateLimitExceeded = 4008 // 速率限制

	// Initialization errors
	ErrSystemAlreadyInitialized = 4009 // 系统已初始化
	ErrInvalidInitKey           = 4010 // 无效的初始化密钥
	ErrInitDataCreationFailed   = 5005 // 初始化数据创建失败

	// 5xxx 服务端错误
	ErrCodeDatabase       = 5001 // 数据库错误
	ErrCodeBusinessLogic  = 5002 // 业务逻辑错误
	ErrCodeFileUpload     = 5003 // 文件上传失败
	ErrCodeStorageService = 5004 // 存储服务错误
	ErrCodeInternal       = 5000 // 内部错误
)

Variables

View Source
var ErrorMessages = map[int]string{
	ErrCodeBindFailed:           "参数绑定错误",
	ErrCodeValidationFailed:     "数据验证失败",
	ErrCodeNotFound:             "资源不存在",
	ErrCodeRouteNotFound:        "路由不存在",
	ErrCodeForbidden:            "权限不足",
	ErrCodeUnauthorized:         "认证失败",
	ErrCodeDuplicate:            "资源已存在",
	ErrCodeRateLimitExceeded:    "速率限制超出",
	ErrSystemAlreadyInitialized: "系统已初始化",
	ErrInvalidInitKey:           "无效的初始化密钥",
	ErrInitDataCreationFailed:   "初始化数据创建失败",
	ErrCodeDatabase:             "数据库错误",
	ErrCodeBusinessLogic:        "业务逻辑错误",
	ErrCodeFileUpload:           "文件上传失败",
	ErrCodeStorageService:       "存储服务错误",
	ErrCodeInternal:             "内部错误",
}

ErrorMessages maps error codes to default messages

Functions

func GetHTTPStatus

func GetHTTPStatus(code int) int

GetHTTPStatus maps error codes to HTTP status codes

func GetMessage

func GetMessage(code int) string

GetMessage returns the default message for an error code

Types

type AppError

type AppError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Err     error  `json:"-"`
}

AppError represents an application error with code and message

func NewBadRequestError

func NewBadRequestError(message string, err ...error) *AppError

NewBadRequestError creates a new bad request error

func NewConflictError

func NewConflictError(message string, err ...error) *AppError

NewConflictError creates a new conflict error

func NewForbiddenError

func NewForbiddenError(message string, err ...error) *AppError

NewForbiddenError creates a new forbidden error

func NewInternalError

func NewInternalError(message string, err ...error) *AppError

NewInternalError creates a new internal error

func NewNotFoundError

func NewNotFoundError(message string, err ...error) *AppError

NewNotFoundError creates a new not found error

func NewUnauthorizedError

func NewUnauthorizedError(message string, err ...error) *AppError

NewUnauthorizedError creates a new unauthorized error

func NewValidationError

func NewValidationError(message string, err ...error) *AppError

NewValidationError creates a new validation error

func (*AppError) Error

func (e *AppError) Error() string

Jump to

Keyboard shortcuts

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