typed_errors

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: MIT Imports: 4 Imported by: 0

README

go-typed-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
  • xerrors.Wrapper support with Unwrap method
  • String.NewWithArgs support to add context arguments for error message, while errors.Is working
  • String.NewWithStack support to store stack trace (untested)
  • IDE highlighting, as type based on strings
Show me the code

https://play.golang.org/p/ZSHBCxXQx6A

package main

import (
	"errors"
	typedErrors "github.com/Bogdan-D/go-typed-errors"
	"log"
)

const ErrWrongBehaviour = typedErrors.String("wrong behaviour: %v")

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

func typedError(args ...interface{}) error {
	err := someFunc()
	if err != nil {
		return ErrWrongBehaviour.NewWithArgs(args...).Wrap(err)
	}
	return nil
}

func main() {
	err := typedError("not typed errors")

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

Benchmark

Comparsion Wrap with Errorf for wrapping errors

goos: linux
goarch: amd64
pkg: github.com/Bogdan-D/go-typed-errors
cpu: Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
BenchmarkWrap-8                  7856391               180.7 ns/op            88 B/op          5 allocs/op
BenchmarkErrorfWrap-8            4080972               291.3 ns/op            64 B/op          3 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Stack added in v0.1.0

func Stack(err error) string

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

func Wrap added in v0.0.3

func Wrap(err, cause error) (e wrapped)

Wrap wraps an error, adds its cause and stack trace

func WrapWithStack added in v0.1.0

func WrapWithStack(err, cause error) wrapped

WrapWithStack wraps cause with err and stores stack trace

Types

type ErrorString added in v0.1.0

type ErrorString = String

type String

type String string

String which used in const as error

func (String) Error

func (e String) Error() string

Error implements errors interface

func (String) New added in v0.1.0

func (e String) New() typedError

New returns new typed error

func (String) NewWithArgs added in v0.1.0

func (e String) NewWithArgs(args ...interface{}) formattedError

NewWithArgs returns new error which would be formattedError Note: if two errors has different arguments they still would be equal Used Sprintf for formatting, so use #Wrap method instead of %w for wrapping

func (String) String

func (e String) String() string

typedError implements stringer interface

type TypedError

type TypedError interface {
	error

	Is(err error) bool        // Is checks if TypedError and provided error has same type
	Wrap(cause error) wrapped // Wrap wraps provided error
	WithStack() wrapped       // Stack stores call stack and returns wrapped error
}

type Wrapped added in v0.1.0

type Wrapped interface {
	Unwrap() error
}

Jump to

Keyboard shortcuts

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