errcode

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: MIT Imports: 6 Imported by: 0

README

错误码设计

参考 新浪开放平台 Error code 的设计

错误返回值格式
{
  "code": 10002,
  "message": "Error occurred while binding the request body to the struct."
}
错误代码说明
1 00 02
服务级错误(1为系统级错误) 服务模块代码 具体错误代码
  • 服务级别错误:1 为系统级错误;2 为普通错误,通常是由用户非法操作引起的
  • 服务模块为两位数:一个大型系统的服务模块通常不超过两位数,如果超过,说明这个系统该拆分了
  • 错误码为两位数:防止一个模块定制过多的错误码,后期不好维护
  • code = 0 说明是正确返回,code > 0 说明是错误返回
  • 错误通常包括系统级错误码和服务级错误码
  • 建议代码中按服务模块将错误分类
  • 错误码均为 >= 0 的数
  • 在本项目中 HTTP Code 固定为 http.StatusOK,错误码通过 code 来表示。

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 NewDetails

func NewDetails(details map[string]interface{}) proto.Message

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 []interface{}) 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
}

func New

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

func (*GrpcStatus) Status

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

func (*GrpcStatus) WithDetails

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

Jump to

Keyboard shortcuts

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