nvidia-container-toolkit/vendor/github.com/tsaikd/KDGoLib/errutil
Evan Lezar 6e1436cefb Update go vendoring
Signed-off-by: Evan Lezar <elezar@nvidia.com>
2021-09-07 13:13:03 +02:00
..
ConsoleFormatter.go Update go vendoring 2021-09-07 13:13:03 +02:00
ErrorFactory.go Update go vendoring 2021-09-07 13:13:03 +02:00
ErrorFormatter.go Update go vendoring 2021-09-07 13:13:03 +02:00
ErrorJSON.go Update go vendoring 2021-09-07 13:13:03 +02:00
ErrorObject.go Update go vendoring 2021-09-07 13:13:03 +02:00
ErrorObjectUtil.go Update go vendoring 2021-09-07 13:13:03 +02:00
JSONFormatter.go Update go vendoring 2021-09-07 13:13:03 +02:00
logger.go Update go vendoring 2021-09-07 13:13:03 +02:00
README.md Update go vendoring 2021-09-07 13:13:03 +02:00
RuntimeCaller.go Update go vendoring 2021-09-07 13:13:03 +02:00
sort.go Update go vendoring 2021-09-07 13:13:03 +02:00
trace.go Update go vendoring 2021-09-07 13:13:03 +02:00
util.go Update go vendoring 2021-09-07 13:13:03 +02:00

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

Cover

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")
}