deeperr

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 5 Imported by: 0

README

deeperr

A lightweight Go error-wrapping library designed to provide stack traces and error codes while maintaining compatibility with the standard library

Usage

$ go get atomicptr.dev/deeperr
package main

import (
	"atomicptr.dev/deeperr"
)

// define your own error codes
const (
	ErrInternal deeperr.Code = 500
	ErrService  deeperr.Code = 503
	ErrAuth     deeperr.Code = 401
)

func main() {
	err := userController()
	deeperr.PrintStacktrace(err)
}

func userController() error {
	return deeperr.NewWithCode(ErrInternal, "controller: failed to resolve user", userService())
}

func userService() error {
	return deeperr.New("service: authentication layer failed", authProvider())
}

func authProvider() error {
	return deeperr.NewWithCode(ErrAuth, "provider: transient network failure", databaseLayer())
}

func databaseLayer() error {
	return deeperr.NewWithCode(ErrService, "database: connection pool exhausted", rootDriver())
}

func rootDriver() error {
	return deeperr.New("driver: socket hang up", nil)
}

this will now print:

E500 controller: failed to resolve user
    /home/christopher/dev/go/deeperr/example/example.go:20
service: authentication layer failed
    /home/christopher/dev/go/deeperr/example/example.go:24
E401 provider: transient network failure
    /home/christopher/dev/go/deeperr/example/example.go:28
E503 database: connection pool exhausted
    /home/christopher/dev/go/deeperr/example/example.go:32
driver: socket hang up
    /home/christopher/dev/go/deeperr/example/example.go:36

License

MIT

Documentation

Overview

Package deeperr is a lightweight Go error-wrapping library designed to provide stack traces and error codes

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(err error, code Code) bool

Contains checks the entire chain and returns true if ANY deeperr.Error in the hierarchy matches the error code

func GetStacktrace

func GetStacktrace(err error) string

GetStacktrace traverses the error chain and constructs a formatted string, representing the full execution path. for each error in the chain, it prints the error message and if the error is a deeperr.Error it will also print the location where the error was created.

func IsCode

func IsCode(err error, code Code) bool

IsCode checks if the first deeperr.Error in the chain has the error code

func PrintStacktrace

func PrintStacktrace(err error)

PrintStacktrace is a helper function that retrieves the full stack trace of an error and writes it directly to standard error (os.Stderr).

Types

type Code

type Code int

Code represents an error code

const CodeUnset Code = -1

CodeUnset means this error doesn't use a code

type Error

type Error interface {
	// Code returns the error code or `deeperr.CodeUnset`
	Code() Code

	// Message returns the error message
	Message() string

	// Error returns the error code and the error message
	Error() string

	// Unwrap returns the next error in the chain
	Unwrap() error

	// Location returns the location where the error was created
	Location() (string, int)
}

Error is an error type with stack traces and error code tagging capabilities

func As added in v0.2.0

func As(err error) (Error, bool)

As transforms the first error in the chain into `deeperr.Error` and returns it and true, returns false otherwise

func New

func New(message string, prev error) Error

New returns a new Error

func NewWithCode

func NewWithCode(code Code, message string, prev error) Error

NewWithCode returns an Error with the associated code

Jump to

Keyboard shortcuts

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