errorhelper

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2025 License: MIT Imports: 6 Imported by: 12

README

errorhelper

Go Reference

Helpers for Go's error.

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

func CallerError(err error, params ...any) error

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 Must added in v0.2.0

func Must[R any](r R, err error) R

Must returns 'r' if 'err' is nil. Otherwise, it panics with 'err'.

func Must0 added in v0.3.0

func Must0(err error)

Must0 panics with 'err', if 'err' is not nil.

func Must2 added in v0.5.0

func Must2[R1, R2 any](r1 R1, r2 R2, err error) (R1, R2)

Must2 returns ('r1', 'r2') if 'err' is nil. Otherwise, it panics with 'err'.

func PackageCallerError added in v0.9.0

func PackageCallerError(err error, params ...any) error

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

func PanicToError(panicArg any, err *error)

PanicToError converts panic to error in the following way:

PanicToError must be called from a defer statement:

func Example() (err error) {
  defer func() {
    PanicToError(recover(), &err)
  }()
  return nil
}

(See tests.)

func UnwrapErrors

func UnwrapErrors(err error) []error

UnwrapErrors returns the result of calling Unwrap method on 'err', in case if err's type contains an Unwrap method that returns []error. Otherwise or if the Unwrap method returns single error, UnwrapErrors returns nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL