errcode

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// 预定义错误
	// Common errors
	Success               = NewError(0, "Ok")
	ErrInternalServer     = NewError(10000, "Internal server error")
	ErrInvalidParam       = NewError(10001, "Invalid params")
	ErrUnauthorized       = NewError(10002, "Unauthorized error")
	ErrNotFound           = NewError(10003, "Not found")
	ErrUnknown            = NewError(10004, "Unknown")
	ErrDeadlineExceeded   = NewError(10005, "Deadline exceeded")
	ErrAccessDenied       = NewError(10006, "Access denied")
	ErrLimitExceed        = NewError(10007, "Beyond limit")
	ErrMethodNotAllowed   = NewError(10008, "Method not allowed")
	ErrSignParam          = NewError(10011, "Invalid sign")
	ErrValidation         = NewError(10012, "Validation failed")
	ErrDatabase           = NewError(10013, "Database error")
	ErrToken              = NewError(10014, "Gen token error")
	ErrInvalidToken       = NewError(10015, "Invalid token")
	ErrTokenTimeout       = NewError(10016, "Token timeout")
	ErrTooManyRequests    = NewError(10017, "Too many request")
	ErrInvalidTransaction = NewError(10018, "Invalid transaction")
	ErrEncrypt            = NewError(10019, "Encrypting the user password error")
	ErrServiceUnavailable = NewError(10020, "Service Unavailable")
)

nolint: golint

Functions

func DecodeErr

func DecodeErr(err error) (int, string)

DecodeErr 对错误进行解码,返回错误code和错误提示

func Errorf added in v1.1.3

func Errorf(msg string, args ...interface{}) error

func NewDetails

func NewDetails(details map[string]any) proto.Message

NewDetails .

func SetHTTPStatusCode

func SetHTTPStatusCode(err *Error, status int)

SetHTTPStatusCode set a specific http status code to err

func Stack added in v1.1.3

func Stack(calldepth int) []byte

Taken from runtime/debug.go

func ToHTTPStatusCode

func ToHTTPStatusCode(code int) int

ToHTTPStatusCode convert custom error code to http status code and avoid return unknown status code.

func ToRPCCode

func ToRPCCode(code int) codes.Code

ToRPCCode 自定义错误码转换为RPC识别的错误码,避免返回Unknown状态码

Types

type Err

type Err struct {
	Code    int
	Message string
	Err     error
}

Err represents an error

func (*Err) Error

func (err *Err) Error() string

Error return error string

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error 返回错误码和消息的结构体 nolint: govet

func NewError

func NewError(code int, msg string) *Error

NewError create a error

func (*Error) Code

func (e *Error) Code() int

Code return error code

func (*Error) Details

func (e *Error) Details() []string

Details return more error details

func (Error) Error

func (e Error) Error() string

Error return a error string

func (*Error) Msg

func (e *Error) Msg() string

Msg return error msg

func (*Error) Msgf

func (e *Error) Msgf(args []any) string

Msgf format error string

func (*Error) WithDetails

func (e *Error) WithDetails(details ...string) *Error

WithDetails return err with detail

type GrpcStatus

type GrpcStatus struct {
	// contains filtered or unexported fields
}

GrpcStatus grpc error

func New

func New(code codes.Code, msg string) *GrpcStatus

New instance a status

func (*GrpcStatus) Status

func (g *GrpcStatus) Status(details ...proto.Message) *status.Status

Status .

func (*GrpcStatus) WithDetails

func (g *GrpcStatus) WithDetails(details ...proto.Message) *GrpcStatus

WithDetails .

type StackError added in v1.1.3

type StackError interface {
	Error() string
	StackTrace() string
}

This package exposes some handy traceback functionality buried in the runtime.

It can also be used to provide context to errors reducing the temptation to panic carelessly, just to get stack information.

The theory is that most errors that are created with the fmt.Errorf style are likely to be rare, but require more context to debug properly. The additional cost of computing a stack trace is therefore negligible.

Jump to

Keyboard shortcuts

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