Documentation
¶
Index ¶
- Constants
- func HTTPStatus(code int) int
- func IsClientError(code int) bool
- func IsInternalError(code int) bool
- func IsServerError(code int) bool
- func NewBadRequestError(message string) error
- func NewError(code int, message string) error
- func NewErrorf(code int, format string, args ...interface{}) error
- func NewForbiddenError() error
- func NewNotFoundError(resource string) error
- func NewPermissionDeniedError(resource, action string) error
- func NewTokenExpiredError() error
- func NewTokenInvalidError() error
- func NewUnauthorizedError() error
- func NewValidationError(field, message string) error
- func WrapBadRequestError(err error, message string) error
- func WrapDatabaseError(err error, operation string) error
- func WrapError(err error, code int, message string) error
- func WrapErrorf(err error, code int, format string, args ...interface{}) error
- func WrapExternalError(err error, service, operation string) error
- func WrapForbiddenError(err error, message string) error
- func WrapInternalServerError(err error, message string) error
- func WrapKafkaError(err error, operation string) error
- func WrapNotFoundError(err error, message string) error
- func WrapRedisError(err error, operation string) error
- func WrapUnauthorizedError(err error, message string) error
- type ErrCode
- type ErrorCategory
- type ErrorInfo
- type ErrorType
Constants ¶
const ( // ErrSuccess - 200: OK. ErrSuccess int = iota + 100001 // ErrUnknown - 500: Internal server error. ErrUnknown // ErrBind - 400: Error occurred while binding the request body to the struct. ErrBind // ErrValidation - 400: Validation failed. ErrValidation // ErrTokenInvalid - 401: Token invalid. ErrTokenInvalid )
通用: 基本错误 Code must start with 1xxxxx.
const ( // ErrDatabase - 500: Database error. ErrDatabase int = iota + 100101 // ErrRedis - 500: Redis error. ErrRedis // ErrKafka - 500: Kafka error. ErrKafka // ErrExternalService - 500: External service error. ErrExternalService )
通用:数据库类错误.
const ( // ErrBadRequest - 400: Bad request. ErrBadRequest int = iota + 100400 ErrUnauthorized // ErrForbidden - 403: Forbidden. ErrForbidden // ErrNotFound - 404: Not found. ErrNotFound // ErrInternalServer - 500: Internal server error. ErrInternalServer )
通用:HTTP状态码错误.
const ( // ErrEncrypt - 401: Error occurred while encrypting the user password. ErrEncrypt int = iota + 100201 // ErrSignatureInvalid - 401: Signature is invalid. ErrSignatureInvalid // ErrExpired - 401: Token expired. ErrExpired // ErrInvalidAuthHeader - 401: Invalid authorization header. ErrInvalidAuthHeader // ErrMissingHeader - 401: The `Authorization` header was empty. ErrMissingHeader // ErrPasswordIncorrect - 401: Password was incorrect. ErrPasswordIncorrect // ErrPermissionDenied - 403: Permission denied. ErrPermissionDenied // ErrAccountLocked - 403: Account is locked. ErrAccountLocked // ErrAccountDisabled - 403: Account is disabled. ErrAccountDisabled // ErrTooManyAttempts - 403: Too many login attempts. ErrTooManyAttempts )
通用:认证授权类错误.
const ( // ErrEncodingFailed - 500: Encoding failed due to an error with the data. ErrEncodingFailed int = iota + 100301 // ErrDecodingFailed - 500: Decoding failed due to an error with the data. ErrDecodingFailed // ErrInvalidJSON - 500: Data is not valid JSON. ErrInvalidJSON // ErrEncodingJSON - 500: JSON data could not be encoded. ErrEncodingJSON // ErrDecodingJSON - 500: JSON data could not be decoded. ErrDecodingJSON // ErrInvalidYaml - 500: Data is not valid Yaml. ErrInvalidYaml // ErrEncodingYaml - 500: Yaml data could not be encoded. ErrEncodingYaml // ErrDecodingYaml - 500: Yaml data could not be decoded. ErrDecodingYaml )
通用:编解码类错误.
const ( // ErrUserNotFound - 404: User not found. ErrUserNotFound int = iota + 200000 // ErrUserAlreadyExists - 400: User already exists. ErrUserAlreadyExists // ErrUserInvalidData - 400: User invalid data. ErrUserInvalidData // ErrUserPermissionDenied - 403: User permission denied. ErrUserPermissionDenied // ErrUserInUse - 400: User is in use. ErrUserInUse // ErrUserCreateFailed - 500: User create failed. ErrUserCreateFailed // ErrUserUpdateFailed ErrUser - 500: User update failed. ErrUserUpdateFailed // ErrUserDeleteFailed - 500: User delete failed. ErrUserDeleteFailed )
User相关错误码
Variables ¶
This section is empty.
Functions ¶
func HTTPStatus ¶ added in v1.0.2
HTTPStatus 根据错误码返回对应的HTTP状态码 只支持标准的6个状态码:200, 400, 401, 403, 404, 500
func IsClientError ¶ added in v1.0.2
IsClientError 判断是否为客户端错误(4xx)
func IsInternalError ¶ added in v1.0.2
IsInternalError 判断是否为内部错误(需要记录详细日志)
func IsServerError ¶ added in v1.0.2
IsServerError 判断是否为服务器错误(5xx)
func NewBadRequestError ¶ added in v1.0.2
NewBadRequestError 请求错误
func NewNotFoundError ¶ added in v1.0.2
NewNotFoundError 资源不存在错误
func NewPermissionDeniedError ¶ added in v1.0.2
NewPermissionDeniedError 权限拒绝错误
func NewTokenExpiredError ¶ added in v1.0.2
func NewTokenExpiredError() error
NewTokenExpiredError Token过期错误
func NewTokenInvalidError ¶ added in v1.0.2
func NewTokenInvalidError() error
NewTokenInvalidError Token无效错误
func NewUnauthorizedError ¶ added in v1.0.2
func NewUnauthorizedError() error
NewUnauthorizedError 未授权错误
func NewValidationError ¶ added in v1.0.2
NewValidationError 验证错误
func WrapBadRequestError ¶ added in v1.0.2
WrapBadRequestError 包装400错误
func WrapDatabaseError ¶ added in v1.0.2
WrapDatabaseError 包装数据库错误
func WrapErrorf ¶ added in v1.0.2
WrapErrorf 包装错误并添加错误码(格式化)
func WrapExternalError ¶ added in v1.0.2
WrapExternalError 包装第三方服务错误
func WrapForbiddenError ¶ added in v1.0.2
WrapForbiddenError 包装403错误
func WrapInternalServerError ¶ added in v1.0.2
WrapInternalServerError 包装500错误
func WrapKafkaError ¶ added in v1.0.2
WrapKafkaError 包装Kafka错误
func WrapNotFoundError ¶ added in v1.0.2
WrapNotFoundError 包装404错误
func WrapRedisError ¶ added in v1.0.2
WrapRedisError 包装Redis错误
func WrapUnauthorizedError ¶ added in v1.0.2
WrapUnauthorizedError 包装401错误
Types ¶
type ErrCode ¶
type ErrCode struct {
// C refers to the code of the ErrCode.
C int
// HTTP status that should be used for the associated error code.
HTTP int
// External (user) facing error text.
Ext string
// Ref specify the reference document.
Ref string
}
ErrCode implements `github.com/marmotedu/errors`.Coder interface.
func (*ErrCode) HTTPStatus ¶
HTTPStatus returns the associated HTTP status code, if any. Otherwise, returns 200.
type ErrorCategory ¶ added in v1.0.2
type ErrorCategory string
ErrorCategory 错误分类
const ( // 内部错误分类 CategoryDatabase ErrorCategory = "database" // 数据库错误 CategoryRedis ErrorCategory = "redis" // Redis错误 CategoryKafka ErrorCategory = "kafka" // Kafka错误 CategoryExternal ErrorCategory = "external" // 第三方服务错误 CategorySystem ErrorCategory = "system" // 系统错误 // 业务错误分类 CategoryAuth ErrorCategory = "auth" // 认证错误 CategoryPermission ErrorCategory = "permission" // 权限错误 CategoryValidation ErrorCategory = "validation" // 参数验证错误 CategoryBusiness ErrorCategory = "business" // 业务逻辑错误 )
type ErrorInfo ¶ added in v1.0.2
type ErrorInfo struct {
Type ErrorType `json:"type"` // 错误类型
Category ErrorCategory `json:"category"` // 错误分类
Code int `json:"code"` // 业务错误码
Message string `json:"message"` // 错误消息
Details string `json:"details"` // 详细错误信息(内部错误时记录)
}
ErrorInfo 错误信息结构
func NewErrorInfo ¶ added in v1.0.2
NewErrorInfo 根据错误实例构造标准错误信息
func (*ErrorInfo) IsBusiness ¶ added in v1.0.2
IsBusiness 判断是否为业务错误
func (*ErrorInfo) IsInternal ¶ added in v1.0.2
IsInternal 判断是否为内部错误