serrors

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2021 License: MIT Imports: 4 Imported by: 1

README

go-errors

Why this repo was created?

Main reason was to create additional methods for better error typing support.

Features

  • Errors as constants
  • errors.Is support
  • Wrap method to wrap original error with errors.Unwrap method
  • String.New support to add context arguments for error message, while errors.Is still compares error itself
  • Error.WithStack support to store stack trace at time method called
Show me the code

https://play.golang.org/p/U2lC-yC2YIb

package main

import (
	"errors"
	"log"

	serr "github.com/bdandy/go-errors"
)

const ErrSomeFunc = serr.String("somefunc for %s failed")

func someFunc() error {
	return errors.New("io error")
}

func funcWithArgs(args ...interface{}) error {
	err := someFunc()
	if err != nil {
		return ErrSomeFunc.New(args...).Wrap(err)
	}
	return nil
}

func main() {
	err := funcWithArgs("tryme!")

	// handle ErrSomeFunc error type
	if errors.Is(err, ErrSomeFunc) {
		log.Print("typedError handled: ", err)
	} else if err != nil {
		log.Print("other error cases:", err)
	}
}

Benchmark

Comparsion with errors.Errorf and pkg/errors

goos: linux
goarch: amd64
pkg: github.com/bdandy/go-errors
cpu: Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
BenchmarkWrap-8                  	 5007164               270.9 ns/op           136 B/op          6 allocs/op
BenchmarkWrapWithStack-8         	 1232276               947.3 ns/op           392 B/op          7 allocs/op
BenchmarkErrorfWrap-8                    4218820               284.9 ns/op           64 B/op           3 allocs/op
BenchmarkPkgErrorWrap-8                  1376254               858.2 ns/op           368 B/op          6 allocs/op
BenchmarkPkgErrorWrapWithStack-8          781378               1593 ns/op            672 B/op          9 allocs/op
PASS

Documentation

Overview

Package serrors provides better sentinel errors support

Index

Constants

This section is empty.

Variables

View Source
var WrapSeparator = ": "

Functions

func Stack

func Stack(err error) string

Stack returns stack if err is wrapped or implements Stack() string method

func Wrap

func Wrap(err, cause error) wrapped

Wrap wraps an error, adds its cause and stack trace

func WrapWithStack

func WrapWithStack(err, cause error) wrapped

WrapWithStack wraps cause with err and stores stack trace

Types

type Comparer

type Comparer interface {
	error
	Is(err error) bool // Is checks if Comparer and provided error has same type
}

type Error

type Error interface {
	Comparer
	Wrap(cause error) wrapped // Wrap wraps provided error
	WithStack() wrapped       // Stack stores call stack and returns wrapped error
}

type String

type String string

String is only one public type which should be used in const as error

func (String) Error

func (e String) Error() string

Error implements error interface

func (String) New

func (e String) New(args ...interface{}) sentinelError

New returns new sentinel error. We assume that New() called only once per error Note: if two errors has different arguments but same type `errors.Is` will return true

func (String) String

func (e String) String() string

String implements fmt.Stringer interface

type Wrapped

type Wrapped interface {
	Unwrap() error
}

Jump to

Keyboard shortcuts

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