errcode

package
v1.3.20 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: MIT Imports: 7 Imported by: 6

README

errcode

Error codes usually include system-level error codes and service-level error codes, consisting of a total of 5 decimal digits, e.g. 20101

First digit Middle two digits Last two digits
For http error codes, 2 indicates a service level error (1 is a system level error) Service Module Code Specific error codes
For grpc error codes, 4 indicates a service level error (3 is a system level error) Service Module Code Specific error codes
  • Error levels occupy one digit: 1 (http) and 3 (grpc) indicate system-level errors, 2 (http) and 4 (grpc) indicate service-level errors, usually caused by illegal user operations.
  • Double-digit service modules: A large system usually has no more than two service modules; if it exceeds that, it's time to split the system.
  • Error codes take up two digits: prevents a module from being customised with too many error codes, which are not well maintained later.

Example of use

Example of http error code usage

    // defining error codes
    var ErrLogin = errcode.NewError(20101, "incorrect username or password")

    // return error
    response.Error(c, errcode.LoginErr)

Example of grpc error code usage

    // defining error codes
    var ErrLogin = NewRPCStatus(40101, "incorrect username or password")

    // return error
    errcode.ErrLogin.Err()
    // return with error details
    errcode.ErrLogin.Err(errcode.Any("err", err))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Success             = NewError(0, "ok")
	InvalidParams       = NewError(10001, "Invalid Parameter")
	Unauthorized        = NewError(10002, "Unauthorized")
	InternalServerError = NewError(10003, "Internal Server Error")
	NotFound            = NewError(10004, "Not Found")
	AlreadyExists       = NewError(10005, "Conflict")
	Timeout             = NewError(10006, "Request Timeout")
	TooManyRequests     = NewError(10007, "Too Many Requests")
	Forbidden           = NewError(10008, "Forbidden")
	LimitExceed         = NewError(10009, "Limit Exceed")
	DeadlineExceeded    = NewError(10010, "Deadline Exceeded")
	AccessDenied        = NewError(10011, "Access Denied")
	MethodNotAllowed    = NewError(10012, "Method Not Allowed")
	ServiceUnavailable  = NewError(10013, "Service Unavailable")
)

nolint http system level error code, error code range 10000~20000

View Source
var (
	StatusSuccess = NewRPCStatus(0, "ok")

	StatusInvalidParams       = NewRPCStatus(30001, "Invalid Parameter")
	StatusUnauthorized        = NewRPCStatus(30002, "Unauthorized")
	StatusInternalServerError = NewRPCStatus(30003, "Internal Server Error")
	StatusNotFound            = NewRPCStatus(30004, "Not Found")
	StatusAlreadyExists       = NewRPCStatus(30005, "Conflict")
	StatusTimeout             = NewRPCStatus(30006, "Request Timeout")
	StatusTooManyRequests     = NewRPCStatus(30007, "Too Many Requests")
	StatusForbidden           = NewRPCStatus(30008, "Forbidden")
	StatusLimitExceed         = NewRPCStatus(30009, "Limit Exceed")
	StatusDeadlineExceeded    = NewRPCStatus(30010, "Deadline Exceeded")
	StatusAccessDenied        = NewRPCStatus(30011, "Access Denied")
	StatusMethodNotAllowed    = NewRPCStatus(30012, "Method Not Allowed")
	StatusServiceUnavailable  = NewRPCStatus(30013, "Service Unavailable")
)

nolint rpc system level error code with status prefix, error code range 30000~40000

Functions

func HCode

func HCode(NO int) int

HCode Generate an error code between 20000 and 30000 according to the number

http service level error code, Err prefix, example.

var ( ErrUserCreate = NewError(HCode(1)+1, "failed to create user") // 200101 ErrUserDelete = NewError(HCode(1)+2, "failed to delete user") // 200102 ErrUserUpdate = NewError(HCode(1)+3, "failed to update user") // 200103 ErrUserGet = NewError(HCode(1)+4, "failed to get user details") // 200104 )

func RCode added in v1.2.0

func RCode(NO int) codes.Code

RCode Generate an error code between 40000 and 50000 according to the number

rpc service level error code, status prefix, example.

var (
	StatusUserCreate = NewRPCStatus(RCode(1)+1, "failed to create user")		// 40101
	StatusUserDelete = NewRPCStatus(RCode(1)+2, "failed to delete user")		// 40102
	StatusUserUpdate = NewRPCStatus(RCode(1)+3, "failed to update user")		// 40103
	StatusUserGet    = NewRPCStatus(RCode(1)+4, "failed to get user details")	// 40104
)

Types

type Detail

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

Detail error details

func Any

func Any(key string, val interface{}) Detail

Any type key value

func (*Detail) String

func (d *Detail) String() string

String detail key-value

type Error

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

Error error

func NewError

func NewError(code int, msg string) *Error

NewError create a new error message

func ParseError added in v1.2.0

func ParseError(err error) *Error

ParseError parsing out error codes from error messages

func ToHTTPErr added in v1.2.0

func ToHTTPErr(st *status.Status) *Error

ToHTTPErr converted to http error

func (*Error) Code

func (e *Error) Code() int

Code get error code

func (*Error) Details

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

Details get error code details

func (*Error) Err added in v1.2.0

func (e *Error) Err() error

Err covert to standard error

func (*Error) Msg

func (e *Error) Msg() string

Msg get error code message

func (*Error) ToHTTPCode

func (e *Error) ToHTTPCode() int

ToHTTPCode convert to http error code

func (*Error) WithDetails

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

WithDetails add error details

type RPCStatus added in v1.2.0

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

RPCStatus rpc status

func NewRPCStatus added in v1.2.0

func NewRPCStatus(code codes.Code, msg string) *RPCStatus

NewRPCStatus create a new rpc status

func (*RPCStatus) Err added in v1.2.0

func (g *RPCStatus) Err(details ...Detail) error

Err return error

func (*RPCStatus) ToRPCCode added in v1.2.0

func (g *RPCStatus) ToRPCCode() codes.Code

ToRPCCode converted to standard RPC error code

func (*RPCStatus) ToRPCErr added in v1.2.0

func (g *RPCStatus) ToRPCErr(desc ...string) error

ToRPCErr converted to standard RPC error

type Responser added in v1.2.0

type Responser interface {
	Success(ctx *gin.Context, data interface{})
	ParamError(ctx *gin.Context, err error)
	Error(ctx *gin.Context, err error) bool
}

Responser response interface

func NewResponse added in v1.2.0

func NewResponse(isFromRPC bool) Responser

NewResponse creates a new response, if isFromRPC=true, it means return from rpc, otherwise default return from http

Jump to

Keyboard shortcuts

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