errno

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 2 Imported by: 2

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 (
	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 common error 预定义错误

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 (e *Err) Error() string

Error 格式化

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 获取code

func (*Error) Details

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

Details 获取details

func (*Error) Error

func (e *Error) Error() string

Error 获取错误信息

func (*Error) Msg

func (e *Error) Msg() string

Msg 获取msg

func (*Error) StatusCode

func (e *Error) StatusCode() int

StatusCode 状态码

func (*Error) WithDetails

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

WithDetails 设置details数据

Jump to

Keyboard shortcuts

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