errors

package module
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 7 Imported by: 7

README

Package errors v2.4.0

The package errors contains various utility regarding errors management.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v2.3.0

func As(receivedErr error, expectedType any) bool

As checks if any error of the stack matches the expectedType API machting the standard library but allowing to wrap errors with ErrCtx + errgo or pkg/errors

func Errorf added in v2.1.0

func Errorf(ctx context.Context, format string, args ...interface{}) error

func Is added in v2.3.0

func Is(receivedErr, expectedError error) bool

Is checks if any error of the stack matches the error value expectedError API machting the standard library but allowing to wrap errors with ErrCtx + errgo or pkg/errors

func IsRootCause deprecated

func IsRootCause(err error, mytype interface{}) bool

IsRootCause return true if the cause of the given error is the same type as mytype. This function takes the cause of an error if the errors stack has been wrapped with errors.Wrapf or errgo.Notef or errgo.NoteMask or errgo.Mask.

Example:

errors.IsRootCause(err, &ValidationErrors{})

Deprecated: Use `As(err, mytype)` instead to match go standard libraries practices

func New added in v2.1.0

func New(ctx context.Context, message string) error

func Newf added in v2.1.0

func Newf(ctx context.Context, format string, args ...interface{}) error

func Notef deprecated

func Notef(ctx context.Context, err error, format string, args ...interface{}) error

Deprecated: Use `Wrap` or `Wrapf` instead of `Notef`. The library is able to unwrap mixed errors (wrapped with `errgo` or `github.com/pkg/errors`).

func RootCause deprecated

func RootCause(err error) error

RootCause returns the cause of an errors stack, whatever the method they used to be stacked: either errgo.Notef or errors.Wrapf.

Deprecated: Use `Is(err, expectedErr)` instead of `if RootCause(err) == expectedErr` to match go standard libraries practices

func RootCtxOrFallback added in v2.1.0

func RootCtxOrFallback(ctx context.Context, err error) context.Context

RootCtxOrFallback unwrap all wrapped errors from err to get the deepest context from ErrCtx errors. If there is no wrapped ErrCtx RootCtxOrFallback returns ctx from parameter.

func UnwrapError added in v2.2.0

func UnwrapError(err error) error

UnwrapError tries to unwrap `err`. It unwraps any causer type, errgo and ErrCtx errors. It returns nil if no err found. This provide the possibility to loop on UnwrapError by checking the return value. E.g.:

for unwrappedErr := err; unwrappedErr != nil; unwrappedErr = UnwrapError(unwrappedErr) {
	...
}

func Wrap added in v2.1.0

func Wrap(ctx context.Context, err error, message string) error

func Wrapf

func Wrapf(ctx context.Context, err error, format string, args ...interface{}) error

Types

type ErrCtx

type ErrCtx struct {
	// contains filtered or unexported fields
}

func (ErrCtx) Ctx

func (err ErrCtx) Ctx() context.Context

func (ErrCtx) Error

func (err ErrCtx) Error() string

func (ErrCtx) Unwrap added in v2.3.0

func (err ErrCtx) Unwrap() error

Unwrap implements error management from the standard library

type ValidationErrors

type ValidationErrors struct {
	Errors map[string][]string `json:"errors"`
}

ValidationErrors store each errors associated to every fields of a model

func (*ValidationErrors) Error

func (v *ValidationErrors) Error() string

type ValidationErrorsBuilder

type ValidationErrorsBuilder struct {
	// contains filtered or unexported fields
}

ValidationErrorsBuilder is used to provide a simple way to create a ValidationErrors struct. The typical usecase is:

func (m *MyModel) Validate(ctx context.Context) *ValidationErrors {
	validations := document.NewValidationErrorsBuilder()

	if m.Name == "" {
		validations.Set("name", "should not be empty")
	}

	if m.Email == "" {
		validations.Set("email", "should not be empty")
	}

	return validations.Build()
}

func NewValidationErrorsBuilder

func NewValidationErrorsBuilder() *ValidationErrorsBuilder

NewValidationErrors return an empty ValidationErrors struct

func (*ValidationErrorsBuilder) Build

Build will send a ValidationErrors struct if there is some errors or nil if no errors has been defined

func (*ValidationErrorsBuilder) Get

func (v *ValidationErrorsBuilder) Get(field string) []string

Get will return all errors set for a specific field

func (*ValidationErrorsBuilder) Merge

Merge ValidationErrors with another ValidationErrors

func (*ValidationErrorsBuilder) MergeWithPrefix

func (v *ValidationErrorsBuilder) MergeWithPrefix(prefix string, verr *ValidationErrors) *ValidationErrorsBuilder

MergeWithPrefix is merging ValidationErrors in another ValidationError adding a prefix for each error field

func (*ValidationErrorsBuilder) Set

Set will add an error on a specific field, if the field already contains an error, it will just add it to the current errors list

Jump to

Keyboard shortcuts

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