Documentation
¶
Overview ¶
Package shorterr is an implementation of a short-circuit error handling inspired by the ? operator in Rust.
Example ¶
package main import ( "encoding/json" "fmt" "io" "os" se "github.com/ansiwen/shorterr" ) func main() { f1 := func() (string, error) { file, err := os.Open("data.json") if err != nil { return "", err } jsonData, err := io.ReadAll(file) if err != nil { return "", fmt.Errorf("can't read data.json: %w", err) } var myData map[string]any json.Unmarshal(jsonData, &myData) if err != nil { return "", fmt.Errorf("unmarshalling failed: %w", err) } val, ok := myData["name"] if !ok { return "", fmt.Errorf("missing name property") } name, ok := val.(string) if !ok { return "", fmt.Errorf("invalid name property") } return name, nil } f2 := func() (name string, err error) { defer se.PassTo(&err) file := se.Try(os.Open("data.json")) jsonData := se.Do(io.ReadAll(file)).Or("can't read data.json") var myData map[string]any se.Check(json.Unmarshal(jsonData, &myData), "unmarshalling failed") val, ok := myData["name"] se.Assert(ok, "missing name property") name, ok = val.(string) se.Assert(ok, "invalid name property") return } _, err1 := f1() _, err2 := f2() fmt.Println(err1) fmt.Println(err2) }
Output: open data.json: no such file or directory open data.json: no such file or directory
Index ¶
- func Assert(ok bool, msg string)
- func Check(err error, msg ...string)
- func PassTo(err *error)
- func Try[A any](a A, err error) A
- func Try2[A, B any](a A, b B, err error) (A, B)
- func Try3[A, B, C any](a A, b B, c C, err error) (A, B, C)
- func Try4[A, B, C, D any](a A, b B, c C, d D, err error) (A, B, C, D)
- func Try5[A, B, C, D, E any](a A, b B, c C, d D, e E, err error) (A, B, C, D, E)
- type Result
- type Result2
- type Result3
- type Result4
- type Result5
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
Assert short-circuits the execution of the current function if ok is false and returns msg as an error. PassTo must be installed with defer before.
func Check ¶
Check short-circuits the execution of the current function if the error is not nil. If the optional msg is provided, the err is wrapped with msg. PassTo must be installed with defer before.
func PassTo ¶
func PassTo(err *error)
PassTo stores the intercepted error in the variable err is pointing to. It must be installed with defer in the current function before the other short-circuit functions are used:
import se "github.com/ansiwen/shorterr" func Foo() (err error) { defer se.PassTo(&err) ...
func Try ¶
Try is a wrapper for functions that return a value and an error. It short-circuits the execution of the current function if the error is not nil. Otherwise it only returns the result value. PassTo must be installed with defer before.
Types ¶
type Result ¶
type Result[A any] struct { // contains filtered or unexported fields }
type Result3 ¶
type Result3[A, B, C any] struct { // contains filtered or unexported fields }
type Result4 ¶
type Result4[A, B, C, D any] struct { // contains filtered or unexported fields }