syserr

package
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrResourceNotFound is returned when the resource is not found.
	ErrResourceNotFound = errors.New("resource not found")
	// ErrResourceAlreadyExists is returned when the resource already exists.
	ErrResourceAlreadyExists = errors.New("resource already exists")
	// ErrInvalidFormat is returned when the format is invalid.
	ErrInvalidFormat = errors.New("invalid format")
	// ErrMissingValue is returned when the value is missing.
	ErrMissingValue = errors.New("missing value")
)

Functions

This section is empty.

Types

type Error

type Error struct {
	// Type the type of the [Error].
	Type Type
	// InternalCode is a code generated by the system to identify errors.
	//
	// This helps external systems, calling the local system, to differentiate errors with the same [Type].
	InternalCode string
	// Message a clear explanation about the occurrence (i.e. an [Error]).
	Message string
	// StaticError is the static error [Error] is attached to.
	//
	// Consider [Error] as a dynamic error as the latter holds values that can be modified at any time without
	// needing to re-allocate another [Error] instance.
	//
	// This field may also work as parent error.
	StaticError error
	// Metadata is a collection of information additional to the properties defined in [Error].
	// These properties are dynamic and external systems should not depend on its formatting nor values.
	Metadata map[string]string
}

An Error refers to an issue or malfunction that occurs within the operating system or other system-level software, which impacts the proper execution of a program. These errors typically arise from interactions between the application and the underlying hardware or operating system resources.

If New is called to create the Error, use Option routines (e.g. WithInternalCode, WithStaticError) to set optional values.

This structure implements stdlib error, errors.Unwrap and fmt.Stringer interfaces.

func New

func New(errType Type, message string, opts ...Option) Error

New allocates a new Error instance.

func NewAboveLimit

func NewAboveLimit(name string, max int) Error

NewAboveLimit allocates a new Error.

Specifies Error properties for `value is above the expected limit` error cases.

func NewBelowLimit

func NewBelowLimit(name string, min int) Error

NewBelowLimit allocates a new Error.

Specifies Error properties for `value is below the expected limit` error cases.

func NewEquals

func NewEquals(name string, invalidVals ...string) Error

NewEquals allocates a new Error.

Specifies Error properties for `value is equals to` error cases.

func NewInvalidFormat

func NewInvalidFormat(name, format string) Error

NewInvalidFormat allocates a new Error.

Specifies Error properties for `invalid format` error cases.

func NewInvalidLength

func NewInvalidLength(name string, expLen int) Error

NewInvalidLength allocates a new Error.

Specifies Error properties for `value does not have the expected length` error cases.

func NewMissingValue

func NewMissingValue(name string) Error

NewMissingValue allocates a new Error.

Specifies Error properties for `missing value` error cases.

func NewNotEquals

func NewNotEquals(name, exp string) Error

NewNotEquals allocates a new Error.

Specifies Error properties for `value is not equals to` error cases.

func NewNotOneOf

func NewNotOneOf(name string, values ...string) Error

NewNotOneOf allocates a new Error.

Specifies Error properties for `value is not one of` error cases.

func NewResourceAlreadyExists

func NewResourceAlreadyExists[T any]() Error

NewResourceAlreadyExists allocates a new Error.

Specifies Error properties for `resource already exists` error cases.

func NewResourceNotFound

func NewResourceNotFound[T any]() Error

NewResourceNotFound allocates a new Error.

Specifies Error properties for `resource not found` error cases.

func (Error) Error

func (e Error) Error() string

func (Error) String

func (e Error) String() string

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap unwraps an underlying static error based on [Error.StaticError].

If given field is nil, a new static error will be generated using errors.New and the [Error.Message].

type Option

type Option func(*Error)

Option is an option routine exposed to clients to set optional values of Error.

func WithInfo

func WithInfo(key, value string) Option

WithInfo appends `key` and `value` into the [Error.Metadata] collection.

If `key` is empty, no entry will be set.

func WithInternalCode

func WithInternalCode(code string) Option

WithInternalCode sets the Error internal code.

func WithStaticError

func WithStaticError(err error) Option

WithStaticError sets the Error static error.

Static errors may help for error checking operations using errors.Is.

Moreover, a static error might be a parent error.

type Type

type Type uint16

Type is a custom integer representing Error types.

const (
	// UnknownCode the error code is not known.
	UnknownCode Type = iota
	// OutOfRange the value is out of the specified range.
	OutOfRange
	// InvalidArgument the value is invalid according to internal rules.
	InvalidArgument
	// MissingPrecondition a precondition is required to be executed.
	MissingPrecondition
	// FailedPrecondition a required precondition was executed, but it resulted in failure.
	FailedPrecondition
	// ResourceExists the resource already exist in a persistence store.
	ResourceExists
	// ResourceNotFound the resource was not present in a persistence store.
	ResourceNotFound
	// PermissionDenied access to a certain operation -or resource- was denied.
	PermissionDenied
	// Unauthenticated the given routine requires the caller (aka. Principal) to be authenticated, yet
	// no authentication was found.
	Unauthenticated
	// Aborted the routine execution was aborted.
	Aborted
	// ResourceExhausted the system cannot handle more calls at this time.
	ResourceExhausted
	// DeadlineExceeded the routine execution could not respond in time, reaching the caller wait deadline.
	DeadlineExceeded
	// Unimplemented the operation has not been implemented yet.
	Unimplemented
	// DataLoss the operation resulted in data missing.
	DataLoss
	// Unavailable the system/operation is not available to accept calls.
	Unavailable
	// Internal a non-public error happened during the routine execution.
	Internal
)

func (Type) String

func (e Type) String() string

type Unwrapper

type Unwrapper interface {
	// Unwrap retrieves the slice of errors.
	Unwrap() []error
}

Unwrapper is construct holding a set of errors.

Its main usage is by casting this from an error interface to retrieve the slice of errors joined with errors.Join (or any structure implementing errors.Unwrap).

Jump to

Keyboard shortcuts

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