golang為什么要統(tǒng)一錯(cuò)誤處理
1.為什么要統(tǒng)一錯(cuò)誤處理
統(tǒng)一錯(cuò)誤處理的目的是為了前端開發(fā)接收到后端的statuscode,之后便于前端邏輯上開發(fā),以及開發(fā)。200代表成功,500失敗,400代表找不到、禁止等異常
2.后端封裝統(tǒng)一接口
/**
* 統(tǒng)一處理
* 錯(cuò)誤碼,response,返回內(nèi)容,error
*/
func HandleResult(statusCode int, response *restful.Response, value interface{}, err error) {
if err != nil {
HandleAllStatus(parseValue(err, statusCode), response, err)
return
}
if statusCode == http.StatusOK {
HandleSuccess(response, value)
return
}
// 解析其他錯(cuò)誤
HandleAllStatus(parseValue(value, statusCode), response, value)
}3.核心函數(shù)
func handle(statusCode int, response *restful.Response, req *restful.Request, err error) {
_, fn, line, _ := runtime.Caller(2)
klog.Errorf("%s:%d %v", fn, line, err)
http.Error(response, sanitizer.Replace(err.Error()), statusCode)
}打印錯(cuò)誤日志,哪個(gè)文件函數(shù)多少行,以及錯(cuò)誤原因
4.常見錯(cuò)誤處理
func HandleInternalError(response *restful.Response, req *restful.Request, err error) {
handle(http.StatusInternalServerError, response, req, err)
}
// HandleBadRequest writes http.StatusBadRequest and log error
func HandleBadRequest(response *restful.Response, req *restful.Request, err error) {
handle(http.StatusBadRequest, response, req, err)
}
func HandleNotFound(response *restful.Response, req *restful.Request, err error) {
handle(http.StatusNotFound, response, req, err)
}
func HandleForbidden(response *restful.Response, req *restful.Request, err error) {
handle(http.StatusForbidden, response, req, err)
}
func HandleUnauthorized(response *restful.Response, req *restful.Request, err error) {
handle(http.StatusUnauthorized, response, req, err)
}
func HandleTooManyRequests(response *restful.Response, req *restful.Request, err error) {
handle(http.StatusTooManyRequests, response, req, err)
}
func HandleConflict(response *restful.Response, req *restful.Request, err error) {
handle(http.StatusConflict, response, req, err)
}5.共用錯(cuò)誤處理

?func HandleAllStatus(statusCode int, response *restful.Response, value interface{}) {
if value == nil {
response.WriteHeader(statusCode)
return
}
switch ee := value.(type) {
case error:
handle(statusCode, response, nil, ee)
case string:
response.WriteHeader(statusCode)
response.WriteAsJson(value)
return
default:
response.WriteHeader(statusCode)
//處理是否為byte數(shù)組
b, ok := value.([]byte)
if ok {
response.Write(b)
} else {
response.WriteEntity(value)
}
}
}6.解析錯(cuò)誤原因
func parseValue(value interface{}, statusCode int) int {
if value == nil {
return statusCode
}
obj := make(map[string]interface{})
switch tValue := value.(type) {
case error:
json.Unmarshal([]byte(tValue.Error()), &obj)
default:
b, ok := value.([]byte)
if ok {
json.Unmarshal(b, &obj)
} else {
j, err := json.Marshal(value)
if err == nil {
json.Unmarshal(j, &obj)
}
}
}
if s, o := obj["code"]; o {
switch rs := s.(type) {
case string:
rs1, err := strconv.Atoi(rs)
if err != nil {
return rs1
}
case int:
return rs
case float64:
return int(rs)
default:
return statusCode
}
}
return statusCode
}到此這篇關(guān)于golang為什么要統(tǒng)一錯(cuò)誤處理的文章就介紹到這了,更多相關(guān)golang統(tǒng)一錯(cuò)誤處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決golang編譯提示dial tcp 172.217.160.113:443: connectex: A connection attempt failed(推薦)
- Golang?Gin解析JSON請(qǐng)求數(shù)據(jù)避免出現(xiàn)EOF錯(cuò)誤
- golang?gorm錯(cuò)誤處理事務(wù)以及日志用法示例
- golang常用庫之pkg/errors包第三方錯(cuò)誤處理包案例詳解
- Golang 錯(cuò)誤捕獲Panic與Recover的使用
- 解決golang結(jié)構(gòu)體tag編譯錯(cuò)誤的問題
- golang提示dial?tcp?172?.217.163.49:443:?connectex:?A?connection?attempt?failed解決
相關(guān)文章
golang中值類型/指針類型的變量區(qū)別總結(jié)
golang的值類型和指針類型receiver一直是大家比較混淆的地方,下面這篇文章主要給大家總結(jié)介紹了關(guān)于golang中值類型/指針類型的變量區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12
go實(shí)現(xiàn)for range迭代時(shí)修改值的操作
這篇文章主要介紹了go實(shí)現(xiàn)for range迭代時(shí)修改值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Golang協(xié)程池gopool設(shè)計(jì)與實(shí)現(xiàn)
本文主要介紹了Golang協(xié)程池gopool設(shè)計(jì)與實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
goland中導(dǎo)包報(bào)紅和go mod問題
這篇文章主要介紹了goland中導(dǎo)包報(bào)紅和go mod問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Golang測(cè)試func?TestXX(t?*testing.T)的使用詳解
一般Golang中的測(cè)試代碼都以xxx_test.go的樣式,在命名測(cè)試函數(shù)的時(shí)候以Testxx開頭,下面給大家介紹Golang測(cè)試func?TestXX(t?*testing.T)的使用,感興趣的朋友跟隨小編一起看看吧2024-08-08
用gin開發(fā)的golang項(xiàng)目三種開發(fā)模式方式
這篇文章主要介紹了用gin開發(fā)的golang項(xiàng)目三種開發(fā)模式方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

