Giter Club home page Giter Club logo

errors's Introduction

errors Go Reference codecov

Fork of xerrors with explicit Wrap instead of %w.

Clear is better than clever.

go get github.com/go-faster/errors
errors.Wrap(err, "message")

Why

  • Using Wrap is the most explicit way to wrap errors
  • Wrapping with fmt.Errorf("foo: %w", err) is implicit, redundant and error-prone
  • Parsing "foo: %w" is implicit, redundant and slow
  • The pkg/errors and xerrors are not maintainted
  • The cockroachdb/errors is too big
  • The errors has no caller stack trace

Don't need traces?

Call errors.DisableTrace or use build tag noerrtrace.

Additional features

Into

Generic type assertion for errors.

// Into finds the first error in err's chain that matches target type T, and if so, returns it.
//
// Into is type-safe alternative to As.
func Into[T error](err error) (val T, ok bool)
if pathError, ok := errors.Into[*os.PathError](err); ok {
    fmt.Println("Failed at path:", pathError.Path)
}

Must

Must is a generic helper, like template.Must, that wraps a call to a function returning (T, error) and panics if the error is non-nil.

func Must[T any](val T, err error) T

License

BSD-3-Clause, same as Go sources

errors's People

Contributors

cuonglm avatar dependabot[bot] avatar ernado avatar jba avatar mpvl avatar neild avatar peczenyj avatar rsc avatar sashamelentyev avatar tdakkota avatar zchee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

errors's Issues

wrong implementation of Join function for older go versions

Hello

errors/join.go

Line 54 in 10bc51a

return unsafe.String(&b[0], len(b))

this will not work for go versions older than 1.20

the reason is: unsafe.String was added on 1.20

this code will not compile on go 1.19, for instance

an alternative is to use unsafe.Pointer like in the example below

https://github.com/valyala/fasthttp/blob/master/b2s_old.go

however there is another issue: the signature of Unwrap method uses a new alternative format (when return an slice of errors)

errors/join.go

Line 57 in 10bc51a

func (e *joinError) Unwrap() []error {

but for older versions of go this Unwrap signature:

errors/wrap.go

Line 40 in 10bc51a

func Unwrap(err error) error {

will use the standard errors package that does not support it.

IMHO we should drop this implementation (or specify an Unwrap function for older versions that supports both signatures) since the go.mod specifies go 1.20

and change the Join implementation for newer version of go to be a function that delegate the call to errors package like we do for Unwrap instead specify a public global variable that points to the Join function.

Compare with thanos errors

https://github.com/thanos-io/thanos/tree/main/pkg/errors is a light replacement of pkg/errors for error wrapping.

feat: allow colored formatting

Use github.com/fatih/color, highlight error text, stacktrace parts (line number, path to file, stacktrace element) with different colors and bold/faint.

Example of colored formatted string:

fmt.Println(
	color.New(color.FgCyan).Sprintf("[%5s]", time.Since(start).Round(time.Second)),
	color.New().Sprint(req.Method),
	color.New(color.Faint).Sprint(req.URL),
	color.New(color.FgYellow).Sprint(lastStatus),
)

Probably implement it as some variant of %+v formatting (do we support flags in formatting strings here?).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.