在使用 gin
開發(fā)接口的時候,返回接口數(shù)據(jù)是這樣寫的。
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
、msg
都是在哪需要返回在哪定義,沒有進行統(tǒng)一管理。
// 比如,返回“用戶手機號不合法”錯誤
c.JSON(http.StatusOK, errno.ErrUserPhone.WithID(c.GetString("trace-id")))
// 正確返回
c.JSON(http.StatusOK, errno.OK.WithData(data).WithID(c.GetString("trace-id")))
errno.ErrUserPhone
、errno.OK
表示自定義的錯誤碼,下面會看到定義的地方。
.WithID()
設(shè)置當前請求的唯一ID,也可以理解為鏈路ID,忽略也可以。
.WithData()
設(shè)置成功時返回的數(shù)據(jù)。
下面分享下編寫的 errno
包源碼,非常簡單,希望大家不要介意。
// errno/errno.go
package errno
import (
"encoding/json"
)
var _ Error = (*err)(nil)
type Error interface {
// i 為了避免被其他包實現(xiàn)
i()
// WithData 設(shè)置成功時返回的數(shù)據(jù)
WithData(data interface{}) Error
// WithID 設(shè)置當前請求的唯一ID
WithID(id string) Error
// ToString 返回 JSON 格式的錯誤詳情
ToString() string
}
type err struct {
Code int `json:"code"` // 業(yè)務編碼
Msg string `json:"msg"` // 錯誤描述
Data interface{} `json:"data"` // 成功時返回的數(shù)據(jù)
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)
}
// errno/code.go
package errno
var (
// OK
OK = NewError(0, "OK")
// 服務級錯誤碼
ErrServer = NewError(10001, "服務異常,請聯(lián)系管理員")
ErrParam = NewError(10002, "參數(shù)有誤")
ErrSignParam = NewError(10003, "簽名參數(shù)有誤")
// 模塊級錯誤碼 - 用戶模塊
ErrUserPhone = NewError(20101, "用戶手機號不合法")
ErrUserCaptcha = NewError(20102, "用戶驗證碼有誤")
// ...
)
code.go
文件中定義。1 | 01 | 01 |
---|---|---|
服務級錯誤碼 | 模塊級錯誤碼 | 具體錯誤碼 |