Skip to content
The Emperor takes care of all errors personally
Go Makefile
Branch: master
Clone or download
Latest commit e0511c6 Aug 8, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Improve circle config Aug 8, 2019
.github Initial commit Jun 13, 2017
.idea Move to go modules and circleci from dep and travis Mar 30, 2019
.vscode Update import name Jul 3, 2019
httperr Deprecate httperr package Jul 18, 2019
utils/keyvals Deprecate keyvals.ToMap Jul 18, 2019
.editorconfig Upgrade linter and code formatting rules Mar 30, 2019
.gitignore Move to go modules and circleci from dep and travis Mar 30, 2019
.golangci.yml Improve golangci config Aug 8, 2019
CHANGELOG.md Prepare release 0.21.3 Jul 30, 2019
LICENSE Initial commit Jun 13, 2017
Makefile Update makefile Aug 8, 2019
README.md Add coverage Jul 23, 2019
emperror.go Add canonical import path Jul 20, 2019
error_context.go Deprecate With functions Jul 17, 2019
error_context_test.go Fix tests on Go 1.13 Aug 8, 2019
error_multi.go Upgrade errors package Jul 13, 2019
error_multi_test.go Rename error related files Jul 7, 2019
error_stack.go Use new errors package Jul 12, 2019
error_stack_test.go Fix tests on Go 1.13 Aug 8, 2019
go.mod Deprecate keyvals.ToMap Jul 18, 2019
go.sum Deprecate keyvals.ToMap Jul 18, 2019
handler.go Add Close function to Handlers Jul 30, 2019
handler_composite.go Reduce the number of error handling related files Jul 7, 2019
handler_composite_test.go Remove testify library dependency Mar 30, 2019
handler_context.go Add WithDetails function Jul 18, 2019
handler_context_test.go Deprecate With functions Jul 17, 2019
handler_details.go Add WithDetails function Jul 18, 2019
handler_details_test.go Add WithDetails function Jul 18, 2019
handler_test.go Add Close function to Handlers Jul 30, 2019
handler_testing.go Refactor the test handler Dec 24, 2018
handler_testing_test.go Remove testify library dependency Mar 30, 2019
panic.go Fix tests on Go 1.11 Aug 8, 2019
panic_go1_11.go Fix tests on Go 1.11 Aug 8, 2019
panic_go1_12.go Fix tests on Go 1.11 Aug 8, 2019
panic_test.go Use new errors package Jul 12, 2019
unwrap.go Upgrade errors package Jul 13, 2019
unwrap_test.go Upgrade errors package Jul 13, 2019
wrap.go Deprecate With functions Jul 17, 2019

README.md

Emperror

CircleCI Coverage Go Report Card GolangCI GoDoc

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")
}

Development

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

$ make check

License

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

You can’t perform that action at this time.