errors

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: MIT Imports: 4 Imported by: 0

README

Errors

Improved error handling compatible with standard library

package main

import (
	"fmt"
    goerr "errors"
	"github.com/difof/goul/errors"
)

func someerrfunc() error {
	return goerr.New("some error")
}

func DoSomething() error {
	return errors.Newi(someerrfunc(), "something went wrong") 
}

func DoSomethingTwoReturns() error {
	return errors.CheckAny(TwoReturns())(
		// This callback is called if TwoReturns() returns result, nil
		func(result int) error {
			// do something with result
			return nil
		})
}

func TwoReturns() (int, error) {
	result := 223
	return result, errors.Newm("something went wrong")
}

func main() {
	// Simple use which prints stack trace
	if err := DoSomething(); err != nil {
		fmt.Println(err)
	}

	// Dual return values
	if err := DoSomethingTwoReturns(); err != nil {
		fmt.Println(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target interface{}) bool

As is a wrapper around go's standard errors.As

func Check

func Check(err error) error

Check returns a new error if the given error is not nil

func CheckAny added in v0.2.1

func CheckAny[R any](result R, err error) func(func(R) error) error

CheckAny is used for two return values function which also returns an error.

It calls the given function if the error is nil, otherwise it returns the error. Also returns the error returned by the given function.

This function is a shortcut for when you either return an error or handle a result as last statement in a function.

func CheckAnyf added in v0.2.3

func CheckAnyf[R any](result R, err error) func(func(R) error, string, ...any) error

CheckAnyf is used for two return values function which also returns an error.

It calls the given function if the error is nil, otherwise it returns the error. Also returns the error returned by the given function.

This function is a shortcut for when you either return an error or handle a result as last statement in a function.

func Checkf added in v0.2.3

func Checkf(err error, msg string, params ...interface{}) error

Checkf returns error or nil. If error is not nil, it will be wrapped with the given message

func Ignore added in v0.2.5

func Ignore[T any](r T, _ error) T

func IgnoreCheckAny added in v0.2.1

func IgnoreCheckAny[R any]() func(R) error

func Is

func Is(err, target error) bool

Is is a wrapper around go's standard errors.Is

func Must added in v0.2.5

func Must[T any](r T, err error) T

func New

func New(err error) error

New adds stack trace to the given error

func Newf

func Newf(format string, params ...interface{}) error

Newf constructs a new Error using the format

func Newi

func Newi(inner error, msg string) error

Newi wraps the error with a new Error and a message

func Newif

func Newif(inner error, format string, params ...interface{}) error

Newif constructs a new formatted error with a wrapped inner error

func Newm

func Newm(msg string) error

Newm constructs a new Error using the message

func News

func News(skip int, err error) error

News constructs a new Error and skips stack frames for getting caller path.

func Newsf

func Newsf(skip int, format string, params ...interface{}) error

func Newsi

func Newsi(skip int, inner error, msg string) error

func Newsif

func Newsif(skip int, inner error, format string, params ...interface{}) error

func Unwrap

func Unwrap(err error) error

Unwrap is a wrapper around go's standard errors.Unwrap

Types

type Error

type Error struct {
	Source  string
	Message error
	Inner   error
}

Error is a lightweight error struct with stack trace. Compatible with standard errors package.

Use NewXY functions to construct new errors.

func NewError

func NewError(source string, message, inner error) *Error

func (*Error) Each

func (e *Error) Each(it func(err error) bool)

Each iterates all inner errors as long as they're Error, starting from itself

func (*Error) Error

func (e *Error) Error() string

Error returns the stack trace of this error

func (*Error) StackTrace

func (e *Error) StackTrace() (list []string)

StackTrace builds the stack trace of all inner errors of Error

func (*Error) String

func (e *Error) String() string

String returns current error's message and source

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the inner error

Jump to

Keyboard shortcuts

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