当前位置:网站首页>Common methods of goframe error handling & use of error codes
Common methods of goframe error handling & use of error codes
2022-07-19 05:25:00 【Hardware tea talk】
Preface abstract
This article will introduce :GoFrame Common methods of error handling & Use of error codes . How to customize error objects 、 How to ignore some stack information 、 How to customize the return of error codes 、 How to get error Object .
Error creating
New/Newf
For creating a custom error message error object , And contains stack information .
New(text string) errorNewf(format string, args ...interface{}) errorWrap/Wrapf
For wrapping other errors error object , Construct a multi-level error message , Contains stack information .
func Wrap(err error, text string) errorfunc Wrapf(err error, format string, args ...interface{}) errorNewSkip/NewSkipf
For creating a custom error message error object , And ignore some stack information ( Ignore up according to the current calling method position ). Advanced features , The average developer rarely uses .
func NewSkip(skip int, text string) error func NewSkipf(skip int, format string, args ...interface{}) errorError code use
Overview of error code related methods
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
Same function New/Newf Method , For creating a custom error message error object , And contains stack information , And add the input of error code object .
NewCode(code gcode.Code, text ...string) errorNewCodef(code gcode.Code, format string, args ...interface{}) errorSample code
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
Same function Wrap/Wrapf Method , For wrapping other errors error object , Construct a multi-level error message , Contains stack information , And add the input of error code parameters .
WrapCode(code gcode.Code, err error, text ...string) errorWrapCodef(code gcode.Code, err error, format string, args ...interface{}) errorSample code
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
Same function NewSkip/NewSkipf, For creating a custom error message error object , And ignore some stack information ( Ignore up according to the current calling method position ), And add error parameter input .
func NewCodeSkip(code, skip int, text string) errorfunc NewCodeSkipf(code, skip int, format string, args ...interface{}) error obtain error Error code interface in
func Code(err error) gcode.Code When given error When the parameter does not contain error code information , This method returns a predefined error code gcode.CodeNil
边栏推荐
- A comprehensive performance optimization, from 5 seconds to 1 second
- Case summary of rotation chart moving speed (constant speed, slow motion)
- Cesium 绑定鼠标事件和移除鼠标事件
- 聊聊并发编程的12种业务场景
- Excel template export of easypoi
- es6新增-函数部分
- Web3js development technology
- Round robin schedule problem
- Three methods for cesium to obtain the longitude and latitude at the mouse click
- JS native object plus attributes
猜你喜欢

Solutions for vscode terminal failure

线上软件测试培训机构柠檬班与iTest.AI平台达成战略合作

Wechat applet cloud development and use method-1
![[AI] action recognition using simple neural network -- Based on coco key points](/img/67/cd6be6e070fb5d4d44ee043ebd7fac.png)
[AI] action recognition using simple neural network -- Based on coco key points

Implementation of synchronization interface of 6 libcurl based on libco

新手学习渗透测试的入门指南

2022年春招最新消息:IT互联网行业平均薪资18500元

Web3js development technology

面试官:大量请求 Redis 不存在的数据,从而影响数据库,该如何解决?

Distributed storage fastdfs
随机推荐
[ES6] use multiple functions such as adding data, filtering and transmitting to the page to realize cases
微信小程序 学习笔记
Web3js development technology
Two or three things to know about operation and maintenance safety
2020-10-22
从20s优化到500ms,我用了这三招
Redis source code analysis skip table implementation
Distributed storage fastdfs
mysql的缓存方案问题解决
Easypoi excel simple export
线上软件测试培训机构柠檬班与iTest.AI平台达成战略合作
Pat class B 1002: write this number
Redis source code analysis dynamic string implementation (SDS)
Shell script configures root to login to other hosts without secret
ArcMap 创建常量栅格并镶嵌至新栅格
Distributed registry etcd
【全网首发】JVM性能问题的自动分析
Usage and examples of vlookup function
指针进阶简单总结
ES6 real case deconstruction (multidimensional array object) new case: