Popularity
4.4
Growing
Activity
8.1
Declining
115
2
5

Programming language: Go
Tags: Error Handling    
Latest version: v0.32.0

emperror alternatives and similar packages

Based on the "Error Handling" category

Do you think we are missing an alternative of emperror or a related project?

Add another 'Error Handling' Package

README

[Emperror](.github/logo.png?raw=true)

Mentioned in Awesome Go

GitHub Workflow Status Codecov Go Report Card GolangCI Go Version GoDoc FOSSA Status

The Emperor takes care of all errors personally.

Go's philosophy encourages to gracefully handle errors whenever possible, but some times recovering from an error is not.

In those cases handling the error means making the best effort to record every detail for later inspection, doing that as high in the application stack as possible.

This project provides tools to make error handling easier.

Read more about the topic here:

Features

Installation

go get emperror.dev/emperror

Usage

Log errors

Logging is one of the most common target to record error events.

Emperror has two logger integrations by default:

Annotate errors passing through an error handler

Emperror can annotate errors with details as defined in emperror.dev/errors

package main

import (
    "emperror.dev/emperror"
    "emperror.dev/errors"
)

func main() {
    handler := emperror.WithDetails(newHandler(), "key", "value")

    err := errors.New("error")

    // handled error will receive the handler details
    handler.Handle(err)
}

Panics and recovers

package main

import (
    "emperror.dev/emperror"
    "emperror.dev/errors"
)

func main() {
    var handler emperror.Handler = newHandler()

    // Recover from panics and handle them as errors
    defer emperror.HandleRecover(handler)

    // nil errors will not panic
    emperror.Panic(nil)

    // this will panic if foo returns with a non-nil error
    // useful in main func for initial setup where "if err != nil" does not make much sense
    emperror.Panic(foo())
}

func foo() error {
    return errors.New("error")
}

Filter errors

Sometimes you might not want to handle certain errors that reach the error handler. A common example is a catch-all error handler in a server. You want to return business errors to the client.

package main

import (
    "emperror.dev/emperror"
    "emperror.dev/errors/match"
)

func main() {
    var handler emperror.Handler = emperror.WithFilter(newHandler(), match.Any{/*any emperror.ErrorMatcher*/})

    // errors matching the provided matcher will not be handled
    handler.Handle(err)
}

Development

When all coding and testing is done, please run the test suite:

make check

License

The MIT License (MIT). Please see [License File](LICENSE) for more information.

FOSSA Status


*Note that all licence references and agreements mentioned in the emperror README section above are relevant to that project's source code only.