ecode

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

ecode

背景

错误码一般被用来进行异常传递,且需要具有携带message文案信息的能力。

错误码之Codes

sky_blue里,错误码被设计成Codes接口,声明如下代码位置

// Codes ecode error interface which has a code & message.
type Codes interface {
	// sometimes Error return Code in string form
	// NOTE: don't use Error in monitor report even it also work for now
	Error() string
	// Code get error code.
	Code() int
	// Message get code message.
	Message() string
	//Detail get error detail,it may be nil.
	Details() []interface{}
}

// A Code is an int error code spec.
type Code int

可以看到该接口一共有四个方法,且type Code int结构体实现了该接口。

注册message

一个Code错误码可以对应一个message,默认实现会从全局变量_messages中获取,业务可以将自定义Code对应的message通过调用Register方法的方式传递进去,如:

cms := map[int]string{
    0: "success",
    101: "param is error",
}
ecode.Register(cms)

fmt.Println(ecode.OK.Message()) // 输出:很好很强大!

注意:map[int]string类型并不是绝对,比如有业务要支持多语言的场景就可以扩展为类似map[int]LangStruct的结构,因为全局变量_messagesatomic.Value类型,只需要修改对应的Message方法实现即可。

Details

Details接口为gRPC预留

转换为ecode

错误码转换有以下两种情况:

  1. 因为框架传递错误是靠ecode错误码,比如bm框架返回的code字段默认就是数字,那么客户端接收到如{"code":404}的话,可以使用ec := ecode.Int(404)ec := ecode.String("404")来进行转换。
  2. 在项目中dao层返回一个错误码,往往返回参数类型建议为error而不是ecode.Codes,因为error更通用,那么上层service就可以使用ec := ecode.Cause(err)进行转换。

判断

错误码判断是否相等:

  1. ecodeecode判断使用:ecode.Equal(ec1, ec2)
  2. ecodeerror判断使用:ecode.EqualError(ec, err)
package ecode

import (
    "github.com/zdao-pro/sky_blue/pkg/ecode"
)

var _ ecode.Codes

// UserErrCode
var (
    UserNotLogin = ecode.New(144, 'user no login');
)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OK = New(200, "success") //请求成功

	ServerErr          = New(500, "server internal error")      //服务器内部错误
	ParamInvaidErr     = New(101, "parameter is invalid")       //请求参数错误
	TokenInvidErr      = New(105, "The param token is invalid") //Token校验失败
	PermissionErr      = New(109, "premission denied")          //授权限制
	ForbiddenErr       = New(403, "no privilege")               //禁止进入
	NotModified        = New(-304, "error")                     // 木有改动
	TemporaryRedirect  = New(-307, "error")                     // 撞车跳转
	RequestErr         = New(-400, "error")                     // 请求错误
	Unauthorized       = New(-401, "error")                     // 未认证
	AccessDenied       = New(-403, "error")                     // 访问权限不足
	NothingFound       = New(-404, "error")                     // 啥都木有
	MethodNotAllowed   = New(-405, "error")                     // 不支持该方法
	Conflict           = New(-409, "error")                     // 冲突
	Canceled           = New(-498, "error")                     // 客户端取消请求
	ServiceUnavailable = New(-503, "error")                     // 过载保护,服务暂不可用
	Deadline           = New(-504, "error")                     // 服务调用超时
	LimitExceed        = New(-509, "error")                     // 超出限制
)

All common ecode

Functions

func Equal

func Equal(a, b Codes) bool

Equal equal a and b by code int.

func EqualError

func EqualError(code Codes, err error) bool

EqualError equal error

func Register

func Register(cm map[int]string)

Register register ecode message map.

Types

type Code

type Code int

A Code is an int error code spec.

func Int

func Int(i int) Code

Int parse code int to error.

func New

func New(e int, msg string) Code

New new a ecode.Codes by int value. NOTE: ecode must unique in global, the New will check repeat and then panic.

func String

func String(e string) Code

String parse code string to error.

func (Code) Code

func (e Code) Code() int

Code return error code

func (Code) Details

func (e Code) Details() []interface{}

Details return details.

func (Code) Error

func (e Code) Error() string

func (Code) Message

func (e Code) Message() string

Message return error message

type Codes

type Codes interface {
	// sometimes Error return Code in string form
	// NOTE: don't use Error in monitor report even it also work for now
	Error() string
	// Code get error code.
	Code() int
	// Message get code message.
	Message() string
	//Detail get error detail,it may be nil.
	Details() []interface{}
}

Codes ..

func Cause

func Cause(e error) Codes

Cause cause from error to ecode.

Jump to

Keyboard shortcuts

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