results

package module
v0.0.0-...-975b364 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: MIT Imports: 1 Imported by: 2

README

results

A Golang error handling library, inspired by Rust (and somewhat Java). No non-std imports, and minimal exports so you can dot import it without a bloated namespace.

Useful for converting those pesky (value, error) returns into something useful. No more of this:

value, err := funkyfunction()
if err != nil {
	panic(err)
}

And instead, this:

value := Unwrap(funkyfunction())

You can also catch specific errors and Unwrap to default values. :)

Usage

Go get it: go get github.com/nobodyawesomer/results

And then (suggested) dot import it:

import (
	. "github.com/nobodyawesomer/results"
)

Then, wrap your function calls, and unwrap the errors:

value := Try(strconv.atoi("bloop")).
	UnwrapOr(10) // defaults to 10 on any error :)

See documentation for more options.

Roadmap

Basically whatever I find useful. I'll probably look into an ergonomic way to incorporate logging, because I often if err != nil {log.Fatal(err)}.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(err error)

Check is syntactical sugar for dealing with errors. It simply panics on error.

func Unwrap

func Unwrap[T any](value T, err error) T

Unwrap attempts to unwrap a value-error pair. If there is an error, it panics. Otherwise, it returns the value. Useful for syntactic sugar, but only use this if you do not expect any errors.

Types

type Result

type Result[R any] interface {
	// Out outputs the Result unresolved.
	Out() (R, error)

	// Unwrap attempts to resolve the Result.
	// If there is an error, it panics.
	// Otherwise, it returns its underlying value.
	Unwrap() R

	// UnwrapOr resolves the Result safely.
	// If there is an error, it quietly returns the provided value.
	// Otherwise, it returns its underlying value.
	UnwrapOr(value R) R

	// UnwrapOrElse resolves the Result safely.
	// If there is an error, it runs the onError callback to
	// generate a new value.
	// Otherwise, it returns its underlying value.
	// onError must not be nil. If you would like to utilize the error,
	// see CatchAll.
	UnwrapOrElse(onError func() R) R

	// UnwrapOrDefault resolves the Result safely.
	// If there is an error, it generates a new value using new(R),
	// yielding the zero value for that type.
	// Otherwise, it returns its underlying value.
	UnwrapOrDefault() R

	// Catch attempts to catch a particular error type.
	// If the error is a match for a given errorType, then it runs
	// the onCatch callback and sets the error type to nil.
	// Otherwise, the error, if it exists, is passed through unchanged
	// along with the value.
	//
	// If onCatch is nil, it will catch the error type and do nothing.
	// This is equivalent to passing it an empty function.
	//
	// The Result is passed back for chaining convenience.
	Catch(errorType error, onCatch func(*R, error)) Result[R]

	// CatchAnd attempts to catch a particular error type.
	// If the error is a match for a given errorType, then it sets
	// the value to value and the error to nil.
	// Otherwise, the error, if it exists, is passed through unchanged
	// along with the value.
	//
	// The Result is passed back for chaining convenience.
	CatchAnd(errorType error, value R) Result[R]

	// CatchAll catches all remaining errors and runs the provided onCatch
	// callback. It then returns the underlying value.
	// onCatch must not be nil.
	CatchAll(onCatch func(*R, error)) R
}

func Try

func Try[T any](value T, err error) Result[T]

Try returns a Result given a value and an error.

Jump to

Keyboard shortcuts

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