errorhelper

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: MIT Imports: 6 Imported by: 0

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

func CallerError(err error) error

CallerError prepends the provided error with caller function/method name. If 'err' is nil, nil is returned.

Example
errf := CallerError(error1)
fmt.Println(errf.Error())
ermth := errMethod{}
errm1 := ermth.Error1()
fmt.Println(errm1.Error())
errm2 := ermth.Error2()
fmt.Println(errm2.Error())
Output:

ExampleCallerError: error1
errMethod.Error1: error1
(*errMethod).Error2: error1

func Must

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

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

func Must0

func Must0(err error)

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

func Must2

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 PanicToError

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 the Unwrap method on 'err', if err's type contains an Unwrap method returning []error. Otherwise or if the Unwrap method returns 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