wraperr

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: Apache-2.0 Imports: 4 Imported by: 2

README

WrapErr

WrapErr is a Golang library for working with errors.

How to get

go get github.com/Chekunin/wraperr

Usage

Let's look at a simple example, where you make an error tracing.

import (
    "github.com/Chekunin/wraperr
    "errors"
)

func main() {
	if err := somethingGoWrong(); err != nil {
		err = wraperr.Wrap(fmt.Errorf("somethingGoWrong"), err)
		fmt.Println(err) // somethingGoWrong: any error
	}
}

func somethingGoWrong() error {
	return errors.New("any error")
}

But with WrapErr you can also operate with any previous error, therefore, for instance, you can send corresponding error from your API.

var errDivisionByZero = errors.New("division by zero")
func divide(numerator int, denominator int) (int, error) {
	if denominator == 0 {
		return 0, errDivisionByZero
	}
	res := numerator / denominator
	return res, nil
}

func main() {
    http.HandleFunc("/divide", func(w http.ResponseWriter, r *http.Request) {
        numerator := getNumeratorFromRequest(r) // e.g. numerator = 5
        denominator := getDenominatorFromRequest(r) // e.g. denominator = 0
        res, err := divide(numerator, denominator)
        if err != nil {
            err = wraperr.Wrap(fmt.Errorf("devide with params %d and %d", denominator, denominator), err)
            if errors.Is(err, errDivisionByZero) {
                http.Error(w, "bad request", 400)
            } else {
                http.Error(w, "internal server error", 500)
            }
            log.Print(err) // devide with params 5 and 0: division by zero
            return
        }
        fmt.Fprintf(w, "the answer is %d", res)
    })
    http.ListenAndServe(":8080", nil)
}

Also WrapErr supports Golang's 1.13 errors.As function.
Thus you can not only work with just a textual view of previous errors, but with original objects of those errors.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Frame

type Frame uintptr

type StackTrace

type StackTrace []Frame

type WrapErr

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

func Wrap added in v0.2.0

func Wrap(curErr error, prevErr error) *WrapErr

func WrapString added in v0.2.2

func WrapString(curErrMessage string, prevErr error) *WrapErr

func (*WrapErr) As

func (e *WrapErr) As(target interface{}) bool

func (WrapErr) ContainsError

func (e WrapErr) ContainsError(target error) bool

func (WrapErr) ContainsType

func (e WrapErr) ContainsType(target interface{}) bool

func (WrapErr) Error

func (e WrapErr) Error() string

func (WrapErr) Is

func (e WrapErr) Is(target error) bool

func (WrapErr) StackTrace

func (e WrapErr) StackTrace() StackTrace

func (WrapErr) String

func (e WrapErr) String() string

func (WrapErr) Unwrap

func (e WrapErr) Unwrap() error

Jump to

Keyboard shortcuts

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