目录1.为什么要统一错误处理2.后端封装统一接口3.核心函数4.常见错误处理5.共用错误处理6.解析错误原因1.为什么要统一错误处理 统一错误处理的目的是为了前端开发接收到后端的st
统一错误处理的目的是为了前端开发接收到后端的statuscode
,之后便于前端逻辑上开发,以及开发。200代表成功,500失败,400代表找不到、禁止等异常
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)
}
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)
}
打印错误日志,哪个文件函数多少行,以及错误原因
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)
}
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)
}
}
}
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统一错误处理内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: golang为什么要统一错误处理
本文链接: https://lsjlt.com/news/144934.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-05
2024-04-05
2024-04-05
2024-04-04
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0