errors

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2021 License: MIT Imports: 3 Imported by: 2

README

Errors package

Usage

package main

import (
	"fmt"

	"github.com/thefabric-io/errors"
)

var (
	Err1 = errors.New("error 1", "ERR001")
	Err2 = errors.New("error 2", "ERR002")
	Err3 = errors.New("error 3", "ERR003")
	Err4 = errors.New("error 4", "ERR004")
	Err5 = errors.New("error 5", "ERR005")
	Err6 = errors.New("error 6", "ERR006")
	Err7 = errors.New("error 7", "ERR007")
)

func main() {
	var result error
	result = errors.Stack(result, Err1, Err2, fmt.Errorf("trivial error"))

	// Stacking directly with Errors type
	if e, ok := result.(*errors.Errors); ok {
		_ = e.Stack(Err3)
	}

	result = errors.Stack(
		result,
		Err4,
		errors.New(
			errors.Message(fmt.Sprintf("error with a subject of '%s' and a value of '%.2f' €", "10c39745-c7fe-429f-a0fb-5035dbdc6c47", 12.00)),
			errors.CodeSubject,
		))

	result = errors.Stack(result, Err5, Err6, Err7, fmt.Errorf("trivial error with a value of '%s'", "0.00 €"))

	fmt.Println(result)

	b, err := result.(*errors.Errors).MarshalJSON()
	if err != nil {
		fmt.Println(errors.Stack(errors.New("could not marshal", "MF001"), err))
	}

	fmt.Println(" ============== JSON Representation ==============")
	fmt.Println(string(b))
}

Documentation

Index

Constants

View Source
const (
	CodeSubject = "subject"
	CodeInvalid = "invalid"
	CodeUnknown = "unknown"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Code

type Code string

func (Code) Equal

func (c Code) Equal(c2 Code) bool

type Comparable

type Comparable interface {
	Compare(err1, err2 error) bool
}

func CompareCodeOnlyStrategy

func CompareCodeOnlyStrategy() Comparable

func CompareMessageOnlyStrategy

func CompareMessageOnlyStrategy() Comparable

func CompareStrictStrategy

func CompareStrictStrategy() Comparable

type Error

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

func New

func New(message Message, code Code) *Error

func (*Error) Error

func (e *Error) Error() string

func (Error) MarshalJSON

func (e Error) MarshalJSON() ([]byte, error)

type Errors

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

func Stack

func Stack(source error, targets ...error) (result *Errors)

func (*Errors) Error

func (e *Errors) Error() string

func (*Errors) First

func (e *Errors) First() Error

func (*Errors) Is

func (e *Errors) Is(err error) bool

func (*Errors) IsWithStrategy

func (e *Errors) IsWithStrategy(err error, comparable Comparable) bool

func (*Errors) Last

func (e *Errors) Last() Error

func (*Errors) MarshalJSON

func (e *Errors) MarshalJSON() ([]byte, error)

func (*Errors) Stack

func (e *Errors) Stack(target error) *Errors

type Message

type Message string

func (Message) Equal

func (m Message) Equal(m2 Message) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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