Categorygithub.com/safeblock-dev/werr
modulepackage
0.1.0
Repository: https://github.com/safeblock-dev/werr.git
Documentation: pkg.go.dev

# README

Highlights

The werr library provides efficient error wrapping capabilities for Go. It is designed with a focus on performance and enables the recording of functions where an error occurred. Here are some key features of the library:

  • Recording information about functions that triggered the error for easy debugging.
  • Support for custom error messages to make errors more informative.
  • Very high performance compared to other error-wrapping libraries.

Introduction

When handling errors in Go, simplicity and performance matter. werr offers a streamlined approach:

return werr.Wrap(err)

This captures the context of where the error originated, aiding in quick debugging.

werr

To use werr in your Go application, simply import it:

import "github.com/safeblock-dev/werr"

Features

  • Error Creation: Create errors using errors.New("error message") as usual.
  • Error Wrapping: Enhance errors with context using werr.Wrap(err).
  • Custom Messages: Add custom messages to errors with werr.Wrapf(err, "custom error message").
  • Error Unwrapping: Retrieve the original error with werr.Unwrap(err) for seamless error propagation.
  • Full Unwrapping: Get the root cause of wrapped errors with werr.UnwrapAll(err).
  • Direct Cause: Identify the immediate cause of an error with werr.Cause(err).
  • Typed Assertion: Check error types with werr.AsWrap(err) for precise error handling.

Example

package main

import (
	"errors"
	"fmt"

	"github.com/safeblock-dev/werr"
)

var errExample = errors.New("find me")

func main() {
	err := example()
	if errors.Is(err, errExample) {
		fmt.Printf("trace: \n%v\n", err)
		fmt.Printf("\nunwrap: %v\n", werr.Unwrap(err))
	}
}

func example() error {
	return werr.Wrap(example2())
}

func example2() error {
	return werr.Wrapf(example3(), "without if")
}

func example3() error {
	if err := newError(); err != nil {
		return werr.Wrapf(err, "wow error!")
	}

	return nil
}

func newError() error {
	return errExample
}

Result

trace: 
main/main.go:21 example()
main/main.go:25 example2()      without if
main/main.go:30 example3()      wow error!
find me

unwrap: find me

Stack Traces Benchmark

Performance benchmarks showcase werr's efficiency in error handling:

Result sample, MacBook Air M1 @ 3.2GHz:

namerunsns/opnote
BenchmarkSimpleError103925219228.67simple error, 10 frames deep
BenchmarkWrapError102190848543.0with wrap error
BenchmarkWrapMsgError101881180588.4with message
BenchmarkErrorxError109699311365errorx library
BenchmarkGoErrorsError1010000001171go-errors library
BenchmarkSimpleError1001911195633.1simple error, 100 frames deep
BenchmarkWrapError10010000001276with wrap error
BenchmarkWrapMsgError1009789681250with message
BenchmarkErrorxError1003127573890errorx library
BenchmarkGoErrorsError1005197402407go-errors library
BenchmarkSimpleErrorPrint1001721454695.1simple error, 100 frames deep, format output
BenchmarkWrapErrorPrint1008562661384with wrap error
BenchmarkWrapMsgErrorPrint1008344801441with message
BenchmarkErrorxErrorPrint1003894030826errorx library, format output
BenchmarkGoErrorsErrorPrint1005053802376go-errors library, format output

Key takeaways:

  • werr provides efficient error creation and wrapping with minimal overhead.
  • Use werr.Unwrap(err) to retrieve the original error for seamless propagation.
  • Enhance error context with custom messages using werr.Wrapf(err, "custom message").
  • Identify the immediate cause of an error with werr.Cause(err) for precise error handling.
  • Verify error types with werr.AsWrap(err) to handle errors based on specific types.

More

Portions of the description and benchmark were adapted from the project errorx.


This update integrates the new functionality descriptions and benchmarks, ensuring clarity and interest in error handling with the werr library.

# Functions

Arg is syntactic sugar.
Args is syntactic sugar.
Cause returns the root cause of the error if it is an Error.
IsWrap checks if the provided error is of type Error.
PanicToError converts a recovered panic to an error.
SetFormatter allows setting a custom error formatting function.
Unwrap retrieves the underlying error wrapped by the provided error.
UnwrapAll recursively unwraps an error that implements the Unwrap() method, effectively retrieving the root cause error from a chain of wrapped errors.
Wrap takes an error and returns a new wrapped error.
Wrapf takes an error, a format string, and optional arguments, and returns a new wrapped error.
Wrapt takes a value, an error and returns a new wrapped error.

# Structs

Error represents an error with additional context such as funcName, file, line, and msg.

# Type aliases

FormatFn defines a function signature for custom error formatting.