当前位置:网站首页>GoFrame 错误处理的常用方法&错误码的使用
GoFrame 错误处理的常用方法&错误码的使用
2022-07-17 05:08:00 【硬件茶谈】
前言摘要
这篇文章将为大家介绍:GoFrame 错误处理的常用方法&错误码的使用。如何自定义错误对象、如何忽略部分堆栈信息、如何自定义错误码的返回、如何获取error对象中的错误码。
错误创建
New/Newf
用于创建一个自定义错误信息的error对象,并包含堆栈信息。
New(text string) errorNewf(format string, args ...interface{}) errorWrap/Wrapf
用于包裹其他错误error对象,构造成多级的错误信息,包含堆栈信息。
func Wrap(err error, text string) errorfunc Wrapf(err error, format string, args ...interface{}) errorNewSkip/NewSkipf
用于创建一个自定义错误信息的error对象,并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。高级功能,一般开发者很少用得到。
func NewSkip(skip int, text string) error func NewSkipf(skip int, format string, args ...interface{}) error错误码使用
错误码相关方法概览
func NewCode(code int, text string) errorfunc NewCodef(code int, format string, args ...interface{}) errorfunc NewCodeSkip(code, skip int, text string) errorfunc NewCodeSkipf(code, skip int, format string, args ...interface{}) errorfunc WrapCode(code int, err error, text string) errorfunc WrapCodef(code int, err error, format string, args ...interface{}) errorNewCode/NewCodef
功能同New/Newf方法,用于创建一个自定义错误信息的error对象,并包含堆栈信息,并增加错误码对象的输入。
NewCode(code gcode.Code, text ...string) errorNewCodef(code gcode.Code, format string, args ...interface{}) error示例代码
func ExampleNewCode() { err := gerror.NewCode(gcode.New(101, "", nil), "My Error") fmt.Println(err.Error()) // My Error fmt.Println(gerror.Code(err)) //101}func ExampleNewCodef() { err := gerror.NewCodef(gcode.New(101, "", nil), "It's %s", "My Error") fmt.Println(err.Error()) //It's My Error fmt.Println(gerror.Code(err).Code()) //101}WrapCode/WrapCodef
功能同Wrap/Wrapf方法,用于包裹其他错误error对象,构造成多级的错误信息,包含堆栈信息,并增加错误码参数的输入。
WrapCode(code gcode.Code, err error, text ...string) errorWrapCodef(code gcode.Code, err error, format string, args ...interface{}) error示例代码
func ExampleWrapCode() { err1 := errors.New("permission denied") err2 := gerror.WrapCode(gcode.New(403, "", nil), err1, "Custom Error") fmt.Println(err2.Error()) // Custom Error: permission denied fmt.Println(gerror.Code(err2).Code()) // 403}func ExampleWrapCodef() { err1 := errors.New("permission denied") err2 := gerror.WrapCodef(gcode.New(403, "", nil), err1, "It's %s", "Custom Error") fmt.Println(err2.Error()) // It's Custom Error: permission denied fmt.Println(gerror.Code(err2).Code()) // 403}NewCodeSkip/NewCodeSkipf
功能同NewSkip/NewSkipf,用于创建一个自定义错误信息的error对象,并且忽略部分堆栈信息(按照当前调用方法位置往上忽略),并增加错误参数输入。
func NewCodeSkip(code, skip int, text string) errorfunc NewCodeSkipf(code, skip int, format string, args ...interface{}) error获取error中的错误码接口
func Code(err error) gcode.Code当给定的error参数不带有错误码信息时,该方法返回预定义的错误码gcode.CodeNil
边栏推荐
- Excel calculates the remaining days of the month
- 网络命令:网卡信息,netstat,arp
- Easypoi之excel多sheet导入
- 2020-10-22
- Wechat applet obtains the week, morning, noon and evening of month, year and day
- 运维安全要了解的二三事
- Addition and removal of cesium geojson data
- uniapp中使用ucharts图表,饼状图,柱状图,折线图
- PAT乙级1017: A除以B
- redis 源码分析 动态字符串实现(sds)
猜你喜欢

Teach you to reproduce log4j2 nuclear weapon level vulnerability hand in hand

ThreadLocal thread safety example and its principle

uniapp 表单(input、radio、picker)提交获取参数值

路由器loopback口实验

云服务器部署WEB项目

Shell script configures root to login to other hosts without secret

Solve the problem of inconsistent prediction effect between text detection training model and information model based on paddleocr

vscode终端无法使用解决的办法

【全网首发】JVM性能问题的自动分析

Cesium 綁定鼠標事件和移除鼠標事件
随机推荐
Pat class B 1017: a divided by B
Wechat applet wx Setclipboarddata copy text
ArcGIS 点云(xyz)数据转DEM
Internship project 2 - Homepage configuration - my data module
Use (offset, page) in JS to achieve login effect
MySQL安装配置教程(超级详细)
Rk356x u-boot Institute (command section) 3.4 usage of MEM memory related commands
轮播图移动速度(匀速,缓动)案例归总
redis 源码分析 跳表实现
热更新及其原理
循环赛制日程表问题
The principle and local storage of the throttle valve of the rotation chart are summarized
ArcMap creates a constant grid and tessellates it into a new grid
性能瓶颈查找-火焰图分析
Router loopback port experiment
【全网首发】JVM性能问题的自动分析
Excel计算本月剩余天数
新手学习渗透测试的入门指南
【Es6】forEach,for... in ,for... Of column, which allows you to quickly distinguish the usage and differences of various for statements through project cases (full version). There are detailed notes ins
指针进阶简单总结