errors

package module
v2.8.3 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 4 Imported by: 15

README

go-errors.

GoReference

This package can be used as drop-in replacement for standard errors package.

This package provide func StackTrace(error) []trace.Location to get the stack trace.

Stack trace can be attached to any error by passing it to func Trace(error) error.

New, and Errorf function will return error that have stack trace.

Documentation

Overview

Package errors.

This package can be used as drop-in replacement for standard errors package.

This package provide StackTrace function to get the stack trace.

Stack trace can be attached to any error by passing it to Trace function.

Example
package main

import (
	"bytes"
	"fmt"
	"os"
	"strings"

	"github.com/payfazz/go-errors/v2"
)

func readFile() (string, error) {
	data, err := os.ReadFile("InvalidFile.txt")
	if err != nil {
		return "", errors.Trace(err)
	}

	return string(data), nil
}

func doSomething() error {
	data, err := readFile()
	if err != nil {
		return errors.Trace(err)
	}

	fmt.Println(data)

	return nil
}

func main() {
	var output bytes.Buffer

	if err := errors.Catch(doSomething); err != nil {
		for _, loc := range errors.StackTrace(err) {
			fmt.Fprintln(&output, loc.String())
		}
	}

	fmt.Println(
		strings.Contains(output.String(), "readFile"),
		strings.Contains(output.String(), "doSomething"),
	)
}
Output:

true true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

see stdlib errors.As

func Assert added in v2.7.0

func Assert(fact bool)

Assert will panic if fact is false

func AssertFact added in v2.8.2

func AssertFact(fact bool, message string)

AssertFact will panic with message if fact is false

func Catch added in v2.0.2

func Catch(f func() error) (err error)

run f, if f panic or returned, that value will be returned by this function

func Catch2 added in v2.3.0

func Catch2[A any](f func() (A, error)) (A, error)

like Catch but suitable for multiple return

func Catch3 added in v2.8.0

func Catch3[A, B any](f func() (A, B, error)) (A, B, error)

like Catch but suitable for multiple return

func Catch4 added in v2.8.0

func Catch4[A, B, C any](f func() (A, B, C, error)) (A, B, C, error)

like Catch but suitable for multiple return

func Check added in v2.5.0

func Check(err error)

will panic if err is not nil

func Errorf

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

see stdlib fmt.Errorf

func Format

func Format(err error) string

Format representation of the Error, including stack trace.

Use err.Error() if you want to get just the error string.

the returned string is not stable, future version maybe returned different format.

func FormatWithFilter added in v2.0.8

func FormatWithFilter(err error, filter func(trace.Location) bool) string

like Format, but you can filter what location to include in the formated string

func FormatWithFilterPkgs added in v2.0.11

func FormatWithFilterPkgs(err error, pkgs ...string) string

like Format, but you can filter pkg location to include in the formated string

func Is

func Is(err, target error) bool

see stdlib errors.Is

func New

func New(text string) error

see stdlib errors.New

func StackTrace

func StackTrace(err error) []trace.Location

Get stack trace of err

return nil if err doesn't have stack trace

func Trace added in v2.0.4

func Trace(err error) error

Trace will return new error that have stack trace

will return same err if err already have stack trace

use Is function to compare the returned error with others, because equality (==) operator will fail

func Trace2 added in v2.8.0

func Trace2[A any](a A, err error) (A, error)

like Trace but suitable for multiple return

func Trace3 added in v2.8.0

func Trace3[A, B any](a A, b B, err error) (A, B, error)

like Trace but suitable for multiple return

func Trace4 added in v2.8.0

func Trace4[A, B, C any](a A, b B, c C, err error) (A, B, C, error)

like Trace but suitable for multiple return

func Unwrap

func Unwrap(err error) error

see stdlib errors.Unwrap

Types

This section is empty.

Directories

Path Synopsis
Provide utility to get stack trace.
Provide utility to get stack trace.

Jump to

Keyboard shortcuts

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