terr

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 5 Imported by: 0

README

terr

A traceable error library for golang.

Example

import (
    "net/http"

    "github.com/deadblue/terr"
)

func loadData() (err error) {
    resp, err := terr.TraceError(http.Get("http://example.com/"))
    if err != nil {
        return
    }
    defer resp.Close()
    // TODO: Parse resp
}

func divide(m, n int) (err error) {
    if (n == 0) {
        err = terr.New("can not divide by zero")
    }
    return
}

func main() {
    loadData()
    divide(5, 0)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errorf added in v0.0.3

func Errorf(format string, a ...any) error

Errorf makes a TracedError with a formatted error.

func New

func New(message string) error

New makes a TracedError with specific message.

func Trace

func Trace(base error) error

Trace makes a TracedError whose original error is |base|.

When |base| is already a TracedError, it will be returned directly.

func TraceError

func TraceError[V any](val V, err error) (V, error)

TraceError wraps |err| to TracedError when it is not nil.

When |base| is already a TracedError, it will be returned directly.

Example:

resp, err := terr.TraceError(http.Get("https://example.com"))

func Wrap

func Wrap(message string, cause error) error

Wrap makes a TracedError whose original error is wrapped from cause with message.

When |cause| is a TracedError, its frames will be copied to new error.

Types

type FrameLite

type FrameLite struct {
	// PC is the program counter for the location in this frame.
	PC uintptr
	// Func is the function name when available.
	Func string
	// File is the name of source file when available.
	File string
	// Line is the line number in source file when available.
	Line int
}

FrameLite is a lite version of runtime.Frame.

func (*FrameLite) String

func (f *FrameLite) String() string

type TracedError

type TracedError struct {
	// contains filtered or unexported fields
}

TracedError represent an error with calling stack.

func (*TracedError) Error

func (e *TracedError) Error() string

Error returns error message with calling stack.

func (*TracedError) Stack

func (e *TracedError) Stack() iter.Seq[FrameLite]

Stack returns calling stack.

func (*TracedError) Unwrap

func (e *TracedError) Unwrap() error

Unwrap returns the original error.

Jump to

Keyboard shortcuts

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