VKoptev / go-errors

Errors wrapper for more convenience :)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-errors

GoDoc golangci-lint

Errors wrapper for more convenience :)

Usage

It's just a wrapper to faster and convenient write go-code. You can use it according to your imagination :)

To create error with reason:

ErrSomethingWentWrong := errors.WithReason("something went wrong")

return ErrSomethingWentWrong

To consider that this error is caused by another error:

return ErrSomethingWentWrong.WithErr(err)

To include data that leads to error:

return ErrSomethingWentWrong.WithX(data)

Bad data without reason and not being caused error (why not?):

errors.WithX(data)

Error builder:

package usefulnesses

import (
	"encoding/json"
	"net"

	"github.com/VKoptev/go-errors"
)

var (
	ErrSomethingWentWrong = errors.WithReason("something went wrong")
)

func Send(conn net.Conn, x interface{}) error {
	b, err := json.Marshal(x)
	if err != nil {
		return ErrSomethingWentWrong.WithErr(err).WithX(x)
	}

	if _, err := conn.Write(b); err != nil {
		return ErrSomethingWentWrong.WithErr(err).WithX(b)
	}

	return nil
}

Helpers

There are 2 useful functions: OneOf and EachOf

OneOf returns true if err is matched with at least one of specified errors:

var (
    errWentWrong = errors.WithReason("went wrong")
    errWentCompletelyWrong = errors.WithReason("went wrong")
)
...
if errors.OneOf(err, errWentWrong, errWentCompletelyWrong) {
    return
}

EachOf returns true if err is matched with each of specified errors:

var errWentWrong = errors.WithReason("went wrong")

func a() error {
    if condition {
        return errWentWrong
    }
    return errWentWrong.WithErr(io.EOF)
}
...
if errors.EachOf(a(), errWentWrong, io.EOF) {
    return
}

Aliases

To avoid importing builtin package two aliases is added:

  • Is(error, ...errors) - alias for builtin function, but it has such an interface as OneOf and EachOf. (!) Only first item of variadic argument is used.
  • As(error, interface{})

Links

Code example.

About

Errors wrapper for more convenience :)

License:MIT License


Languages

Language:Go 100.0%