commonerrors

package
v1.94.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: Apache-2.0 Imports: 5 Imported by: 11

Documentation

Overview

Package commonerrors defines typical errors which can happen.

Index

Constants

View Source
const (
	TypeReasonErrorSeparator = ':'
	MultipleErrorSeparator   = '\n'
)

Variables

View Source
var (
	ErrNotImplemented     = errors.New("not implemented")
	ErrNoExtension        = errors.New("missing extension")
	ErrNoLogger           = errors.New("missing logger")
	ErrNoLoggerSource     = errors.New("missing logger source")
	ErrNoLogSource        = errors.New("missing log source")
	ErrUndefined          = errors.New("undefined")
	ErrInvalidDestination = errors.New("invalid destination")
	ErrTimeout            = errors.New("timeout")
	ErrLocked             = errors.New("locked")
	ErrStaleLock          = errors.New("stale lock")
	ErrExists             = errors.New("already exists")
	ErrNotFound           = errors.New("not found")
	ErrUnsupported        = errors.New("unsupported")
	ErrUnavailable        = errors.New("unavailable")
	ErrWrongUser          = errors.New("wrong user")
	ErrUnauthorised       = errors.New("unauthorised")
	ErrUnknown            = errors.New("unknown")
	ErrInvalid            = errors.New("invalid")
	ErrConflict           = errors.New("conflict")
	ErrMarshalling        = errors.New("unserialisable")
	ErrCancelled          = errors.New("cancelled")
	ErrEmpty              = errors.New("empty")
	ErrUnexpected         = errors.New("unexpected")
	ErrTooLarge           = errors.New("too large")
	ErrForbidden          = errors.New("forbidden")
	ErrCondition          = errors.New("failed condition")
	ErrEOF                = errors.New("end of file")
	ErrMalicious          = errors.New("suspected malicious intent")
	ErrOutOfRange         = errors.New("out of range")
	// ErrWarning is a generic error that can be used when an error should be raised but it shouldn't necessary be
	// passed up the chain, for example in cases where an error should be logged but the program should continue. In
	// these situations it should be handled immediately and then ignored/set to nil.
	ErrWarning = errors.New(warningStr)
)

List of common errors used to qualify and categorise go errors Note: if adding error types to this list, ensure mapping functions (below) are also updated.

Functions

func Any

func Any(target error, err ...error) bool

Any determines whether the target error is of the same type as any of the errors `err`

func ConvertContextError

func ConvertContextError(err error) error

ConvertContextError converts a context error into common errors.

func CorrespondTo added in v1.29.0

func CorrespondTo(target error, description ...string) bool

CorrespondTo determines whether a `target` error corresponds to a specific error described by `description` It will check whether the error contains the string in its description. It is not case-sensitive. ```code

  CorrespondTo(errors.New("feature a is not supported"), "not supported") = True
```

func DeserialiseError added in v1.45.0

func DeserialiseError(text []byte) (deserialisedError, err error)

DeserialiseError unmarshals text into an error. It tries to determine the error type.

func Errorf added in v1.83.0

func Errorf(targetErr error, format string, args ...any) error

Errorf is similar to fmt.Errorf although it will try to follow the error convention we use i.e. `errortype: message` but differs in that the wrapped error will be the targetErr

func GetCommonErrorReason added in v1.88.0

func GetCommonErrorReason(srcErr error) (reason string, err error)

func GetErrorReason added in v1.88.0

func GetErrorReason(srcErr error) (reason string, err error)

func GetUnderlyingErrorType added in v1.88.0

func GetUnderlyingErrorType(srcErr error) (commonerrorType, err error)

func Ignore added in v1.32.0

func Ignore(target error, ignore ...error) error

Ignore will return nil if the target error matches one of the errors to ignore

func IsCommonError added in v1.64.0

func IsCommonError(target error) bool

IsCommonError returns whether an error is a commonerror

func IsEmpty added in v1.45.0

func IsEmpty(err error) bool

IsEmpty states whether an error is empty or not. An error is considered empty if it is `nil` or has no description.

func IsWarning added in v1.64.0

func IsWarning(target error) bool

IsWarning will return whether an error is actually a warning

func New added in v1.83.0

func New(targetError error, msg string) error

New is similar to errors.New or fmt.Errorf but creates an error of type targetError

func NewWarning added in v1.64.0

func NewWarning(target error) (ok bool, err error)

NewWarning will create a warning wrapper around an existing commonerror so that it can be easily recovered. If the underlying error is not a commonerror then ok will be set to false

func Newf added in v1.83.0

func Newf(targetError error, msgFormat string, args ...any) error

Newf is similar to New but allows to format the message

func None

func None(target error, err ...error) bool

None determines whether the target error is of none of the types of the errors `err`

func ParseWarning added in v1.64.0

func ParseWarning(target error) (ok bool, err error)

ParseWarning will extract the error that has been wrapped by ErrWarning. It will return nil if the error was not one of ErrWarning with ok set to false. It will also set ok to false if the underlying error cannot be parsed

func RelatesTo added in v1.66.0

func RelatesTo(target string, errors ...error) bool

RelatesTo determines whether an error description string could relate to a particular set of common errors This assumes that the error description follows the convention of placing the type of errors at the start of the string.

func SerialiseError added in v1.45.0

func SerialiseError(err error) ([]byte, error)

SerialiseError marshals an error following a certain convention: `error type: reason`.

func UndefinedParameter added in v1.84.0

func UndefinedParameter(msg string) error

UndefinedParameter returns an undefined error with a message

func UndefinedVariable added in v1.84.0

func UndefinedVariable(variableName string) error

UndefinedVariable returns an undefined error related to a variable.

func UndefinedVariableWithMessage added in v1.84.0

func UndefinedVariableWithMessage(variableName string, msg string) error

UndefinedVariableWithMessage returns an undefined error with a message.

func WrapError added in v1.83.0

func WrapError(targetError, originalError error, msg string) error

WrapError wraps an error into a particular targetError. However, if the original error has to do with a contextual error (i.e. ErrCancelled or ErrTimeout), it will be passed through without having is type changed. This method should be used to safely wrap errors without losing information about context control information. If the target error is not set, the wrapped error will be of type ErrUnknown.

func WrapErrorf added in v1.83.0

func WrapErrorf(targetError, originalError error, msgFormat string, args ...any) error

WrapErrorf is similar to WrapError but uses a format for the message

func WrapIfNotCommonError added in v1.84.0

func WrapIfNotCommonError(targetError, originalError error, msg string) error

WrapIfNotCommonError is similar to WrapError but only wraps an error if it is not a common error.

func WrapIfNotCommonErrorf added in v1.84.0

func WrapIfNotCommonErrorf(targetError, originalError error, msgFormat string, args ...any) error

WrapIfNotCommonErrorf is similar to WrapError but only wraps an error if it is not a common error.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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