errors

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 5 Imported by: 11

README

errors

library for easily helping with errors.

License

MIT

Documentation

https://pkg.go.dev/gitlab.com/golang-utils/errors

Installation

go get gitlab.com/golang-utils/errors@latest

Usage

(everything can be combined freely)

simple

import (
	"errors"
	. "gitlab.com/golang-utils/errors"
)


var (
	ErrCantOpenFile     = Error("can't open file %q: %w")
	ErrFileDoesNotExist = errors.New("file does not exist")
)

// also without params call usable
err := ErrCantOpenFile.Params("missing.exe", ErrFileDoesNotExist)

// is true
errors.Is(err, ErrCantOpenFile) 

// is true
errors.Is(err, ErrFileDoesNotExist)

// errInner is ErrFileDoesNotExist
errInner := errors.Unwrap(err)

// returns `can't open file "missing.exe": file does not exist`
err.Error()

hidden wrapped error

import (
	"errors"
	. "gitlab.com/golang-utils/errors"
)

var (
	ErrCantOpenFile     = Error("can't open file %q")
	ErrFileDoesNotExist = errors.New("file does not exist")
)

// also without params call usable
err := ErrCantOpenFile.Wrap(ErrFileDoesNotExist).Params("missing.exe")

// is true
errors.Is(err, ErrCantOpenFile) 

// is true
errors.Is(err, ErrFileDoesNotExist)

// is ErrFileDoesNotExist
errInner := errors.Unwrap(err)

// returns `can't open file "missing.exe"`
err.Error()

Collection of errors

import (
	"errors"
	. "gitlab.com/golang-utils/errors"
)


var (
	ErrCantOpenFile     = Error("can't open file %q")
	ErrFileDoesNotExist = errors.New("file does not exist")
	ErrWeatherNotFine   = errors.New("the weather is bad")
)

err := Errors(
	ErrCantOpenFile.Wrap(ErrFileDoesNotExist).Params("missing.exe"),
	ErrWeatherNotFine,
)

// all of this is true
errors.Is(err, ErrCantOpenFile)
errors.Is(err, ErrFileDoesNotExist)
errors.Is(err, ErrWeatherNotFine)

// unwraps the first error
err1 := errors.Unwrap(err)

// is true
err1 == ErrCantOpenFile)

// unwraps the second error
err2 := errors.Unwrap(err)

// is true
err2 == ErrWeatherNotFine

// is true: there is no third error (ErrFileDoesNotExist can be unwrapped from err1)
errors.Unwrap(err) == nil
Map

import (
	"errors"
	. "gitlab.com/golang-utils/errors"
)

var (
	ErrCantOpenFile     = Error("can't open file %q")
	ErrFileDoesNotExist = errors.New("file does not exist")
	ErrWeatherNotFine   = errors.New("the weather is bad")
)

var err = ErrMap{
	"file": "missing.exe",
	"errors": Errors(
		ErrCantOpenFile.Wrap(ErrFileDoesNotExist).Params("missing.exe"),
		ErrWeatherNotFine,
	),
}

// all of this is true
errors.Is(err, ErrCantOpenFile)
errors.Is(err, ErrFileDoesNotExist)
errors.Is(err, ErrWeatherNotFine)

// is true
err.Get("file") == "missing.exe" 

// get the error collection back
errs := errors.Unwrap(err)

// first entry in the error collection
err1 := errors.Unwrap(errs)

// is true
errors.Is(err1, ErrCantOpenFile) 

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrMap

type ErrMap map[string]interface{}

func (ErrMap) Add added in v0.0.3

func (e ErrMap) Add(key string, val any)

func (ErrMap) Error

func (e ErrMap) Error() string

func (ErrMap) Get

func (e ErrMap) Get(key string) interface{}

func (ErrMap) Is

func (e ErrMap) Is(target error) bool

func (ErrMap) Unwrap

func (e ErrMap) Unwrap() error

type Error

type Error string

func (Error) Error

func (e Error) Error() string

func (Error) Params

func (e Error) Params(args ...interface{}) error

func (Error) Wrap

func (e Error) Wrap(err error) *errWithArgs

type ErrorCollection

type ErrorCollection struct {
	Errors []error
	// contains filtered or unexported fields
}

func Errors

func Errors(errs ...error) *ErrorCollection

func (*ErrorCollection) Error

func (e *ErrorCollection) Error() string

func (*ErrorCollection) Is

func (e *ErrorCollection) Is(target error) bool

func (*ErrorCollection) Unwrap

func (e *ErrorCollection) Unwrap() error

Jump to

Keyboard shortcuts

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