errors

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2024 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package errors implements functions to manipulate errors.

Example (StackTraceFormatting)
package main

import (
	"fmt"

	"github.com/komuw/ong/errors"
)

const expectedUser = "admin"

func login(user string) error {
	if user == expectedUser {
		return nil
	}

	return errors.New("invalid user")
}

func main() {
	err := login("badGuy")
	fmt.Printf("%+v", err)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v0.0.98

func As(err error, target any) bool

As is a pass through to the same func from the standard library errors package.

func Dwrap added in v0.0.29

func Dwrap(errp *error)

Dwrap(aka deferred wrap) adds stack traces to the error. It does nothing when *errp == nil.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/komuw/ong/errors"
)

func main() {
	fetchUser := func(u string) (errp error) {
		defer errors.Dwrap(&errp)

		users := []string{"John", "Alice", "Kamau"}
		if !slices.Contains(users, u) {
			return fmt.Errorf("user %s not found", u)
		}

		return nil
	}

	e := fetchUser("Emmy")
	fmt.Printf("%+#v", e)
}

func Errorf added in v0.0.98

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

Errorf is equivalent to the one in standard library mainly in spirit.

func Is added in v0.0.98

func Is(err, target error) bool

Is is a pass through to the same func from the standard library errors package.

func Join added in v0.0.98

func Join(errs ...error) error

Join returns an error that wraps the given errors. Any nil error values are discarded. Join returns nil if every value in errs is nil. The error formats as the concatenation of the strings obtained by calling the Error method of each element of errs, with a newline between each string.

A non-nil error returned by Join implements the Unwrap() error method. Unwrap returns an error whose text is the concatenation of each of the errs texts.

It only returns the stack trace of the first error.

Note that this function is equivalent to the one in standard library mainly in spirit. This is not a direct replacement of the standard library one.

func New

func New(text string) error

New returns an error with the supplied message. It also records the stack trace at the point it was called.

Error values implement fmt.Formatter and can be formatted by the fmt package. The following verbs are supported:

%s   print the error.
%v   see %s
%+v  print the error and stacktrace.

func StackTrace

func StackTrace(err error) string

StackTrace returns the stack trace contained in err, if any, else an empty string.

func Unwrap added in v0.0.98

func Unwrap(err error) error

Unwrap is a pass through to the same func from the standard library errors package.

func Wrap

func Wrap(err error) error

Wrap returns err, capturing a stack trace. It is a no-op if err had already been wrapped by this library.

Example
package main

import (
	"fmt"
	"os"

	"github.com/komuw/ong/errors"
)

func main() {
	opener := func(p string) error {
		_, err := os.Open(p)
		if err != nil {
			return errors.Wrap(err)
		}
		return nil
	}

	fmt.Printf("%+v", opener("/this/file/does/not/exist.txt"))
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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