errcode

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2021 License: MIT Imports: 2 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, "Success")
	ErrInternalServer     = NewError(10001, "Internal server error")
	ErrBind               = NewError(10002, "Bind request error")
	ErrInvalidParam       = NewError(10003, "Invalid params")
	ErrSignParam          = NewError(10004, "Invalid sign")
	ErrValidation         = NewError(10005, "Validation failed")
	ErrDatabase           = NewError(10006, "Database error")
	ErrToken              = NewError(10007, "Gen token error")
	ErrInvalidToken       = NewError(10108, "Invalid token")
	ErrTokenTimeout       = NewError(10109, "Token timeout")
	ErrTooManyRequests    = NewError(10110, "Too many request")
	ErrInvalidTransaction = NewError(10111, "Invalid transaction")
	ErrEncrypt            = NewError(10112, "Encrypting the user password error")
	ErrLimitExceed        = NewError(10113, "Beyond limit")
	ErrServiceUnavailable = NewError(10114, "Service Unavailable")
)

nolint: golint

Functions

func DecodeErr

func DecodeErr(err error) (int, string)

DecodeErr 对错误进行解码,返回错误code和错误提示

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

func (e *Error) StatusCode() int

StatusCode trans err code to http status code

func (*Error) WithDetails

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

WithDetails return err with detail

Jump to

Keyboard shortcuts

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