Categorygithub.com/aluka-7/metacode
modulepackage
1.1.1
Repository: https://github.com/aluka-7/metacode.git
Documentation: pkg.go.dev

# README

项目简介

metadata 用于储存各种元信息

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

在这里,错误码被设计成Codes接口,声明如下:

// 错误代码接口,其中包含代码和消息.
type Codes interface {
    // 有时错误返回字符串形式的代码
    // 注意:请勿在监控器报告中使用“error”,即使它现在也可以使用
    Error() string
    // 获取错误代码.
    Code() int
    // 获取错误信息.
    Message() string
    // 获取错误详细信息,可能为nil.
    Details() []interface{}
}

// Code是int型错误代码规范.
type Code int

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

注册message

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

cms := map[int]string{
    0: "很好很强大!",
    -304: "啥都没变啊~",
    -404: "啥都没有啊~",
}
metacode.Register(cms)

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

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

Details

Details接口为gRrc预留,gRrc传递异常会将服务端的错误码pb序列化之后赋值给Details,客户端拿到之后反序列化得到,具体可阅读status的实现:

  1. code包内的Status结构体实现了Codes接口
  2. grpc/status包内包装了metacode.Statusgrpc.Status进行互相转换的方法
  3. grpcclientserver则使用转换方法将gRrc底层返回的error最终转换为metacode.Status

转换为code

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

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

判断

错误码判断是否相等:

  1. codecode判断使用:metacode.Equal(mc1, mc2)
  2. codeerror判断使用:metacode.EqualError(mc, err)

使用工具生成

使用proto协议定义错误码,格式如下:

// user.proto
syntax = "proto3";

package code;

enum UserErrCode { 
  UserUndefined = 0; // 因protobuf协议限制必须存在!!!无意义的0,工具生成代码时会忽略该参数
  UserNotLogin = 123; // 正式错误码
}

需要注意以下几点:

  1. 必须是enum类型,且名字规范必须以"ErrCode"结尾,如:UserErrCode
  2. 因为protobuf协议限制,第一个enum值必须为无意义的0

使用feo tool protoc --code user.proto进行生成

# Packages

No description provided by the author

# Functions

Bool get boolean from metadata in context use strconv.Parse.
Cause cause from error to code.
Equal equal a and b by code int.
EqualError equal error.
Error new status with code and message.
Errorf new status with code and message.
FromCode create status from code.
FromContext returns the incoming metacode in ctx if it exists.
FromProto new status from gRpc detail.
Int64 get int64 value from metadata in context.
Int parse code int to error.
IsIncomingKey represent this key should extract from rpc metadata.
IsOutgoingKey represent this key should propagate by rpc.
Join joins any number of mds into a single Metadata.
NewCode 新建一个新的元数据。 注意:代码必须在全局范围内唯一,新代码将检查重复,然后出现恐慌。.
NewContext creates a new context with md attached.
New creates an Metadata from a given key-value map.
Pairs returns an Metadata formed by the mapping of key, value ..
Range range value from metadata in context filtered by filterFunc.
Register register code message map.
String parse code string to error.
String get string value from metacode in context.
Value get value from metadata in context return nil if not found.
WithContext return no deadline context and retain metadata.

# Constants

metacode common key.
metacode common key.
Criticality 重要性.
Device 客户端信息.
Dispatch.
NOTE: !!!业务可重新修改key名!!!.
Mirror.
Network.
metacode common key.
metacode common key.
metacode common key.
Timeout.
Trace.

# Variables

访问权限不足.
客户端取消请求.
冲突.
服务调用超时.
超出限制.
不支持该方法.
啥都木有.
木有改动.
正确.
请求错误.
服务器错误.
过载保护,服务暂不可用.
成功.
撞车跳转.
未认证.
服务器请求参数校验出错.

# Structs

Status statusError is an alias of a status proto implement metacode.Codes.

# Interfaces

错误代码接口,其中包含代码和消息.

# Type aliases

Code是int型错误代码规范.
Metadata is a mapping from metadata keys to values.