errcode

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2022 License: MIT Imports: 4 Imported by: 0

README

errcode

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

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

安装

go get -u github.com/zhufuyi/pkg/errcode


使用示例

http错误码使用示例

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

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

grpc错误码使用示例

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

    // 返回错误
    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(10001, "参数错误")
	StatusUnauthorized        = NewGRPCStatus(10002, "认证错误")
	StatusInternalServerError = NewGRPCStatus(10003, "服务内部错误")
	StatusNotFound            = NewGRPCStatus(10004, "资源不存在")
	StatusAlreadyExists       = NewGRPCStatus(10005, "资源已存在")
	StatusTimeout             = NewGRPCStatus(10006, "超时")
	StatusTooManyRequests     = NewGRPCStatus(10007, "请求过多")
	StatusForbidden           = NewGRPCStatus(10008, "拒绝访问")
	StatusLimitExceed         = NewGRPCStatus(10009, "访问限制")

	StatusDeadlineExceeded = NewGRPCStatus(10010, "已超过最后期限")
	StatusAccessDenied     = NewGRPCStatus(10011, "拒绝访问")
	StatusMethodNotAllowed = NewGRPCStatus(10012, "不允许使用的方法")
)

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

View Source
var (
	Success             = NewError(0, "ok")
	InvalidParams       = NewError(10001, "参数错误")
	Unauthorized        = NewError(10002, "认证错误")
	InternalServerError = NewError(10003, "服务内部错误")
	NotFound            = NewError(10004, "资源不存在")
	AlreadyExists       = NewError(10005, "资源已存在")
	Timeout             = NewError(10006, "超时")
	TooManyRequests     = NewError(10007, "请求过多")
	Forbidden           = NewError(10008, "拒绝访问")
	LimitExceed         = NewError(10009, "访问限制")

	DeadlineExceeded = NewError(10010, "已超过最后期限")
	AccessDenied     = NewError(10011, "拒绝访问")
	MethodNotAllowed = NewError(10012, "不允许使用的方法")
)

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

Functions

func RPCErr

func RPCErr(g *GRPCStatus, details ...Detail) error

RPCErr rpc error

func ToRPCCode

func ToRPCCode(code int) codes.Code

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

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

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) StatusCode

func (e *Error) StatusCode() int

StatusCode 对应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

Jump to

Keyboard shortcuts

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