Documentation
¶
Overview ¶
Package errorhelper contains error-related helpers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallerError ¶ added in v0.6.0
CallerError prepends the provided error error's Error() with caller function/method name and the provided parameters. If there are no parameters, colon is used. If 'err' is nil, nil is returned.
Example ¶
errf1 := CallerError(error1) fmt.Println(errf1.Error()) errf2 := CallerError(error1, " ") fmt.Println(errf2.Error()) ermth := errMethod{} errm1 := ermth.Error11() fmt.Println(errm1.Error()) errm2 := ermth.Error12() fmt.Println(errm2.Error())
Output: ExampleCallerError:error1 ExampleCallerError error1 errMethod.Error11: error1 (*errMethod).Error12--error1
func Must2 ¶ added in v0.5.0
Must2 returns ('r1', 'r2') if 'err' is nil. Otherwise, it panics with 'err'.
func PackageCallerError ¶ added in v0.9.0
PackageCallerError prepends the provided error's Error() with caller function/method name prepended with package name and the provided parameters. If there are no parameters, colon is used. If 'err' is nil, nil is returned.
Example ¶
errf1 := PackageCallerError(error1) fmt.Println(errf1.Error()) errf2 := PackageCallerError(error1, " ") fmt.Println(errf2.Error()) ermth := errMethod{} errm1 := ermth.Error21() fmt.Println(errm1.Error()) errm2 := ermth.Error22() fmt.Println(errm2.Error())
Output: errorhelper.ExamplePackageCallerError:error1 errorhelper.ExamplePackageCallerError error1 errorhelper.errMethod.Error21: error1 errorhelper.(*errMethod).Error22--error1
func PanicToError ¶ added in v0.4.0
PanicToError converts panic to error in the following way:
- if surrounding function panics with an error, this error is returned;
- if surrounding function panics with a string, the error wrapping this string is returned;
- if surrounding function panics with a fmt.Stringer, the error wrapping fmt.Stringer.String is returned;
- if surrounding function panics with an encoding.TextMarshaler, the error wrapping encoding.TextMarshaler.MarshalText is returned;
- otherwise the panic is reraised.
PanicToError must be called from a defer statement:
func Example() (err error) { defer func() { PanicToError(recover(), &err) }() return nil }
(See tests.)
Types ¶
This section is empty.