errcode

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2022 License: MIT Imports: 5 Imported by: 6

README

errcode

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

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

使用示例

http错误码使用示例

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

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

grpc错误码使用示例

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

    // 返回错误
    errcode.ErrLogin.Err()
    // 返回附带错误详情信息
    errcode.ErrLogin.Err(errcode.Any("err", err))

Documentation

Index

Constants

This section is empty.

Variables

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

	StatusInvalidParams       = NewGRPCStatus(300001, "参数错误")
	StatusUnauthorized        = NewGRPCStatus(300002, "认证错误")
	StatusInternalServerError = NewGRPCStatus(300003, "服务内部错误")
	StatusNotFound            = NewGRPCStatus(300004, "资源不存在")
	StatusAlreadyExists       = NewGRPCStatus(300005, "资源已存在")
	StatusTimeout             = NewGRPCStatus(300006, "访问超时")
	StatusTooManyRequests     = NewGRPCStatus(300007, "请求过多")
	StatusForbidden           = NewGRPCStatus(300008, "拒绝访问")
	StatusLimitExceed         = NewGRPCStatus(300009, "访问限制")

	StatusDeadlineExceeded   = NewGRPCStatus(300010, "已超过最后期限")
	StatusAccessDenied       = NewGRPCStatus(300011, "拒绝访问")
	StatusMethodNotAllowed   = NewGRPCStatus(300012, "不允许使用的方法")
	StatusServiceUnavailable = NewGRPCStatus(300013, "服务不可用")
)

nolint rpc系统级别错误码,有status前缀

View Source
var (
	Success             = NewError(0, "ok")
	InvalidParams       = NewError(100001, "参数错误")
	Unauthorized        = NewError(100002, "认证错误")
	InternalServerError = NewError(100003, "服务内部错误")
	NotFound            = NewError(100004, "资源不存在")
	AlreadyExists       = NewError(100005, "资源已存在")
	Timeout             = NewError(100006, "超时")
	TooManyRequests     = NewError(100007, "请求过多")
	Forbidden           = NewError(100008, "拒绝访问")
	LimitExceed         = NewError(100009, "访问限制")

	DeadlineExceeded         = NewError(100010, "已超过最后期限")
	AccessDenied             = NewError(100011, "拒绝访问")
	MethodNotAllowed         = NewError(100012, "不允许使用的方法")
	MethodServiceUnavailable = NewError(100013, "服务不可用")
)

nolint http系统级别错误码,无Err前缀

Functions

func GCode

func GCode(NO int) codes.Code

GCode 根据编号生成400000~500000之间的错误码

func HCode

func HCode(NO int) int

HCode 根据编号生成200000~300000之间的错误码

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 错误

func NewError

func NewError(code int, msg string) *Error

NewError 创建新错误信息

func (*Error) Code

func (e *Error) Code() int

Code 错误码

func (*Error) Details

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

Details 错误详情

func (*Error) Error

func (e *Error) Error() string

String 打印错误

func (*Error) Msg

func (e *Error) Msg() string

Msg 错误信息

func (*Error) Msgf

func (e *Error) Msgf(args []interface{}) string

Msgf 附加信息

func (*Error) ToHTTPCode

func (e *Error) ToHTTPCode() int

ToHTTPCode 转换为http错误码

func (*Error) WithDetails

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

WithDetails 携带附加错误详情

type GRPCStatus

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

GRPCStatus grpc 状态

func NewGRPCStatus

func NewGRPCStatus(code codes.Code, msg string) *GRPCStatus

NewGRPCStatus 新建一个status

func (*GRPCStatus) Err

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

Err return error

func (*GRPCStatus) ToRPCCode

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

ToRPCCode converted to standard RPC error code

func (*GRPCStatus) ToRPCErr

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

ToRPCErr converted to standard RPC error

Jump to

Keyboard shortcuts

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