首頁 > 軟體

golang為什麼要統一錯誤處理

2022-04-02 19:00:45

1.為什麼要統一錯誤處理

統一錯誤處理的目的是為了前端開發接收到後端的statuscode,之後便於前端邏輯上開發,以及開發。200代表成功,500失敗,400代表找不到、禁止等異常

2.後端封裝統一介面

/**
* 統一處理
* 錯誤碼,response,返回內容,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
}
// 解析其他錯誤
HandleAllStatus(parseValue(value, statusCode), response, value)
}

3.核心函數

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)
}

列印錯誤紀錄檔,哪個檔案函數多少行,以及錯誤原因

4.常見錯誤處理

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.共用錯誤處理

 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陣列
b, ok := value.([]byte)
if ok {
response.Write(b)
} else {
response.WriteEntity(value)
}
}
}

6.解析錯誤原因

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
}

到此這篇關於golang為什麼要統一錯誤處理的文章就介紹到這了,更多相關golang統一錯誤處理內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com