当前位置: 首页 > news >正文

go-错误码的最佳实践

一、背景

在工程开发中,我们有以下场景可以用错误码解决

  1. 我们不太方便直接将内部的错误原因暴露给外部,可以根据错误码得到对应的外部暴露消息
  2. 通过设定错误码判断是客户端或者服务端的问题,避免不必要的排障浪费
  3. 方便查找日志,定位问题

错误码设计

设计带有错误码的error

核心代码

type withCode struct {msg   stringcode  int
}func WithCode(code int, format string, args ...interface{}) error {msg := fmt.Sprintf(format, args...)return errors.Wrap(&withCode{msg: msg,code:  code,}, msg)
}// Error return the externally-safe error message.
func (w *withCode) Error() string { return msg }func ParseCoder(err error) Coder {codeErr := &withCoder{}if errors.As(err, codeErr) {if coder, ok := codes[codeErr.code]; ok {return coder}}return UnknownCoder
}

完整代码
github.com/marmotedu/errors v1.0.2

Coder的设计

提供register方法将错误码和相关信息注册在map内部,需要用到错误码信息的时候根据code拿到相关信息返回即可
接口设计

type Coder interface {// HTTP status that should be used for the associated error code.HTTPStatus() int// External (user) facing error text.String() string// Code returns the code of the coderCode() int
}

完整实现

var codes = map[int]Coder{}
var codeMux = &sync.Mutex{}type Coder interface {// HTTP status that should be used for the associated error code.HTTPStatus() int// External (user) facing error text.String() string// Code returns the code of the coderCode() int
}type ErrCode struct {// C refers to the code of the ErrCode.C int// HTTP status that should be used for the associated error code.HTTP int// External (user) facing error text.Ext string
}var _ errors.Coder = &ErrCode{}// Code returns the integer code of ErrCode.
func (coder ErrCode) Code() int {return coder.C
}// String implements stringer. String returns the external error message,
// if any.
func (coder ErrCode) String() string {return coder.Ext
}// HTTPStatus returns the associated HTTP status code, if any. Otherwise,
// returns 200.
func (coder ErrCode) HTTPStatus() int {if coder.HTTP == 0 {return http.StatusInternalServerError}return coder.HTTP
}// MustRegister register a user define error code.
// It will panic when the same Code already exist.
func MustRegister(coder Coder) {if coder.Code() == 0 {panic("code '0' is reserved by 'github.com/marmotedu/errors' as ErrUnknown error code")}codeMux.Lock()defer codeMux.Unlock()if _, ok := codes[coder.Code()]; ok {panic(fmt.Sprintf("code: %d already exist", coder.Code()))}codes[coder.Code()] = coder
}// ParseCoder parse any error into *withCode.
// nil error will return nil direct.
// None withStack error will be parsed as ErrUnknown.
func ParseCoder(err error) Coder {if err == nil {return nil}if v, ok := err.(*withCode); ok {if coder, ok := codes[v.code]; ok {return coder}}return unknownCoder
}// IsCode reports whether any error in err's chain contains the given error code.
func IsCode(err error, code int) bool {if v, ok := err.(*withCode); ok {if v.code == code {return true}if v.cause != nil {return IsCode(v.cause, code)}return false}return false
}func init() {codes[unknownCoder.Code()] = unknownCoder
}

错误码注册

在工程内部创建一个文件专门设置错误码,并实现init方法针对错误码和信息完成注册

const (// ErrUserNotFound - 404: User not found.ErrUserNotFound int = iota + 110001// ErrUserAlreadyExist - 400: User already exist.ErrUserAlreadyExist
)// iam-apiserver: secret errors.
const (// ErrEncrypt - 400: Secret reach the max count.ErrReachMaxCount int = iota + 110101//  ErrSecretNotFound - 404: Secret not found.ErrSecretNotFound
)// iam-apiserver: policy errors.
const (// ErrPolicyNotFound - 404: Policy not found.ErrPolicyNotFound int = iota + 110201
)func init() {register(ErrUserNotFound, 404, "User not found")register(ErrUserAlreadyExist, 400, "User already exist")register(ErrReachMaxCount, 400, "Secret reach the max count")register(ErrSecretNotFound, 404, "Secret not found")register(ErrPolicyNotFound, 404, "Policy not found")register(ErrSuccess, 200, "OK")register(ErrUnknown, 500, "Internal server error")register(ErrBind, 400, "Error occurred while binding the request body to the struct")register(ErrValidation, 400, "Validation failed")
}

错误码使用

func WriteResponse(c *gin.Context, err error, data interface{}) {if err == nil {c.JSON(http.statusOK, data)return}if coder := coder.ParseCoder(err); coder != coder.UnknownCoder {c.JSON(coder.HTTPStatus, Response{Code: coder.Code(),Message: coeder.String(),Data: data,	})return }c.JSON(http.StatusOk, err)
http://www.lryc.cn/news/408251.html

相关文章:

  • Python面试题:使用Matplotlib和Seaborn进行数据可视化
  • 模拟实现c++中的vector模版
  • uniapp安卓通过绝对路径获取文件
  • Known框架实战演练——进销存业务单据
  • 解决npm依赖树冲突的方法以及npm ERR! code ERESOLVE错误的解决方案
  • Spring Boot + Spring Batch + Quartz 整合定时批量任务
  • C++STL简介(二)
  • 嵌入式高频面试题100道及参考答案(3万字长文)
  • python爬虫-事件触发机制
  • LeetCode-day27-3106. 满足距离约束且字典序最小的字符串
  • C++中的static_cast函数
  • 从零开始学习网络安全渗透测试之基础入门篇——(二)Web架构前后端分离站Docker容器站OSS存储负载均衡CDN加速反向代理WAF防护
  • 2679. 矩阵中的和
  • Unity Playables:下一代动画与音频序列
  • matlab仿真 模拟调制(下)
  • RabbitMQ是什么?
  • 追问试面试系列:分布式id
  • 护网紧急情况应对指南:Linux 应急响应手册
  • WEB攻防-通用漏洞-SQL 读写注入-MYSQLMSSQLPostgreSQL
  • 【前端学习笔记】CSS基础一
  • Github遇到的问题解决方法总结(持续更新...)
  • 数字信封+数字签名工具类测试样例(Java实现)
  • The Schematic workflow failed. See above.
  • 操作系统面试知识点总结4
  • Lua实现面向对象以及类的继承
  • 机器学习课程学习周报五
  • vue3.0学习笔记(二)——生命周期与响应式数据(ref,reactive,toRef,toRefs函数)
  • C++——QT:保姆级教程,从下载到安装到用QT写出第一个程序
  • 掌握互联网路由选择协议:从基础入门到实战
  • [笔记]ONVIF服务端实现[进行中...]