errcode

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 7 Imported by: 0

README

errcode

错误码通常包括系统级错误码和服务级错误码,一共5位十进制数字组成,例如20101

第一位数字 中间两位数字 最后两位数字
对于http错误码,2表示服务级错误(1为系统级错误) 服务模块代码 具体错误代码
对于grpc错误码,4表示服务级错误(3为系统级错误) 服务模块代码 具体错误代码
  • 错误级别占一位数:1(http)和3(grpc)表示系统级错误,2(http)和4(grpc)表示服务级错误,通常是由用户非法操作引起的。
  • 服务模块占两位数:一个大型系统的服务模块通常不超过两位数,如果超过,说明这个系统该拆分了。
  • 错误码占两位数:防止一个模块定制过多的错误码,后期不好维护。

使用示例

http错误码使用示例

    // 定义错误码
    var ErrLogin = errcode.NewError(20101, "用户名或密码错误")

    // 请求返回
    response.Error(c, errcode.LoginErr)

grpc错误码使用示例

    // 定义错误码
    var ErrLogin = NewRPCStatus(40101, "用户名或密码错误")

    // 返回错误
    errcode.ErrLogin.Err()
    // 返回附带错误详情信息
    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 added in v1.4.0

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.4.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.4.0

func ParseError(err error) *Error

ParseError parsing out error codes from error messages

func ToHTTPErr added in v1.4.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.4.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 added in v1.4.0

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.4.0

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

RPCStatus rpc status

func NewRPCStatus added in v1.4.0

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

NewRPCStatus create a new rpc status

func (*RPCStatus) Err added in v1.4.0

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

Err return error

func (*RPCStatus) ToRPCCode added in v1.4.0

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

ToRPCCode converted to standard RPC error code

func (*RPCStatus) ToRPCErr added in v1.4.0

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

ToRPCErr converted to standard RPC error

type Responser added in v1.4.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.4.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