mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-22 16:29:18 +00:00
6e1436cefb
Signed-off-by: Evan Lezar <elezar@nvidia.com> |
||
---|---|---|
.. | ||
ConsoleFormatter.go | ||
ErrorFactory.go | ||
ErrorFormatter.go | ||
ErrorJSON.go | ||
ErrorObject.go | ||
ErrorObjectUtil.go | ||
JSONFormatter.go | ||
logger.go | ||
README.md | ||
RuntimeCaller.go | ||
sort.go | ||
trace.go | ||
util.go |
errutil
An error handling helper, providing more APIs than built-in package (errors, fmt), and compatible with go error interface
Why use errutil instead of built-in errors and fmt
Usage
- Import package from master branch
import "github.com/tsaikd/KDGoLib/errutil"
- Declare error factory
var ErrorOccurWithReason = errutil.NewFactory("An error occur, reason: %q")
- Return error with factory
func doSomething() (err error) {
// do something
// return error with factory,
// first argument is parent error,
// the others are used for factory
return ErrorOccurWithReason.New(nil, "some reason here")
}
- Handle errors
func errorHandlingForOneCase() {
if err := doSomething(); err != nil {
if ErrorOccurWithReason.In(err) {
// handling expected error
return
}
// handling unexpected error
return
}
}
func errorHandlingForMultipleCases() {
if err := doSomething(); err != nil {
switch errutil.FactoryOf(err) {
case ErrorOccurWithReason:
// handling expected error
return
default:
// handling unexpected error
return
}
}
}
Optional usage
- Import from v1 branch
import "gopkg.in/tsaikd/KDGoLib.v1/errutil"
- Use like built-in errors package
- bad case because all errors should be exported for catching by other package
func doSomething() (err error) {
// do something
// return error with factory
return errutil.New("An error occur")
}