erry

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: MIT Imports: 3 Imported by: 1

README

erry

golangci-lint Go Reference

Description

A Go package for handling multiple errors.

⚡️ The project is in rapid stage of development, issues and critics are welcome.

⚡️ There will be no need to use this package after Go 1.20 release, because the release brings new multierror approach.

Features

  • simple minded
  • standart library errors.As & errors.Is compatible
  • dependency free

Table of contents

Getting started

See the documentation for further details.

Installation
go get github.com/k1gabyt0/erry
Usage
Create new multi-error
 errA := errors.New("err A")
 errB := errors.New("err B")

 multierr := erry.NewError("multierror", errA, errB)
 // also this below is the same
 // multierr = erry.NewError("multierror").Wrap(errA, errB)
 //       msg="multierror"
 //           /       \
 //        errA      errB

 fmt.Println(multierr)
 if errors.Is(multierr, errA) {
  fmt.Println("This is error A")

 }
 if errors.Is(multierr, errB) {
  fmt.Println("This is error B")

 }
 // Output: multierror:
 // err A
 // err B
 // This is error A
 // This is error B

Output:

multierror:
    err A
    err B
This is error A
This is error B
Transform existing error into multi-error
 errA := errors.New("err A")
 errB := errors.New("err B")
 errC := errors.New("err C")

 multierr := erry.ErrorFrom(errA).Wrap(errB, errC)
 //          msg: errA.Error()
 //     original: errA
 //               /  \
 //            errB  errC
 fmt.Println(multierr)

 if errors.Is(multierr, errA) {
  fmt.Println("This is error A")

 }
 if errors.Is(multierr, errB) {
  fmt.Println("This is error B")

 }
 if errors.Is(multierr, errB) {
  fmt.Println("This is error C")

 }
 // Output: err A:
 // err B
 //  err C
 // This is error A
 // This is error B
 // This is error C

Alternatives

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MError

type MError struct {
	// contains filtered or unexported fields
}

MError is an error that able to store multiple errors.

func ErrorFrom

func ErrorFrom(original error) *MError

ErrorFrom creates an MError err from passed original error and it's inner errs.

err's msg will be the same as original.Error(). Function call errors.Is(err, original) will return true.

If passed original is already an MError, then original is returned.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/k1gabyt0/erry"
)

func main() {
	errA := errors.New("err A")
	errB := errors.New("err B")
	errC := errors.New("err C")

	multierr := erry.ErrorFrom(errA).Wrap(errB, errC)
	//          msg: errA.Error()
	//     original: errA
	//               /  \
	//            errB  errC
	fmt.Println(multierr)

	if errors.Is(multierr, errA) {
		fmt.Println("This is error A")

	}
	if errors.Is(multierr, errB) {
		fmt.Println("This is error B")

	}
	if errors.Is(multierr, errB) {
		fmt.Println("This is error C")

	}
}
Output:
err A:
	err B
	err C
This is error A
This is error B
This is error C

func NewError

func NewError(msg string, errs ...error) *MError

NewError returns MError with passed msg and errs.

Passed nil errs though will be filtered. Only non-nil errs are stored.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/k1gabyt0/erry"
)

func main() {
	errA := errors.New("err A")
	errB := errors.New("err B")

	multierr := erry.NewError("multierror", errA, errB)
	// also this below is the same
	// multierr = erry.NewError("multierror").Wrap(errA, errB)
	//       msg="multierror"
	//           /       \
	//        errA      errB

	fmt.Println(multierr)
	if errors.Is(multierr, errA) {
		fmt.Println("This is error A")

	}
	if errors.Is(multierr, errB) {
		fmt.Println("This is error B")

	}
}
Output:
multierror:
	err A
	err B
This is error A
This is error B

func (*MError) As

func (v *MError) As(target any) bool

As finds the first error in v's tree that matches target(starting with the root), and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

As finds the first matching error in a preorder traversal of the tree.

func (*MError) Error

func (v *MError) Error() string

Error formats error messages with new lines starting with v.msg.

func (*MError) Errors

func (v *MError) Errors() []error

Errors returns all errors stored in v.

func (*MError) Is

func (v *MError) Is(target error) bool

Is reports whether any error in v's tree matches target.

It is always returns true if checked against non-nil original.

func (*MError) Message

func (v *MError) Message() string

Message returns root message of MError that will be displayed first.

func (*MError) Original

func (v *MError) Original() error

Original error that have been passed to become mutli-error.

func (*MError) Wrap added in v0.4.1

func (v *MError) Wrap(errs ...error) *MError

Wrap stores passed errs in MError.

Passed nil errs though will be filtered. Only non-nil errs are stored.

Jump to

Keyboard shortcuts

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