当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
vlookup函数的使用方法及实例
Two JS methods of rolling wheel loading and modal box dragging
cookie是否有效时间限定?如何设置cookie?手把手教你设置
Easypoi之excel简单导出
Flex弹性布局
字幕文件与视频文件对不上的处理方式
Use echars to realize water drop, ring, segmentation, stacking, organization chart, map outline and other charts
2.6.2 内存泄漏
Two or three things to know about operation and maintenance safety
redis源码分析 2 迭代器
Excel calculates the remaining days of the month
Class object automatic injection attribute operation tool
Single arm routing configuration
2020-10-22
Router loopback port experiment
uniapp中使用ucharts图表,饼状图,柱状图,折线图
The first smart contract program faucet sol
实习项目2-主页配置-我的数据模块
ThreadLocal thread safety example and its principle
交换机用户模式、特权模式、全局模式、端口模式








