pan

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: BSD-3-Clause Imports: 2 Imported by: 15

Documentation

Overview

Package pan (short for panic) can be used to implement internal error propagation via panic and recover. A benefit over naive panic/recover usage is that runtime errors and other unrelated panics are disregarded.

The check, must and mustcheck subpackages are designed to be used via dot imports (if that's something you prefer).

Example 1

import "import.name/pan"

func check(err error)              { pan.Check(err) }
func must[T any](x T, err error) T { return pan.Must(x, err) }

func internal() string {
    check(os.Chdir("/nonexistent"))
    return must(os.Getwd())
}

func Public() (s string, err error) {
    err = pan.Recover(func() {
        s = internal()
    })
    return
}

Example 2

import (
    "import.name/pan"
    . "import.name/pan/mustcheck"
)

func internal() string {
    Check(os.Chdir("/nonexistent"))
    return Must(os.Getwd())
}

func Public() (s string, err error) {
    err = pan.Recover(func() {
        s = internal()
    })
    return
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(err error)

Check panics if err is not nil. See Error, Fatal and Recover.

func Error

func Error(x interface{}) error

Error returns an error if x is a value created by Check, Must, Panic or Wrap. If x is nil, nil is returned. If x is something else, Error panics with x as the panic value.

func Fatal

func Fatal(x interface{})

Fatal is like Error, but the error is written to stderr and the program terminates with exit status 1.

func Must added in v0.2.0

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

Must panics if err is not nil. Otherwise it returns x. See Error, Fatal and Recover.

func Panic added in v0.2.0

func Panic(err error)

Panic unconditionally. See Error, Fatal and Recover.

func Recover

func Recover(f func()) (err error)

Recover invokes f and returns any error value passed to Check, Must, Panic or Wrap.

func Wrap added in v0.1.1

func Wrap(err error) error

Wrap error. See Error, Fatal and Recover.

Types

This section is empty.

Directories

Path Synopsis
Package check will export only symbols prefixed with word Check.
Package check will export only symbols prefixed with word Check.
Package must will export only symbols prefixed with word Must.
Package must will export only symbols prefixed with word Must.
Package mustcheck will export only symbols prefixed with words Check or Must.
Package mustcheck will export only symbols prefixed with words Check or Must.

Jump to

Keyboard shortcuts

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