errz

package module
v0.0.0-...-f997ca8 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2021 License: MIT Imports: 7 Imported by: 34

README

GoDoc Go Report Card

errz

Package errz allows you to handle errors in one line of code.

You should use errz when you want to..
  • ..easily detect & find memory leaks like a nil pointer dereference
  • ..give context to your errors trough stack traces
  • ..avoid the verbose but idiomatic error handling pattern if err != nil { return err }
When should i not use errz?

If you write algorithms, modules or libraries with high performance in mind.

--

The name of this package was inspired by Marcel van Lohuizen.
It has a dependency on github.com/pkg/errors

Using errz

Logging

When you want to log/report an error.

err := foo()
errz.Log(err)

This is useful during development and mostly used at the top level of an application

Return on error

It means stopping further code execution and returning to the calling function. panic/recover is used to stop code execution when a error occurred.

func bar() (err error){
  defer errz.Recover(&err) //recover all panics

  err = foo()
  errz.Fatal(err) //panics on error

  //Some pieces of code.
  //Not executed when foo() returned a error
  //..
  //..

  return err
}

This pattern will not just handle errors. It will also handle memory corruptions, turns them into an error, adds a stack trace to it and returns the error. This can prevent you from extensive debugging sessions.

License

MIT

Documentation

Overview

Package errz provides highlevel error handling helpers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fatal

func Fatal(err error)

Fatal adds a stack trace to the error if needed and panics.

func Fatalf

func Fatalf(err error, format string, args ...interface{})

FatalF adds a stack trace and an addtional message using printf semantics to the error and panics.

func Fatalm

func Fatalm(err error, msg string)

Fatalm adds a stack trace with an additonal message to the error and panics.

func Log

func Log(err error)

Log an error + stack trace to the underlying logger. Usually used at the top level of a application to log errors without handling them.

func Recover

func Recover(errs ...*error)

Recover recovers a panic usually introduced by Fatal or a memory corruption. In the case of a plain error Recover decorates the error with a stacktrace. When a error pointer is passed `Recover(&err) the error is writted to the error pointer, and therefore returned to the calling function. In the case of `Recover()` called without args the error is logged to the underlying logr implementation.

Example:

func myfunc() (err error) {
  defer errz.Recover(&err)
  ...
  ...
}

func WithLogger

func WithLogger(l logr.Logger)

WithLogger set a package wide logger. Take a look at https://github.com/go-logr/logr for a list of popular logger implemetations.

Types

This section is empty.

Jump to

Keyboard shortcuts

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