merr

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2020 License: GPL-3.0 Imports: 2 Imported by: 0

README

merr

merr is library for handle multiple error on golang.

This allows a function ig Go to return an list of error. It will be usefull when you do call some function to get multiple error handler.

merr is fully compatible with the Go standard library errors package, including the functions As, Is, and Unwrap. This provides a standardized approach for introspecting on error values.

Documentation

Go Doc Go Report Card Build Status

Installation

Install using go get github.com/firdasafridi/merr.

Example

merr is easy to use in existing Go applications/libraries that may not be aware of it.

The Set function is used to create a list of errors. If you need to set prefix you can add it with SetPrefix.

var mulerr merr.Error

if err := sampleLogic1(); err != nil {
	mulerr.Set(err)
}

if err := sampleLogic2(); err != nil {
	mulerr.SetPrefix("Sample Prefix", err)
}
    
return mulerr.IsError()
Customizing the formating of the errors

If you want customizing format error it will be simple like this.

var funcFormat = func(errorList []error) string {
 	if len(errorList) == 1 {
		return ""
	}

	var listErr string
	for _, err := range errorList {
		listErr += err.Error() + "\n"
	}
	return "[ERROR Change]" + listErr
}

merr.NewFormat(funcFormat)

Documentation

Overview

Package merr is library for handle multiple error on golang (see https://pkg.go.dev/github.com/firdasafridi/merr?tab=overview) for more documentation

This allows a function ig Go to return an list of error. It will be useful when you do call some function to get multiple error handler.

merr is fully compatible with the Go standard library errors package, including the functions As, Is, and Unwrap. This provides a standardized approach for introspecting on error values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatErr

func FormatErr(errorList []error) string

FormatErr is a basic formatter that outputs the error.

func Len

func Len(err error) int

Len error from merr.Errors

func NewFormat

func NewFormat(fErr func(errorList []error) string)

NewFormat change default format error it used

Types

type Error

type Error struct {
	Errors []error
}

Error is containt field to tract multiple errors. This accumulate errors.

func Check

func Check(err error) *Error

Check will return multi error format if the data is true

func (*Error) Error

func (err *Error) Error() string

func (*Error) IsError

func (err *Error) IsError() error

IsError returns an error if errors represent a list of errors, or return nil if the list of error is empty. This function is useful at the end of the accumulation to handling the error.

func (*Error) Len

func (err *Error) Len() int

Len returns count errors represent list

func (*Error) Set

func (err *Error) Set(newErr ...error)

Set is function to set new error if not found it will return no error

func (*Error) SetPrefix

func (err *Error) SetPrefix(prefix string, newErr error)

SetPrefix is function to set new error with prefix if not found it will return no error

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns an error from Error (or nil if there are no errors). This error returned will further support Unwrap to get the next error, etc. The order will match the order of Errors in the merr.Error at the time of calling.

func (*Error) WrappedErrors

func (err *Error) WrappedErrors() []error

WrappedErrors returns the list of errors that this Error is wrapping. It is an implementation of the errwrap.Wrapper interface so that merr.Error can be used with that library.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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