新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrame錯誤碼特性-錯誤碼實(shí)現(xiàn)
當(dāng)業(yè)務(wù)需要更復(fù)雜的錯誤碼定義時,我們可以自定義實(shí)現(xiàn)業(yè)務(wù)自己的錯誤碼,只需要實(shí)現(xiàn)?gcode.Code?相關(guān)的接口即可。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、牟平網(wǎng)站維護(hù)、網(wǎng)站推廣。
我們來看個例子。
自定義錯誤碼
定義結(jié)構(gòu)體并實(shí)現(xiàn)?gcode.code?接口定義的方法
type BizCode struct {
code int
message string
detail BizCodeDetail
}
type BizCodeDetail struct {
Code string
HttpCode int
}
func (c BizCode) BizDetail() BizCodeDetail {
return c.detail
}
func (c BizCode) Code() int {
return c.code
}
func (c BizCode) Message() string {
return c.message
}
func (c BizCode) Detail() interface{} {
return c.detail
}
func New(httpCode int, code string, message string) gcode.Code {
return BizCode{
code: 0,
message: message,
detail: BizCodeDetail{
Code: code,
HttpCode: httpCode,
},
}
}定義業(yè)務(wù)錯誤碼
var (
CodeNil = New(200, "OK", "")
CodeNotFound = New(404, "Not Found", "Resource does not exist")
CodeInternal = New(500, "Internal Error", "An error occurred internally")
// ...
)
使用到中間件
func ResponseHandler(r *ghttp.Request) {
r.Middleware.Next()
// There's custom buffer content, it then exits current handler.
if r.Response.BufferLength() > 0 {
return
}
res, err := r.GetHandlerResponse()
code := gerror.Code(err)
if code == gcode.CodeNil && err != nil {
code = CodeInternal
} else {
code = CodeNil
}
if bizCode, ok := code.(BizCode); ok {
r.Response.WriteStatus(bizCode.BizDetail().HttpCode)
}
_ = r.Response.WriteJson(g.Map{
`code`: gcode.CodeOK.Code(),
`message`: gcode.CodeOK.Message(),
`data`: res,
})
} 網(wǎng)頁名稱:創(chuàng)新互聯(lián)GoFrame教程:GoFrame錯誤碼特性-錯誤碼實(shí)現(xiàn)
分享URL:http://www.dlmjj.cn/article/coshsos.html


咨詢
建站咨詢
