All Projects → icelolly → go-errors

icelolly / go-errors

Licence: MIT License
Flexible, general-purpose error handling for Go.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-errors

Traceback with variables
Adds variables to python traceback. Simple, lightweight, controllable. Debug reasons of exceptions by logging or pretty printing colorful variable contexts for each frame in a stacktrace, showing every value. Dump locals environments after errors to console, files, and loggers. Works in Jupyter and IPython. Install with pip or conda.
Stars: ✭ 509 (+2894.12%)
Mutual labels:  stacktrace, errors, error-handling
Errors
Drop-in replacement for the standard library errors package and github.com/pkg/errors
Stars: ✭ 88 (+417.65%)
Mutual labels:  stacktrace, errors, error
Bugsnag Node
[DEPRECATED] Please upgrade to our Universal JS notifier "@bugsnag/js" • https://github.com/bugsnag/bugsnag-js
Stars: ✭ 48 (+182.35%)
Mutual labels:  errors, error-handling, error
Tracerr
Golang errors with stack trace and source fragments.
Stars: ✭ 646 (+3700%)
Mutual labels:  stacktrace, errors, error-handling
errors
errors with paired message and caller stack frame
Stars: ✭ 19 (+11.76%)
Mutual labels:  stacktrace, errors, error-handling
Bugsnag Go
Automatic panic monitoring for Go and Go web frameworks, like negroni, gin, and revel
Stars: ✭ 155 (+811.76%)
Mutual labels:  errors, error-handling, error
failure
An error handling package for Go.
Stars: ✭ 24 (+41.18%)
Mutual labels:  errors, error-handling, error
rust-error-handle
detail rust error handle
Stars: ✭ 47 (+176.47%)
Mutual labels:  error-handling, error
Tslog
📝 tslog - Expressive TypeScript Logger for Node.js.
Stars: ✭ 321 (+1788.24%)
Mutual labels:  stacktrace, error-handling
Panic Overlay
Displays JS errors in browsers. Shows sources. Use with any framework. 💥✨
Stars: ✭ 50 (+194.12%)
Mutual labels:  stacktrace, error-handling
Node Common Errors
Common error classes and utility functions
Stars: ✭ 247 (+1352.94%)
Mutual labels:  errors, error
fail
Better error handling solution specially designed for web application servers
Stars: ✭ 27 (+58.82%)
Mutual labels:  errors, error-handling
bugsnag-java
Bugsnag error reporting for Java.
Stars: ✭ 51 (+200%)
Mutual labels:  errors, error-handling
ErrorLayout
Simple layout to show custom error toast with animation
Stars: ✭ 13 (-23.53%)
Mutual labels:  error-handling, error
Bugsnag React Native
Error monitoring and reporting tool for native exceptions and JS errors in React Native apps
Stars: ✭ 374 (+2100%)
Mutual labels:  stacktrace, error-handling
koa-better-error-handler
A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
Stars: ✭ 51 (+200%)
Mutual labels:  error-handling, error
TrackJS-Node
TrackJS Error Monitoring agent for NodeJS
Stars: ✭ 26 (+52.94%)
Mutual labels:  error-handling, error
jsonerror
Makes Go error-handling a breeze!
Stars: ✭ 28 (+64.71%)
Mutual labels:  errors, error-handling
Whoops
PHP errors for cool kids
Stars: ✭ 12,646 (+74288.24%)
Mutual labels:  errors, error-handling
Bugsnag Ruby
Bugsnag error monitoring & reporting software for rails, sinatra, rack and ruby
Stars: ✭ 211 (+1141.18%)
Mutual labels:  errors, error-handling

go-errors Workflow Badge Go Report Card Badge GoDoc Badge

This package aims to provide flexible general-purpose error handling functionality, with the following specific features in mind:

  • Error wrapping: Allowing an error's cause to be preserved along with additional information.
  • Stack tracing: Allowing the path taken to return an error to be easily identified.
  • Structured fields: Allowing errors to be logged with additional contextual information.

This library has built upon the mistakes we've made and lessons we've learnt with regards to error handling at Icelolly whilst working on our internal APIs. This library was inspired by approaches found elsewhere in the community, most notably the approach found in Upspin.

Usage

Creating and Wrapping Errors

Errors may have a few different pieces of information attached to them; an errors.Kind, a message, and fields. All of these things are optional, but at least an errors.Kind or message must be given if using errors.New. Along with this information, file and line information will be added automatically. If you're wrapping an error, the only thing you must pass is an error to wrap as the first argument:

const ErrInvalidName errors.Kind = "auth: user's name is invalid"

func persistUser(user *User) error {
    if user.Name == "" {
        // Error kinds like `ErrInvalidName` can be used to react to different
        // errors to decide how to handle them at the caller.
        return errors.New(ErrInvalidName)
    }

    err := persist(user)
    if err != nil {
        // Wrapping errors let's you add contextual information which may be
        // extracted again later (e.g. for logging).
        return errors.Wrap(err, "auth: failed to persist user in DB").
            WithField("user", user)
    }

    return nil
}

Handling Errors

Most error handling is done using errors.Is, which checks if the given error is of a given errors.Kind. If the error doesn't have it's own errors.Kind, then errors.Is will look through the errors stack until it finds an errors.Kind:

func registerUserHandler(w http.ResponseWriter, r *http.Request) {
    user := // ...

    err := persistUser(user)
    switch {
    case errors.Is(err, ErrInvalidName):
        http.Error(w, "user has invalid username", 400)
        return
    case err != nil:
        http.Error(w, http.StatusText(500), 500)
        return
    }

    // ...
}

A more thorough example of usage can be found in the example/ directory. It showcases creating errors, wrapping them, handling different kinds of errors, and dealing with things like logging.

See More

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].