handler

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Overview

A Try/Handle/Catch error handling mechanism implemented using generics, which provides an API similar to previous error handling proposals for the language such as https://github.com/golang/go/issues/32437.

In contrast to previous proposals, however, this implementation makes uses of calls to panic() to handle errors, rather than implicit function returns, since the latter requires language changes. This has performance implications in error cases, so it is not recommended to for use in situations where a high frequency of error returns are expected, but rather for exceptional cases. Fortunately, this is still likely to cover the majority of situations.

This package is intended to be imported unqualified, e.g.

import . "github.com/robdavid/genutil-go/errors/handler"

There are three main functions: Try(), Catch() and Handle(). Try can be used to remove the error component from a function where the function returns a value and an error, e.g.

f := Try(os.Open(fname))

If the error non-nil, the function panics, with a value of a particular type (TryError), which wraps the error. The error can recovered via the Catch() or Handle() functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Catch

func Catch(retErr *error)

A function that will recover a panic created by Try. This should be called in a defer statement prior to calls to Try. The parameter should be a pointer to the calling function's error return value which will be set to the error intercepted by Try.

e.g.

  func readFileTest(fname string) (content []byte, err error) {
	   defer Catch(&err)
	   f := Try(os.Open(fname))
	   defer f.Close()
	   content = Try(io.ReadAll(f))
	   return
  }

func Check

func Check(err error)

An alias for Try0

func Handle

func Handle(handler func(err error))

A function that will recover a panic created by Try. This should be called in a defer statement prior to calls to Try. The handler parameter is an error handling function that can be used to place error handling in one place in your function, such as wrapping the error in another error type. e.g.

func readFileWrapErr(fname string) (content []byte, err error) {
  defer Handle(func(e error) {
    err = fmt.Errorf("Error reading %s: %w", fname, e)
  })
  content = Try(os.ReadFile(fname))
  return
}

func Must

func Must[T any](t T, err error) T

Must removes the error value from a return value. Panics if err is non-nil, otherwise returns the value e.g.

f := Must(os.Open("myfile"))

func PackageName added in v0.17.1

func PackageName() string

PackageName returns the pull path of this package

func Raise added in v0.15.0

func Raise(err error)

Raise an error. No-op if err is nil. An alias for Try0.

func Try

func Try[T any](t T, err error) T

Removes the error component of a function's return value. If there is no error, the non-error value is returned. Otherwise the function panics with a TryError value, wrapping the error. The panic and the error can be recovered via Catch or Handle functions. e.g.

f := Try(os.Open("myfile"))

func Try0

func Try0(err error)

Zero argument variant of Try (for functions that return an error value only)

func Try1

func Try1[T any](t T, err error) T

An alias for Try

func Try2

func Try2[T1 any, T2 any](p1 T1, p2 T2, err error) (T1, T2)

Variant of try with 2 non-error arguments

func Try3

func Try3[T1 any, T2 any, T3 any](p1 T1, p2 T2, p3 T3, err error) (T1, T2, T3)

Variant of try with 3 non-error arguments

func Try4

func Try4[T1 any, T2 any, T3 any, T4 any](p1 T1, p2 T2, p3 T3, p4 T4, err error) (T1, T2, T3, T4)

Variant of try with 4 non-error arguments

func Try5

func Try5[T1 any, T2 any, T3 any, T4 any, T5 any](p1 T1, p2 T2, p3 T3, p4 T4, p5 T5, err error) (T1, T2, T3, T4, T5)

Variant of try with 5 non-error arguments

func Try6

func Try6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any](p1 T1, p2 T2, p3 T3, p4 T4, p5 T5, p6 T6, err error) (T1, T2, T3, T4, T5, T6)

Variant of try with 6 non-error arguments

func Try7

func Try7[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any](p1 T1, p2 T2, p3 T3, p4 T4, p5 T5, p6 T6, p7 T7, err error) (T1, T2, T3, T4, T5, T6, T7)

Variant of try with 7 non-error arguments

func Try8

func Try8[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any, T8 any](p1 T1, p2 T2, p3 T3, p4 T4, p5 T5, p6 T6, p7 T7, p8 T8, err error) (T1, T2, T3, T4, T5, T6, T7, T8)

Variant of try with 8 non-error arguments

func Try9

func Try9[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any, T8 any, T9 any](p1 T1, p2 T2, p3 T3, p4 T4, p5 T5, p6 T6, p7 T7, p8 T8, p9 T9, err error) (T1, T2, T3, T4, T5, T6, T7, T8, T9)

Variant of try with 9 non-error arguments

Types

type TryError

type TryError struct {
	Error error
}

Simple error wrapper. Try will panic with a value of this type when it encounters an error.

func (TryError) String

func (te TryError) String() string

Display a message including the underlying error

Jump to

Keyboard shortcuts

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