教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 Go-统一定义API错误码

Go-统一定义API错误码

发布时间:2022-03-06   编辑:jiaochengji.com
教程集为您提供Go-统一定义API错误码等资源,欢迎您收藏本站,我们将为您提供最新的Go-统一定义API错误码资源
<h2>改之前</h2>

在使用 <code>gin</code> 开发接口的时候,返回接口数据是这样写的。

<pre><code class="lang-go hljs">type response struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } // always return http.StatusOK c.JSON(http.StatusOK, response{ Code: 20101, Msg: "用户手机号不合法", Data: nil, })</code></code></pre>

这种写法 <code>code</code>、<code>msg</code> 都是在哪需要返回在哪定义,没有进行统一管理。

<h2>改之后</h2><pre><code class="lang-go hljs">// 比如,返回“用户手机号不合法”错误 c.JSON(http.StatusOK, errno.ErrUserPhone.WithID(c.GetString("trace-id"))) // 正确返回 c.JSON(http.StatusOK, errno.OK.WithData(data).WithID(c.GetString("trace-id")))</code></code></pre>

<code>errno.ErrUserPhone</code>、<code>errno.OK</code> 表示自定义的错误码,下面会看到定义的地方。

<code>.WithID()</code> 设置当前请求的唯一ID,也可以理解为链路ID,忽略也可以。

<code>.WithData()</code> 设置成功时返回的数据。

下面分享下编写的 <code>errno</code> 包源码,非常简单,希望大家不要介意。

<h2>errno 包源码</h2><pre><code class="lang-go hljs">// errno/errno.go package errno import ( "encoding/json" ) var _ Error = (*err)(nil) type Error interface { // i 为了避免被其他包实现 i() // WithData 设置成功时返回的数据 WithData(data interface{}) Error // WithID 设置当前请求的唯一ID WithID(id string) Error // ToString 返回 JSON 格式的错误详情 ToString() string } type err struct { Code int `json:"code"` // 业务编码 Msg string `json:"msg"` // 错误描述 Data interface{} `json:"data"` // 成功时返回的数据 ID string `json:"id,omitempty"` // 当前请求的唯一ID,便于问题定位,忽略也可以 } func NewError(code int, msg string) Error { return &err{ Code: code, Msg: msg, Data: nil, } } func (e *err) i() {} func (e *err) WithData(data interface{}) Error { e.Data = data return e } func (e *err) WithID(id string) Error { e.ID = id return e } // ToString 返回 JSON 格式的错误详情 func (e *err) ToString() string { err := &struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` ID string `json:"id,omitempty"` }{ Code: e.Code, Msg: e.Msg, Data: e.Data, ID: e.ID, } raw, _ := json.Marshal(err) return string(raw) } </code></code></pre><pre><code class="lang-go hljs">// errno/code.go package errno var ( // OK OK = NewError(0, "OK") // 服务级错误码 ErrServer = NewError(10001, "服务异常,请联系管理员") ErrParam = NewError(10002, "参数有误") ErrSignParam = NewError(10003, "签名参数有误") // 模块级错误码 - 用户模块 ErrUserPhone = NewError(20101, "用户手机号不合法") ErrUserCaptcha = NewError(20102, "用户验证码有误") // ... )</code></code></pre><h2>错误码规则</h2><ul><li>错误码需在 <code>code.go</code> 文件中定义。</li><li>错误码需为 > 0 的数,反之表示正确。</li></ul><h4>错误码为 5 位数</h4><table><thead><tr><th align="left">1</th><th align="left">01</th><th align="left">01</th></tr></thead><tbody><tr><td align="left">服务级错误码</td><td align="left">模块级错误码</td><td align="left">具体错误码</td></tr></tbody></table><ul><li>服务级别错误码:1 位数进行表示,比如 1 为系统级错误;2 为普通错误,通常是由用户非法操作引起。</li><li>模块级错误码:2 位数进行表示,比如 01 为用户模块;02 为订单模块。</li><li>具体错误码:2 位数进行表示,比如 01 为手机号不合法;02 为验证码输入错误。</li></ul>

以上代码在 go-gin-api 项目中,地址:https://github.com/xinliangno...


到此这篇关于“Go-统一定义API错误码”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
gin定义统一处理错误
Go-统一定义API错误码
1.2Go语言开发环境搭建
Golang实践-error
go 语言学习历程
Go 语言中的错误处理机制
Go 开发关键技术指南 | 为什么你要选择 Go?(内含超全知识大图)
使用golang进行kong限流插件开发
golang错误处理机制(异常处理)
golang和python有什么区别?

[关闭]
~ ~