All Projects → therne → errorist

therne / errorist

Licence: MIT license
Utilities for coping with errors and panics like a boss in Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to errorist

Leaf
Lightweight Error Augmentation Framework
Stars: ✭ 201 (+957.89%)
Mutual labels:  error-handling
Poica
🧮 A research programming language on top of C macros
Stars: ✭ 231 (+1115.79%)
Mutual labels:  error-handling
errors
errors with paired message and caller stack frame
Stars: ✭ 19 (+0%)
Mutual labels:  error-handling
Errors Spring Boot Starter
Elegant Error Handling for Spring Boot
Stars: ✭ 209 (+1000%)
Mutual labels:  error-handling
Tcso
Try Catch Stack overflow (TcSo) Is a collection of Try statements in all the programming languages under the globe which catches the exception and searches for the cause of the caught exception in the stack overflow automatically.
Stars: ✭ 228 (+1100%)
Mutual labels:  error-handling
Await Of
await wrapper for easier errors handling without try-catch
Stars: ✭ 240 (+1163.16%)
Mutual labels:  error-handling
Fstoolkit.errorhandling
An opinionated F# Library for error handling
Stars: ✭ 200 (+952.63%)
Mutual labels:  error-handling
catchr
catchr: Flexible, useful tools for dealing with conditions in R, for new users and veterans
Stars: ✭ 17 (-10.53%)
Mutual labels:  error-handling
Grace
Handle Go recover, panic, and errors in a graceful way. Multiple errors support, basic filters and custom handlers.
Stars: ✭ 229 (+1105.26%)
Mutual labels:  error-handling
cakephp-error-email
ErrorEmail Plugin for CakePHP3.x
Stars: ✭ 16 (-15.79%)
Mutual labels:  error-handling
Ultracopier
Ultracopier acts as a replacement for files copy dialogs. Features: play/pause, speed limitation, on-error resume, error/collision management ...
Stars: ✭ 220 (+1057.89%)
Mutual labels:  error-handling
Sneaker
An easy way to send emails whenever an exception occurs on server.
Stars: ✭ 223 (+1073.68%)
Mutual labels:  error-handling
teapot
Utilities for working with HTTP status codes, errors, and more
Stars: ✭ 14 (-26.32%)
Mutual labels:  error-handling
Bugsnag Ruby
Bugsnag error monitoring & reporting software for rails, sinatra, rack and ruby
Stars: ✭ 211 (+1010.53%)
Mutual labels:  error-handling
fail
Better error handling solution specially designed for web application servers
Stars: ✭ 27 (+42.11%)
Mutual labels:  error-handling
Emperror
The Emperor takes care of all errors personally
Stars: ✭ 201 (+957.89%)
Mutual labels:  error-handling
Webpack Messages
Beautifully format Webpack messages throughout your bundle lifecycle(s)!
Stars: ✭ 238 (+1152.63%)
Mutual labels:  error-handling
ddderr
👺 Reflection-free Domain-Driven errors for Go.
Stars: ✭ 29 (+52.63%)
Mutual labels:  error-handling
laravel-ignition
A beautiful error page for Laravel apps
Stars: ✭ 276 (+1352.63%)
Mutual labels:  error-handling
hutplate
A Go library over standard net/http library with auth, session, err handling and more.
Stars: ✭ 28 (+47.37%)
Mutual labels:  error-handling

errorist

Godoc Reference MIT License Badge

Package errorist provides useful error handling utilities inspired by Thanos Coding Style Guide and Uber go style guide.

Closing Resources

errorist provides hassle-free functions closing io.Closer with defer.

With Error Capture

The most recommended way is using error capture. An error caused by Close will be captured on the given error pointer unless it already has an error as the value.

func printFile(path string) (err error) {
    f, err := os.OpenFile(path)
    if err != nil {
        return errors.Wrapf(err, "open %s", path)
    }
    defer errorist.CloseWithErrCapture(f, &err)
    ...
}

With Error Channel

An error also can be captured and sent to an error channel (chan error). It is a good fit with resources in goroutines.

errChan := make(chan error)
go func() {
    f, err := os.OpenFile(path)
    defer errorist.CloseWithErrChan(f, errChan)
}()

With Error Log

Otherwise, why don't we just log and ignore it? Default logger is log.Println but you are able to customize it with options.

defer errorist.CloseWithLogOnErr(f)

// with logrus
defer errorist.CloseWithLogOnErr(f, errorist.LogWithLogrus(logger.Warn))

Adding Contexts with Error Wrapping

If you're familiar with errors.Wrap or fmt.Errorf, you may want to do the same error handling with errorist. errorist provides the option wrapping errors with context.

func printFile(path string) (err error) {
    f, err := os.OpenFile(path)
    if err != nil {
        return errors.Wrapf(err, "open %s", path)
    }
    defer errorist.CloseWithErrCapture(f, &err, errorist.Wrapf("close %s", path))
    ...
}

Note that errorist.Wrapf is just an option specifier for errorist; it cannot be used solely. If you want to just wrap errors, you can use pkg/errors or fmt.Errorf with %w pattern added in Go 1.13.

Recovering from Panics

errorist provides panic recovery functions that can be used with defer.

wg, ctx := errgroup.WithContext(context.Background())
wg.Go(func() (err error) {
    defer errorist.RecoverWithErrCapture(&err)
    ...
})

Stacktrace is prettified, and calls from non-project source will be filtered by default. You can customize stacktrace format with options. For details, please refer options.go.

fmt.Println("%+v", err)
panic: assignment to entry in nil map
    github.com/therne/errorist.panicStation (panic_test.go:67)
    github.com/therne/errorist.TestWrapPanicWith.func1.1.1 (panic_test.go:19)
    github.com/therne/errorist.TestWrapPanicWith.func1.1 (panic_test.go:13)
    github.com/therne/errorist.TestWrapPanicWith.func1 (panic_test.go:12)
    github.com/therne/errorist.TestWrapPanicWith (panic_test.go:11)

Prettifying Stacktraces on Errors

pkg/errors is the most popular and powerful tool for handling and wrapping errors. errorist provides extracting and prettifying a stacktrace from errors created or wrapped by pkg/errors.

errorist.Stacktrace(err) ==
    []string{
        "github.com/some/app/api/Server.Handle (api.go:84)",
        "github.com/some/app/controller/Controller.Get (controller.go:11)",
    }

The below example shows how to use it in API responses for debugging purposes.

func handleGinError(c *gin.Context) {
    c.Next()
    if len(c.Errors) > 0 {
        err := c.Errors.Last().Err
        c.JSON(500, gin.H{
            "error":       err.Error(),
            "stacktraces": errorist.Stacktrace(err)
        })
    }
}

Options

You can use global options, package-wide, or with call arguments. Options set on a smaller scope can override options set on a wider scope.

errorist.SetGlobalOptions(
    errorist.IncludeNonProjectFiles(),
    errorist.WithDetailedTrace(),
)

// package-local options can override global options
errorist.SetPackageLevelOptions(
    errorist.IncludedPackages("github.com/my/pkg"),
    errorist.LogWithLogrus(pkgLogger),
)

// call options can override options above
errorist.CloseWithErrCapture(f, &err, errorist.Wrapf("close %s", path))

For detailed options, please refer Godoc or options.go.

License: MIT
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].