catch

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2023 License: MIT Imports: 2 Imported by: 12

README

catch

Go Reference

Package catch helps to work with panics. Use with care and don't break Go's convention to explicitly check for errors (https://go.dev/blog/error-handling-and-go). By no means it is a good idea to work against language design.

Documentation

Overview

Package catch is an experiment to see if a concise way to work with panics will break Go's convention to explicitly check for errors as described in https://blog.golang.org/error-handling-and-go. By no means it is a good idea to work against language design. So this is to be handled with care!

Somewhere I read a good advice regarding Go's panic:

Do not expect someone else to recover from your panic!

This is quite in line with the “Don't panic across package boundaries” rule. If you panic across package boundaries you are responsible for the program to crash. This shall be something truly exceptional!

Consider that, when my C++ compiler started to support exception handling (yes exceptions weren't there forever), they explained that C++ exceptions shall represent some truly exceptional program state. But today – also in Java – exceptions are rather common (“exception” / “common” - think about it). They are used for error handling. IMHO Andrei Alexandrescu elaborates on this very nicely in https://www.youtube.com/watch?v=kaI4R0Ng4E8.

Addendum to the license

You must not use this package before you really understand the above advice.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must(err error)

Must calls panic(err) if err is not nil.

func MustRet

func MustRet[T any](v T, err error) T

MustRet calls panic(err) if err is not nil. Otherwise it returns v.

func RecoverAs added in v0.2.0

func RecoverAs(err *error)

RecoverAs is used as a deferred function to return a recovered panic as a Recovered error.

Example
type p struct {
	t   time.Time
	msg string
}
err := func() (err error) {
	defer RecoverAs(&err)
	panic(p{
		t:   time.Date(1971, 12, 24, 18, 0, 0, 0, time.UTC),
		msg: "Don't panic!",
	})
}()
fmt.Println(err)
Output:

panic: catch.p={t:{wall:0 ext:62198042400 loc:<nil>} msg:Don't panic!}

Types

type Recovered

type Recovered struct {
	Panic any
}

Recovered is an error that wraps a Panic. It is used by Return.OnRecover() to return a recovered panic.

func (Recovered) Error

func (err Recovered) Error() string

Error make Recovered implement error.

func (Recovered) Unwrap

func (err Recovered) Unwrap() error

Unwrap returns the recovered Panic as error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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