exception

package
v2.0.0-...-98cc131 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package exception provides a simple exception mechanism to reduce boilerplate for trivial error forwarding in a function. Example:

func f0() error {
  return nil
}

func f1() (int, error) {
  return 1, nil
}

func MyFunc() (err error) {
  defer PropagateException(&err)

  Mustf(f0(),"f0 failed")
  i:=Must1f(R1(f1()), "f1 failed")
  fmt.Printf("got %d\n", i)
  return nil
}

Index

Constants

This section is empty.

Variables

View Source
var All = func(_ error) bool {
	return true
}
View Source
var None = func(_ error) bool {
	return false
}

Functions

func Exception

func Exception(r interface{}) error

Exception provie the error object from an exception object.

func ForwardExceptionf

func ForwardExceptionf(msg string, args ...interface{})

ForwardExceptionf add an error context to a forwarded exception. Usage: defer ForwardExceptionf("error context").

func Must

func Must(err error)

Must converts an error (e.g. provided by a nested function call) into an exception. It provides no regular result.

func Must1

func Must1[A any](r1 A, err error) A

Must1 converts an error into an exception. It provides one regular result.

Usage: Must1(ErrorFunctionWithOneRegularResult()).

func Must1f

func Must1f[A any](r result1[A], msg string, args ...interface{}) A

Must1f converts an error into an exception. It provides one regular result, which has to be provided by method R1. Optionally an error context can be given.

Usage: Must1f(R1(ErrorFunctionWithOneRegularResult()), "context").

The intermediate function R1 is required , because GO does not allow to compose arguments provided by a function with multiple return values with additional arguments.

func Must2

func Must2[A, B any](r1 A, r2 B, err error) (A, B)

Must2 converts an error into an exception. It provides two regular results.

func Must2f

func Must2f[A, B any](r result2[A, B], msg string, args ...interface{}) (A, B)

Must2f like Must1f, but for two regular results, which has to be provided by method R2. The error is wrapped by the given error context.

func Must3

func Must3[A, B, C any](r1 A, r2 B, r3 C, err error) (A, B, C)

Must3 converts an error into an exception. It provides three regular results.

func Must3f

func Must3f[A, B, C any](r result3[A, B, C], msg string, args ...interface{}) (A, B, C)

Must3f like Must1f, but for three regular results, which has to be provided by method R3.

func Must4

func Must4[A, B, C, D any](r1 A, r2 B, r3 C, r4 D, err error) (A, B, C, D)

Must4 converts an error into an exception. It provides four regular results.

func Must4f

func Must4f[A, B, C, D any](r result4[A, B, C, D], msg string, args ...interface{}) (A, B, C, D)

Must4f like Must1f, but for four regular results, which has to be provided by method R4.

func Mustf

func Mustf(err error, msg string, args ...interface{})

Must converts an error (e.g. provided by a nested function call) into an exception. It provides no regular result. The given error context is used to wrap the error.

func PropagateException

func PropagateException(errp *error, matchers ...Matcher)

PropagateException catches an exception provided by variations of MustX functions and forwards the error to its argument. It must be called by defer. Cannot reuse PropagateExceptionf, because recover MUST be called at TOP level defer function to recover the panic.

func PropagateExceptionf

func PropagateExceptionf(errp *error, msg string, args ...interface{})

PropagateExceptionf catches an exception provided by variations of MustX functions and forwards the error to its argument. It must be called by defer. If an error context is given (msg!=""), it is added to the propagated error.

func PropagateMatchedExceptionf

func PropagateMatchedExceptionf(errp *error, m Matcher, msg string, args ...interface{})

func R1

func R1[A any](r1 A, err error) result1[A]

R1 bundles the result of an error function with one additional argument to be passed to Must1f.

func R2

func R2[A, B any](r1 A, r2 B, err error) result2[A, B]

R2 bundles the result of an error function with two additional arguments to be passed to Must1.

func R3

func R3[A, B, C any](r1 A, r2 B, r3 C, err error) result3[A, B, C]

R3 bundles the result of an error function with three additional arguments to be passed to Must3.

func R4

func R4[A, B, C, D any](r1 A, r2 B, r3 C, r4 D, err error) result4[A, B, C, D]

R4 bundles the result of an error function with four additional arguments to be passed to Must4.

func Throw

func Throw(err error)

Throw throws an exception if err !=nil.

func Throwf

func Throwf(err error, msg string, args ...interface{})

Throwf throws an exception if err !=nil. A given error context is given, it wraps the error by the context.

Types

type Matcher

type Matcher func(err error) bool

func And

func And(matchers ...Matcher) Matcher

func ByPrototypes

func ByPrototypes(protos ...error) Matcher

func Or

func Or(matchers ...Matcher) Matcher

Jump to

Keyboard shortcuts

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