golang为什么要统一错误处理


Posted in Golang onApril 03, 2022

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.共用错误处理

golang为什么要统一错误处理

 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统一错误处理内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Golang 相关文章推荐
Go语言中break label与goto label的区别
Apr 28 Golang
解决golang结构体tag编译错误的问题
May 02 Golang
Golang之sync.Pool使用详解
May 06 Golang
Goland使用Go Modules创建/管理项目的操作
May 06 Golang
go xorm框架的使用
May 22 Golang
go web 预防跨站脚本的实现方式
Jun 11 Golang
再次探讨go实现无限 buffer 的 channel方法
Jun 13 Golang
Go语言设计模式之结构型模式
Jun 22 Golang
Go中的条件语句Switch示例详解
Aug 23 Golang
Golang并发操作中常见的读写锁详析
Aug 30 Golang
Go语言特点及基本数据类型使用详解
Mar 21 Golang
简单聊聊Golang中defer预计算参数
Mar 25 #Golang
Go 中的空白标识符下划线
golang生成vcf通讯录格式文件详情
golang实现浏览器导出excel文件功能
Golang使用Panic与Recover进行错误捕获
Mar 22 #Golang
Go语言特点及基本数据类型使用详解
详解Golang如何优雅的终止一个服务
Mar 21 #Golang
You might like
一个简单的自动发送邮件系统(二)
2006/10/09 PHP
投票管理程序
2006/10/09 PHP
php数组函数序列之in_array() 查找数组值是否存在
2011/10/29 PHP
PHP性能优化准备篇图解PEAR安装
2011/12/05 PHP
php中禁止单个IP与ip段访问的代码小结
2012/07/04 PHP
基于PHP常用函数的用法详解
2013/05/10 PHP
基于php在各种web服务器的运行模式详解
2013/06/03 PHP
如何设置mysql允许外网访问
2013/06/04 PHP
thinkphp配置连接数据库技巧
2014/12/02 PHP
详解YII关联查询
2016/01/10 PHP
Jquery插件写法笔记整理
2012/09/06 Javascript
关于jQuery参考实例 1.0 jQuery的哲学
2013/04/07 Javascript
JS清除IE浏览器缓存的方法
2013/07/26 Javascript
JQuery中对Select的option项的添加、删除、取值
2013/08/25 Javascript
js 获取元素下面所有li的两种方法
2014/04/14 Javascript
js下将阿拉伯数字每三位一逗号分隔(如:15000000转化为15,000,000)
2014/06/02 Javascript
JS定义类的六种方式详解
2016/05/12 Javascript
解析vue data不可以使用箭头函数问题
2018/07/03 Javascript
学习使用ExpressJS 4.0中的新Router的用法
2018/11/06 Javascript
详解Js里的for…in和for…of的用法
2019/03/28 Javascript
js实现图片粘贴到网页
2019/12/06 Javascript
vue项目前端微信JSAPI与外部H5支付相关实现过程及常见问题
2020/04/14 Javascript
[01:26]DOTA2荣耀之路2:iG,China
2018/05/24 DOTA
解决Python中字符串和数字拼接报错的方法
2016/10/23 Python
Python动刷新抢12306火车票的代码(附源码)
2018/01/24 Python
Python 实现将数组/矩阵转换成Image类
2020/01/09 Python
Django权限设置及验证方式
2020/05/13 Python
Win10用vscode打开anaconda环境中的python出错问题的解决
2020/05/25 Python
Python读取pdf表格写入excel的方法
2021/01/22 Python
HomeAway英国:全球领先的度假租赁在线市场
2020/02/03 全球购物
社团文化节邀请函
2014/01/10 职场文书
工作失职检讨书范文
2014/01/16 职场文书
调解协议书
2014/04/16 职场文书
党的群众路线教育实践活动个人整改措施
2014/10/27 职场文书
大学开学感言
2015/08/01 职场文书
详解GaussDB for MySQL性能优化
2021/05/18 MySQL