must

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2021 License: Apache-2.0 Imports: 14 Imported by: 27

README

Go library to shorten handling of impossible conditions

GoDoc go.dev reference

must.OK(os.Unsetenv("FOO"))
bs := must.Bytes(json.Marshal(dataStructureDefinedInCode))
defer must.Do(f.Close)

is

if err := os.Unsetenv("FOO"); err != nil {
     panic(err)
}
bs, err := json.Marshal(dataStructureDefinedInCode)
if err != nil {
    panic(err)
}
defer func() {
    if err := f.Close(); err != nil{
        panic(err)
    }
}()

Why panic?

Go error handling style is practical for majority of errors. However not all errors are meaningful and actionable by the caller, so it does not make sense to surface them.

Go tacitly acknowledges it by providing functions regex.MustCompile and template.Must in the standard library. This library expands on the same idea.

Where is it useful?

The library is proven to be useful in the following situations:

  • Programming errors in all kinds of code, similar to regex.MustCompile,
  • Filesystem-related errors in a build system,
  • Fatal errors in server-side SaaS code.

The last point needs an explanation.

Unlike CLI tools where errors ought to be handled in a way meaningful to a user who might not know the internals of the application, server-side SaaS applications are run by the SREs or developers who do not need the extra wrapping, and instead require fatal errors to be delivered reliably and with all gory details.

Passing errors up the stack in a regular Go way risks losing the error (by forgetting to wrap it, or by swallowing it completely), and prevents the original stacktrace from being surfaced. panicing at the place of fatal error is reliable and precise.

Is it a good idea to use must everywhere?

Definitely not. Go rules for error handling are well thought-out. Reserve must for programming errors and impossible conditions.

Copyright Tectonic Networks, Inc.

Licensed under Apache 2.0 license.

Authors:

Documentation

Overview

Package must is a library to shorten handling of impossible conditions

must.OK(os.Unsetenv("FOO"))
bytes := must.Bytes(json.Marshal(dataStructureDefinedInCode))
defer must.Do(f.Close)

is

if err := os.Unsetenv("FOO"); err != nil {
    panic(err)
}

bytes, err := json.Marshal(dataStructureDefinedInCode)
if err != nil {
    panic(err)
}

defer func() {
    if err := f.Close(); err != nil{
        panic(err)
    }
}()

Go error handling style is practical for majority of errors. However not all errors are meaningful and actionable by the caller, so it does not make sense to surface them.

Go tacitly acknowledges it by providing functions regex.MustCompile and template.Must in the standard library. This library expands on the same idea.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any(arg interface{}, err error) interface{}

Any panics on error, returns the first argument otherwise

func BigInt added in v0.5.0

func BigInt(arg *big.Int, err error) *big.Int

BigInt panics on error, returns the first argument otherwise

func Bool

func Bool(arg bool, err error) bool

Bool panics on error, returns the first argument otherwise

func Bools

func Bools(arg []bool, err error) []bool

Bools panics on error, returns the first argument otherwise

func Byte

func Byte(arg byte, err error) byte

Byte panics on error, returns the first argument otherwise

func Bytes

func Bytes(arg []byte, err error) []byte

Bytes panics on error, returns the first argument otherwise

func Complex128

func Complex128(arg complex128, err error) complex128

Complex128 panics on error, returns the first argument otherwise

func Complex128s

func Complex128s(arg []complex128, err error) []complex128

Complex128s panics on error, returns the first argument otherwise

func Complex64

func Complex64(arg complex64, err error) complex64

Complex64 panics on error, returns the first argument otherwise

func Complex64s

func Complex64s(arg []complex64, err error) []complex64

Complex64s panics on error, returns the first argument otherwise

func Do

func Do(fn func() error)

Do calls the function and panics on error.

For use primarily in defer statements.

BAD example:

// f.Close will be called now, must.OK will be called on function exit
defer must.OK(f.Close())

GOOD example:

defer must.Do(f.Close)

func ECDSAPrivateKey added in v0.5.0

func ECDSAPrivateKey(arg *ecdsa.PrivateKey, err error) *ecdsa.PrivateKey

ECDSAPrivateKey panics on error, returns the first argument otherwise

func ECDSAPublicKey added in v0.5.0

func ECDSAPublicKey(arg *ecdsa.PublicKey, err error) *ecdsa.PublicKey

ECDSAPublicKey panics on error, returns the first argument otherwise

func ED25519PrivateKey added in v0.5.0

func ED25519PrivateKey(arg *ed25519.PrivateKey, err error) *ed25519.PrivateKey

ED25519PrivateKey panics on error, returns the first argument otherwise

func ED25519PublicKey added in v0.5.0

func ED25519PublicKey(arg *ed25519.PublicKey, err error) *ed25519.PublicKey

ED25519PublicKey panics on error, returns the first argument otherwise

func Float32

func Float32(arg float32, err error) float32

Float32 panics on error, returns the first argument otherwise

func Float32s

func Float32s(arg []float32, err error) []float32

Float32s panics on error, returns the first argument otherwise

func Float64

func Float64(arg float64, err error) float64

Float64 panics on error, returns the first argument otherwise

func Float64s

func Float64s(arg []float64, err error) []float64

Float64s panics on error, returns the first argument otherwise

func HTMLTemplate added in v0.5.0

func HTMLTemplate(arg *htmltemplate.Template, err error) *htmltemplate.Template

HTMLTemplate panics on error, returns the first argument otherwise

func HTTPHandler added in v0.4.0

func HTTPHandler(arg http.Handler, err error) http.Handler

HTTPHandler panics on error, returns the first argument otherwise

func HTTPRequest added in v0.3.0

func HTTPRequest(arg *http.Request, err error) *http.Request

HTTPRequest panics on error, returns the first argument otherwise

func HTTPResponse added in v0.5.0

func HTTPResponse(arg *http.Response, err error) *http.Response

HTTPResponse panics on error, returns the first argument otherwise

func HTTPTransport added in v0.5.0

func HTTPTransport(arg *http.Transport, err error) *http.Transport

HTTPTransport panics on error, returns the first argument otherwise

func Hash added in v0.5.0

func Hash(arg hash.Hash, err error) hash.Hash

Hash panics on error, returns the first argument otherwise

func IOReadCloser

func IOReadCloser(arg io.ReadCloser, err error) io.ReadCloser

IOReadCloser panics on error, returns the first argument otherwise

func IOReader added in v0.6.0

func IOReader(arg io.Reader, err error) io.Reader

IOReader panics on error, returns the first argument otherwise

func IOWriter

func IOWriter(arg io.Writer, err error) io.Writer

IOWriter panics on error, returns the first argument otherwise

func Int

func Int(arg int, err error) int

Int panics on error, returns the first argument otherwise

func Int16

func Int16(arg int16, err error) int16

Int16 panics on error, returns the first argument otherwise

func Int16s

func Int16s(arg []int16, err error) []int16

Int16s panics on error, returns the first argument otherwise

func Int32

func Int32(arg int32, err error) int32

Int32 panics on error, returns the first argument otherwise

func Int32s

func Int32s(arg []int32, err error) []int32

Int32s panics on error, returns the first argument otherwise

func Int64

func Int64(arg int64, err error) int64

Int64 panics on error, returns the first argument otherwise

func Int64s

func Int64s(arg []int64, err error) []int64

Int64s panics on error, returns the first argument otherwise

func Int8

func Int8(arg int8, err error) int8

Int8 panics on error, returns the first argument otherwise

func Int8s

func Int8s(arg []int8, err error) []int8

Int8s panics on error, returns the first argument otherwise

func Ints

func Ints(arg []int, err error) []int

Ints panics on error, returns the first argument otherwise

func NetIP

func NetIP(arg net.IP, err error) net.IP

NetIP panics on error, returns the first argument otherwise

func NetListener

func NetListener(arg net.Listener, err error) net.Listener

NetListener panics on error, returns the first argument otherwise

func NetURL

func NetURL(arg *url.URL, err error) *url.URL

NetURL panics on error, returns the first argument otherwise

func OK

func OK(err error)

OK panics on error

func OSFile

func OSFile(arg *os.File, err error) *os.File

OSFile panics on error, returns the first argument otherwise

func OSFileInfo

func OSFileInfo(arg os.FileInfo, err error) os.FileInfo

OSFileInfo panics on error, returns the first argument otherwise

func OSFileInfos

func OSFileInfos(arg []os.FileInfo, err error) []os.FileInfo

OSFileInfos panics on error, returns the first argument otherwise

func RSAPrivateKey added in v0.5.0

func RSAPrivateKey(arg *rsa.PrivateKey, err error) *rsa.PrivateKey

RSAPrivateKey panics on error, returns the first argument otherwise

func RSAPublicKey added in v0.5.0

func RSAPublicKey(arg *rsa.PublicKey, err error) *rsa.PublicKey

RSAPublicKey panics on error, returns the first argument otherwise

func Rune

func Rune(arg rune, err error) rune

Rune panics on error, returns the first argument otherwise

func Runes

func Runes(arg []rune, err error) []rune

Runes panics on error, returns the first argument otherwise

func String

func String(arg string, err error) string

String panics on error, returns the first argument otherwise

func Strings

func Strings(arg []string, err error) []string

Strings panics on error, returns the first argument otherwise

func TextTemplate added in v0.5.0

func TextTemplate(arg *texttemplate.Template, err error) *texttemplate.Template

TextTemplate panics on error, returns the first argument otherwise

func Time added in v0.5.0

func Time(arg time.Time, err error) time.Time

Time panics on error, returns the first argument otherwise

func Uint

func Uint(arg uint, err error) uint

Uint panics on error, returns the first argument otherwise

func Uint16

func Uint16(arg uint16, err error) uint16

Uint16 panics on error, returns the first argument otherwise

func Uint16s

func Uint16s(arg []uint16, err error) []uint16

Uint16s panics on error, returns the first argument otherwise

func Uint32

func Uint32(arg uint32, err error) uint32

Uint32 panics on error, returns the first argument otherwise

func Uint32s

func Uint32s(arg []uint32, err error) []uint32

Uint32s panics on error, returns the first argument otherwise

func Uint64

func Uint64(arg uint64, err error) uint64

Uint64 panics on error, returns the first argument otherwise

func Uint64s

func Uint64s(arg []uint64, err error) []uint64

Uint64s panics on error, returns the first argument otherwise

func Uint8

func Uint8(arg uint8, err error) uint8

Uint8 panics on error, returns the first argument otherwise

func Uint8s

func Uint8s(arg []uint8, err error) []uint8

Uint8s panics on error, returns the first argument otherwise

func Uintptr

func Uintptr(arg uintptr, err error) uintptr

Uintptr panics on error, returns the first argument otherwise

func Uintptrs

func Uintptrs(arg []uintptr, err error) []uintptr

Uintptrs panics on error, returns the first argument otherwise

func Uints

func Uints(arg []uint, err error) []uint

Uints panics on error, returns the first argument otherwise

func X509Certificate added in v0.5.0

func X509Certificate(arg *x509.Certificate, err error) *x509.Certificate

X509Certificate panics on error, returns the first argument otherwise

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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