errorutil

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

README

errorutil

This is a fork of mathpresso/go-utils/errorutil with some fixes and improvements.


Go Reference

Package errorutil provides simple error wrapper for some features. Inspired by https://github.com/pkg/errors

Currently, errorutil provides error chaining mechanism with hierachy, and auto stacktrace binding.


Installation

$ go get https://github.com/coxwave/go-errorutil

Documentation

Overview

Package errorutil provides simple error wrapper for some features. Inspired by https://github.com/pkg/errors

Currently, errorutil provides error chaining mechanism with hierachy, and auto stacktrace binding.

Example (Nested)

If you want to set some cause-error for your error, simply use .FromCause() option

_ = func() error {
	ErrStatic := errors.New("static error")
	causeErr := errors.New("cause error")
	return errorutil.Wrap(ErrStatic, errorutil.FromCause(causeErr))
}
Example (Simple)

If you want to just wrap error with stack trace, simply wrap your error with .Wrap()

_ = func() error {
	err := errors.New("some error")
	return errorutil.Wrap(err)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func OriginalErr added in v0.2.1

func OriginalErr(err error) error

OriginalErr returns the original error without any wrapping

func Wrap

func Wrap(err error, opts ...WrapOpt) error

Wrap wraps the error with provided options.

Types

type Frame

type Frame uintptr

Frame represents a program counter inside a stack Frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

func (Frame) Format

func (f Frame) Format(s fmt.State, verb rune)

Format formats the Frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   function name and path of source file relative to the compile time
      GOPATH separated by \n\t (<funcname>\n\t<path>)
%+v   equivalent to %+s:%d

func (Frame) MarshalText

func (f Frame) MarshalText() ([]byte, error)

MarshalText formats a stacktrace Frame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format

func (st StackTrace) Format(s fmt.State, verb rune)

Format formats the stack of Frames according to the fmt.Formatter interface.

%s	lists source files for each Frame in the stack
%v	lists the source file and line number for each Frame in the stack

Format accepts flags that alter the printing of some verbs, as follows:

%+v   Prints filename, function, and line number for each Frame in the stack.

type WrapOpt

type WrapOpt func(w *wrapped)

func AutoStackTrace

func AutoStackTrace() WrapOpt

AutoStackTrace is the option which automatically bind caller's stacktrace to error. This makes some error-capturing module (like https://github.com/getsentry/sentry-go) can extract proper stacktrace of your error. For convenience, this option is enabled by default even if you don't include it.

func FromCause

func FromCause(cause error) WrapOpt

FromCause is the option which wraps the error with provided cause. If you Unwrap this error, provided cause will be extracted.

Jump to

Keyboard shortcuts

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