errors

package module
v0.0.0-...-7a3cfc8 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2019 License: MIT Imports: 4 Imported by: 0

README

errors GoDoc Report card

package main

import (
	"encoding/json"
	"fmt"

	"github.com/rafaelsq/errors"
)

var (
	ErrA = fmt.Errorf("err a")
)

func responseErrA() error {
	return ErrA
}

func responseErrB(id int) error {
	err := responseErrA()
	if err != nil {
		return errors.New("err b").SetArg("id", id).SetParent(err)
	}

	return nil
}

func main() {
	err := responseErrB(10)
	fmt.Println(err) // "err b; err a"

	fmt.Println(errors.Cause(err) == ErrA) // true

	er, _ := err.(*errors.Error)
	fmt.Printf("caller %s\n", er.Caller) // caller main.go:21

	b, _ := json.Marshal(er.Args)
	fmt.Printf("args %q\n", b) // args {"id": 10}

	fmt.Printf("parent %v\n", *er.Parent) // parent err a

	errs := errors.List(err)
	fmt.Println("len", len(errs))                   // 2
	fmt.Println(">", errs[0])                       // err a
	fmt.Println(">", errs[1])                       // err b; err a
	fmt.Println(">", (errs[1].(*errors.Error)).Msg) // err b
}

Documentation

Overview

Package errors provides simple error stack and a little more of context to the error

Normally you would compose your error doing something like this

response, err := doSomething(somethingID)
if err != nil {
    return fmt.Errorf(
        "my error with id %d, response \"%v\"; %v",
        somethingID,
        response,
        err,
    )
}

All the context from your error will have to be in the error string. After yout would need to extract all context from the string above.

What this package do is add arguments to your error;

response, err := doSomething(somethingID)
if err != nil {
    return errors.New("my error").
         SetParent(err).
         SetArg("somethingID", somethingID).
         SetArg("response", response)
}

fmt.Println(err) // "something err; my error"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Caller

func Caller(skipFrames int) string

Caller returns the file:number from where is was called

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error

func List

func List(err error) []error

List returns a list of errors

Types

type Error

type Error struct {
	Msg    string
	Parent *error
	Args   map[string]interface{}
	Caller string
}

Error is the error with arguments

func Errorf

func Errorf(format string, args ...interface{}) *Error

Errorf formats according to a format specifier

func New

func New(err string) *Error

New creates a new Error.

parentErr := New("parent")
err := New("error").SetArg("key", "value").SetParent(parentErr)

func (*Error) Error

func (e *Error) Error() string

Error returns erro as a string

func (*Error) SetArg

func (e *Error) SetArg(arg string, value interface{}) *Error

SetArg set an argument to the Error

func (*Error) SetParent

func (e *Error) SetParent(err error) *Error

SetParent set a parent error to Error

Jump to

Keyboard shortcuts

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