当前位置:网站首页>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
边栏推荐
- Flex flexible layout
- Solutions for vscode terminal failure
- MySQL cache solution problem solving
- Talk about 12 business scenarios of concurrent programming
- 第一个智能合约程序Faucet.sol
- 单臂路由配置
- Web3js development technology
- Cesium BIND Mouse Events and remove Mouse Events
- Multifunctional (Implementation) encapsulation function
- Markdown notes and related shortcut keys of typora
猜你喜欢

Mapbox loads local offline terrain

单臂路由配置

redis 源码分析 跳表实现

A comprehensive performance optimization, from 5 seconds to 1 second

父组件加scoped有时也会影响子组件

mysql的事务

The latest news of spring recruitment in 2022: the average salary of it Internet industry is 18500 yuan

柠檬班软件测试培训可靠吗 这个从培训班逆袭成功的案例告诉你

云服务器部署WEB项目

分布式注册中心-etcd
随机推荐
es6新增-函数部分
UML(用例图,类图,对象图,包图)
Wechat applet learning notes
B域,M域,O域具体是指什么
How to deal with the mismatch between subtitle files and video files
redis源码分析 2 迭代器
从20s优化到500ms,我用了这三招
Wechat applet wx Setclipboarddata copy text
Implementation of synchronization interface of 6 libcurl based on libco
Multifunctional (Implementation) encapsulation function
微信小程序 学习笔记
MySQL cache strategy and solution
SQL injection
Case summary of rotation chart moving speed (constant speed, slow motion)
ES6 real case deconstruction (multidimensional array object) new case:
Excel calculates the remaining days of the month
Redis source code analysis dynamic string implementation (SDS)
Addition and removal of cesium geojson data
Solve the problem of inconsistent prediction effect between text detection training model and information model based on paddleocr
Nacos配置管理