werrors

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

werrors is a minimalistic package that providers only Wrap, Wrapf, WithMessage, WithMessagef, Cause and Into functions.

This allow use the standard "errors" package for New / Errorf, Is, As, etc.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type causer interface {
       Cause() error
}

If the error does not implement Cause, we will call Unwrap in a loop until find the original error. If the error is nil, nil will be returned without further investigation.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/peczenyj/errors/werrors"
)

func fn() error {
	e1 := errors.New("error")
	e2 := werrors.Wrap(e1, "inner")
	e3 := werrors.Wrap(e2, "middle")

	return werrors.Wrap(e3, "outer")
}

func main() {
	err := fn()
	fmt.Println(err)
	fmt.Println(werrors.Cause(err))

}
Output:

outer: middle: inner: error
error

func Into

func Into[T error](err error) (val T, ok bool)

Into finds the first error in err's chain that matches target type T, and if so, returns it.

Into is type-safe alternative to As.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/peczenyj/errors/werrors"
)

type timeoutError struct {
	err error
}

func (te *timeoutError) Error() string {
	return "timeout: " + te.err.Error()
}

func (te *timeoutError) Timeout() bool {
	return true
}

type TimeoutError interface {
	Timeout() bool
	error
}

func main() {
	err := errors.New("ops")
	err = &timeoutError{err}
	err = werrors.Wrap(err, "unexpected")

	if terr, ok := werrors.Into[TimeoutError](err); ok {
		fmt.Println(terr)
	}

}
Output:

timeout: ops

func WithMessage

func WithMessage(err error, message string) error

WithMessage is an alias to Wrap.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/peczenyj/errors/werrors"
)

func main() {
	cause := errors.New("whoops")
	err := werrors.WithMessage(cause, "oh noes")
	fmt.Println(err)

}
Output:

oh noes: whoops

func WithMessagef

func WithMessagef(err error, format string, args ...any) error

WithMessagef is an alias to Wrapf.

func Wrap

func Wrap(err error, message string) error

Wrap returns an error annotating err, it is the equivalent to

fmt.Errorf("%s: %w", message, err)

If err is nil, Wrap returns nil.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/peczenyj/errors/werrors"
)

func main() {
	cause := errors.New("whoops")
	err := werrors.Wrap(cause, "oh noes")
	fmt.Println(err)

}
Output:

oh noes: whoops

func Wrapf

func Wrapf(err error, format string, args ...any) error

Wrapf returns an formatted error annotating err, it is the equivalent to

message := fmt.Sprintf(format, args...)
fmt.Errorf("%s: %w", message, err)

If err is nil, Wrapf returns nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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