errchain

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: MIT Imports: 1 Imported by: 0

README

errchain

Go Reference

About

Here's Go package for errors chaining for further examining using the standard errors.Is. You can learn more about working with errors in Go in this article. Explore example below for more understanding.

This package uses module version numbering.

Install

With a correctly configured Go toolchain run:

go get github.com/electrofocus/errchain

Examples

Let's build new error from multiple errors and examine it with errors.Is:

package main

import (
	"errors"
	"fmt"
	"io"
	"net/http"
	"os"

	"github.com/electrofocus/errchain"
)

func main() {
	var (
		myErr = errors.New("my err")
		err   = errchain.New(myErr, io.EOF, os.ErrClosed, http.ErrHijacked)
	)

	if errors.Is(err, io.EOF) {
		fmt.Printf("here we have %q error\n", io.EOF)
	}

	if errors.Is(err, myErr) {
		fmt.Printf("and %q error\n", myErr)
	}

	if errors.Is(err, os.ErrClosed) {
		fmt.Printf("and %q error\n", os.ErrClosed)
	}

	if errors.Is(err, http.ErrHijacked) {
		fmt.Printf("and %q error,\n", http.ErrHijacked)
	}

	if !errors.Is(err, http.ErrAbortHandler) {
		fmt.Printf("but don't have %q error\n", http.ErrAbortHandler)
	}
}

Open above example in The Go Playground.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(errs ...error) error

New builds errors chain from errs. You can use errors.Is to check the chain for compliance with any error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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