errors

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2020 License: MIT Imports: 3 Imported by: 0

README

errors

⚡️errors wrapping and stack traces

You can add the library using this example:

go get -u github.com/krocos/errors

Usage example is placed in errors_test.go file.

Documentation

Overview

Package errors provides functions and types for effective work with errors, the wraps, and stack traces.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target interface{}) bool

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

func Is

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

func JSONStack

func JSONStack(err error) []byte

JSONStack returns a json-encoded stack of errors.

func New

func New(msg string) error

New creates new error.

func NewWithFields

func NewWithFields(msg string, fields map[string]interface{}) error

NewWithFields creates new error with contextual fields.

func Restore

func Restore(stack []map[string]interface{}) error

Restore restores an error from a result returned by Stack func.

func RestoreRaw

func RestoreRaw(stack []byte) error

RestoreRaw restores an error from a result returned by JSONStack func.

Example
package main

import (
	"fmt"

	"github.com/krocos/errors"
)

func main() {
	err := errors.NewWithFields("1", errors.Fields{
		"f1": "v1",
	})
	err = errors.WrapWithFields(err, "2", errors.Fields{
		"f2": "v2",
	})

	b := errors.JSONStack(err)
	fmt.Println(string(b))

	// Restore error from a []byte (json) stack.
	err = errors.RestoreRaw(b)
	if err != nil {
		fmt.Println(err.Error())
	}

}
Output:
[{"fields":{"f2":"v2"},"message":"2"},{"fields":{"f1":"v1"},"message":"1"}]
2: 1

func Stack

func Stack(err error) []map[string]interface{}

Stack returns a stack of errors in sequential order.

func Unwrap

func Unwrap(err error) error

Unwrap returns previous error if err's type contains an Unwrap method returning error, otherwise, Unwrap returns nil. (Doc copied from builtin function.)

func Wrap

func Wrap(err error, msg string) error

Wrap wraps previous error into a new one with explanatory message.

func WrapWithFields

func WrapWithFields(err error, msg string, fields map[string]interface{}) error

WrapWithFields wraps previous error into a new one with explanatory message and with contextual fields.

Types

type Error

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

Error contains current and previous possible errors.

func (Error) Error

func (err Error) Error() string

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns previous error.

type Fields

type Fields map[string]interface{}

Fields represents just an alias for a more complicated on reading map[string]interface{}.

Jump to

Keyboard shortcuts

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