serrors

package module
v2.0.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

serrors

GitHub release (latest SemVer) GitHub Workflow Status GitHub GitHub go.mod Go version

A Go library to create errors with stack traces.

Requirements

  • Go 1.24+

Installation

go get github.com/Siroshun09/serrors/v2

Usage

Creating errors
  • With message: serrors.New("msg")
  • With format and args: serrors.Errorf("msg: %s", "hello")
  • Wrap an existing error: serrors.WithStackTrace(err)
    • If err is nil, it returns nil.
    • If err already has a stack trace from this package, it returns err as-is.
Getting stack traces
  • serrors.GetStackTrace(err) returns a stack trace for err.
    • If err already has a stack trace attached (created by this package), it returns that.
    • Otherwise, it returns the current call site's stack trace.
    • If err is nil, it returns nil.
  • serrors.GetAttachedStackTrace(err) returns the attached stack trace and a bool.
    • The bool indicates whether err had an attached stack trace.
  • serrors.GetCurrentStackTrace() returns the current StackTrace.
Example
package main

import (
    "errors"
    "fmt"

    "github.com/Siroshun09/serrors/v2"
)

func main() {
    base := errors.New("base error")

    // Wrap with stack trace
    err := serrors.Wrap(base)

    // Retrieve the attached stack trace
    st, ok := serrors.GetAttachedStackTrace(err)
    if ok {
        fmt.Println("stack trace attached:")
        fmt.Println(st.String())
    }

    // Or always get a stack trace (attached or current)
    fmt.Println(serrors.GetStackTrace(err))
}
Interoperability
  • The wrapped error implements Unwrap() error, so it works with errors.Is and errors.As.
  • fmt.Errorf("...: %w", err) can be used in combination with these errors as usual.

License

This project is under the Apache License version 2.0. Please see LICENSE for more info.

Copyright © 2024-2026, Siroshun09

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAttrs

func GetAttrs(err error) iter.Seq2[error, slog.Attr]

func GetStackTraces

func GetStackTraces(err error) iter.Seq2[error, StackTrace]

GetStackTraces returns a sequence of errors and their associated StackTrace.

This function recursively returns errors and stack traces by repeating the following process:

1. If the given error has a StackTrace itself, this function returns the wrapped error and its StackTrace

  • In this case, the iterator returns only one error.

2. If the given error has an Unwrap() error function, this function calls it and tries to process step 1 again 3. If the given error has an Unwrap() []error function, this function calls it and tries to process step 1 for each error in the returned slice

func New

func New(msg string, attrs ...slog.Attr) error

New creates an error with a StackTrace and slog.Attr.

func WithMsg

func WithMsg(err error, msg string, attrs ...slog.Attr) error

WithMsg wraps an error with a message, StackTrace and slog.Attr.

func Wrap

func Wrap(err error, attrs ...slog.Attr) error

Wrap wraps an error with a StackTrace and slog.Attr.

Types

type Frame

type Frame struct {
	Function string
	File     string
	Line     int
}

func (Frame) AppendText

func (s Frame) AppendText(ret []byte) ([]byte, error)

func (Frame) String

func (s Frame) String() string

String formats Frame as "function (file:line)"

type StackTrace

type StackTrace []Frame

func GetAttachedStackTrace

func GetAttachedStackTrace(err error) (StackTrace, bool)

GetAttachedStackTrace returns the StackTrace if the given error has one.

The returned bool indicates whether the given error has a StackTrace.

func GetCurrentStackTrace

func GetCurrentStackTrace() StackTrace

GetCurrentStackTrace returns the current StackTrace.

func GetStackTrace

func GetStackTrace(err error) StackTrace

GetStackTrace returns a StackTrace for err.

If err does not have a StackTrace, this function creates the current StackTrace.

Also, if err is nil, this function returns nil.

func (StackTrace) AppendText

func (st StackTrace) AppendText(ret []byte) ([]byte, error)

func (StackTrace) String

func (st StackTrace) String() string

Jump to

Keyboard shortcuts

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