Documentation
¶
Overview ¶
Package errors 提供错误处理。
核心功能:
- 错误码定义(HTTP/gRPC)
- 错误构造器
- 错误响应转换
使用示例:
err := errors.New(errors.CodeNotFound, "user not found") err = errors.Wrap(err, errors.CodeInternal, "failed to get user") httpCode := err.HTTPStatus()
Index ¶
- func As(err error, target any) bool
- func GRPCCode(err error) codes.Code
- func GetHTTPStatus(err error) int
- func GetMessage(err error) string
- func Is(err, target error) bool
- func IsCode(err error, code Code) bool
- func Join(errs ...error) error
- func RegisterCode(code Code, httpStatus int, grpcCode codes.Code, message string)
- func ToGRPCStatus(err error) *status.Status
- type Code
- type Error
- func BadRequest(message string) *Error
- func Conflict(message string) *Error
- func ErrAlreadyExists(message string) *Error
- func ErrCache(err error) *Error
- func ErrDatabase(err error) *Error
- func ErrInternal(message string) *Error
- func ErrInvalidArgument(message string) *Error
- func ErrMQ(err error) *Error
- func ErrMissingParam(param string) *Error
- func ErrNotFound(message string) *Error
- func ErrPermissionDenied(message string) *Error
- func ErrThirdParty(err error, service string) *Error
- func ErrTimeout(message string) *Error
- func ErrUnauthenticated(message string) *Error
- func ErrUnavailable(message string) *Error
- func ErrValidation(message string) *Error
- func Forbidden(message string) *Error
- func FromCode(code Code) *Error
- func FromGRPCError(err error) *Error
- func FromGRPCStatus(s *status.Status) *Error
- func InternalErr(err error) *Error
- func New(code Code, message string) *Error
- func Newf(code Code, format string, args ...any) *Error
- func NotFoundErr(message string) *Error
- func ServiceUnavailable(message string) *Error
- func Unauthorized(message string) *Error
- func Wrap(err error, code Code, message string) *Error
- func Wrapf(err error, code Code, format string, args ...any) *Error
- func (e *Error) Code() Code
- func (e *Error) Error() string
- func (e *Error) GetMeta(key string) any
- func (e *Error) HTTPStatus() int
- func (e *Error) Is(target error) bool
- func (e *Error) LogFields() map[string]any
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) Message() string
- func (e *Error) Metadata() map[string]any
- func (e *Error) StackTrace() []string
- func (e *Error) Unwrap() error
- func (e *Error) WithMeta(key string, value any) *Error
- func (e *Error) WithMetadata(meta map[string]any) *Error
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterCode ¶
RegisterCode 注册自定义错误码
Types ¶
type Code ¶
type Code int
Code 错误码
const ( // 成功 OK Code = 0 // 通用错误 1-999 Unknown Code = 1 Canceled Code = 2 InvalidArgument Code = 3 NotFound Code = 4 AlreadyExists Code = 5 PermissionDenied Code = 6 Unauthenticated Code = 7 ResourceExhausted Code = 8 FailedPrecondition Code = 9 Aborted Code = 10 OutOfRange Code = 11 Unimplemented Code = 12 Internal Code = 13 DataLoss Code = 15 Timeout Code = 16 // 认证/授权 1000-1999 TokenExpired Code = 1001 TokenInvalid Code = 1002 TokenMissing Code = 1003 AccessDenied Code = 1004 SessionExpired Code = 1005 // 参数校验 2000-2999 ValidationFailed Code = 2001 MissingParameter Code = 2002 InvalidFormat Code = 2003 OutOfBounds Code = 2004 // 业务错误 3000-3999 UserNotFound Code = 3001 UserExists Code = 3002 OrderNotFound Code = 3003 InsufficientBalance Code = 3004 // 外部服务 4000-4999 DatabaseError Code = 4001 CacheError Code = 4002 MQError Code = 4003 ThirdPartyError Code = 4004 NetworkError Code = 4005 // 系统错误 5000-5999 SystemError Code = 5001 ConfigError Code = 5002 InitError Code = 5003 )
错误码定义 (按模块划分区间) 0: 成功 1-999: 通用错误 1000-1999: 认证/授权 2000-2999: 参数校验 3000-3999: 业务错误 4000-4999: 外部服务 5000-5999: 系统错误
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error 应用错误
func FromGRPCStatus ¶
FromGRPCStatus 从 gRPC Status 创建错误
Click to show internal directories.
Click to hide internal directories.