Preface
This article will introduce to you: Common methods of GoFrame error handling and the use of error codes. How to customize the error object, how to ignore part of the stack information, how to customize the return of the error code, and how to obtain the error code in the error object.
Error creation
New/Newf
Used to create an error object with custom error information and contains stack information.
New(text string) error Newf(format string, args ...interface{}) error
Wrap/Wrapf
Used to wrap other error objects, construct multiple levels of error information, including stack information.
func Wrap(err error, text string) error func Wrapf(err error, format string, args ...interface{}) error
NewSkip/NewSkipf
Used to create an error object with custom error information and ignore part of the stack information (ignored up according to the current call method position). Advanced features are rarely used by developers.
func NewSkip(skip int, text string) error func NewSkipf(skip int, format string, args ...interface{}) error
Error code usage
Overview of error code related methods
func NewCode(code int, text string) error func NewCodef(code int, format string, args ...interface{}) error func NewCodeSkip(code, skip int, text string) error func NewCodeSkipf(code, skip int, format string, args ...interface{}) error func WrapCode(code int, err error, text string) error func WrapCodef(code int, err error, format string, args ...interface{}) error
NewCode/NewCodef
The function is the same as the New/Newf method, which is used to create an error object with custom error information, and contains stack information, and add input to the error code object.
NewCode(code , text ...string) error NewCodef(code , format string, args ...interface{}) error
Sample code
func ExampleNewCode() { err := ((101, "", nil), "My Error") (()) // My Error ((err)) //101 } func ExampleNewCodef() { err := ((101, "", nil), "It's %s", "My Error") (()) //It's My Error ((err).Code()) //101 }
WrapCode/WrapCodef
The function is the same as the Wrap/Wrapf method, which is used to wrap other error objects, construct multiple levels of error information, include stack information, and add input of error code parameters.
WrapCode(code , err error, text ...string) error WrapCodef(code , err error, format string, args ...interface{}) error
Sample code
func ExampleWrapCode() { err1 := ("permission denied") err2 := ((403, "", nil), err1, "Custom Error") (()) // Custom Error: permission denied ((err2).Code()) // 403 } func ExampleWrapCodef() { err1 := ("permission denied") err2 := ((403, "", nil), err1, "It's %s", "Custom Error") (()) // It's Custom Error: permission denied ((err2).Code()) // 403 }
NewCodeSkip/NewCodeSkipf
The function is the same as NewSkip/NewSkipf, which is used to create an error object with custom error information, and ignore part of the stack information (ignoring up according to the current call method position), and add error parameter input.
func NewCodeSkip(code, skip int, text string) error func NewCodeSkipf(code, skip int, format string, args ...interface{}) error
Get the error code interface in error
func Code(err error)
When the given error parameter does not contain error code information, the method returns the predefined error code
Summarize
Through this article, we learned how to customize the error object, how to ignore part of the stack information, how to customize the return of the error code, and how to obtain the error code in the error object.
The above is the detailed content of common methods and examples of GoFrame error handling. For more information about GoFrame error handling error codes, please pay attention to my other related articles!