systemerror

package
v0.1.1-0...-03eb687 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyExists = errors.New("already exists")

ErrAlreadyExists the specified element already exists.

View Source
var ErrDomain = errors.New("domain error")

ErrDomain static error for generic domain operations.

View Source
var ErrInvalidArgument = errors.New("invalid argument")

ErrInvalidArgument the given argument is invalid.

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound the specified element was not found.

View Source
var ErrOutOfRange = errors.New("out of range")

ErrOutOfRange the given input/resource/data is out of range.

View Source
var ErrPermissionDenied = errors.New("permission denied")

ErrPermissionDenied the specified principal has no permission to access a resource.

View Source
var ErrUnauthenticated = errors.New("unauthenticated")

ErrUnauthenticated no principal was found or its session has expired.

Functions

This section is empty.

Types

type Container

type Container interface {
	Unwrap() []error
}

Container is an aggregation structure that holds a slice of errors. Commonly already implemented by the returning value from errors.Join routine.

type Error

type Error interface {
	error
	// Unwrap returns static error (if any). Use it to run error type checks.
	//
	// For example:
	//	ok := errors.Is(err, ErrOutOfRange) // returns true if StaticError is ErrOutOfRange.
	Unwrap() error
	// Status returns the system ErrStatus code of this error.
	Status() Status
	// Reason returns domain-owned ErrReason code. Similar to ErrStatus but code might not be known by external systems.
	Reason() string
	// Message returns error ErrMessage.
	Message() string
	// LocalizedMessage returns locale-based error message. Returns Message as fallback value if localized ErrMessage is empty.
	LocalizedMessage() string
	// Metadata returns custom parameters related to the error.
	Metadata() map[string]string
}

Error is an extension of the standard lib's error interface, bringing high-level error metadata like Status fields. Extends built-in error interface.

type Status

type Status uint64

Status is the system status of an Error. Implements fmt.Stringer, returning a Status full name as string (e.g. StatusInvalidArgument -> "INVALID_ARGUMENT").

Systems should use these to map transport error codes (e.g. StatusNotFound -> http.StatusNotFound).

const (
	StatusInvalidArgument Status = iota + 1
	StatusFailedPrecondition
	StatusOutOfRange
	StatusUnauthenticated
	StatusPermissionDenied
	StatusNotFound
	StatusAborted
	StatusAlreadyExists
	StatusResourceExhausted
	StatusCancelled
	StatusDataLoss
	StatusUnknown
	StatusInternal
	StatusNotImplemented
	StatusUnavailable
	StatusDeadlineExceeded
	StatusDomain
)

func (Status) String

func (s Status) String() string

type SystemError

type SystemError struct {
	// ErrStatus the system ErrStatus code of this error.
	ErrStatus Status
	// ErrReason domain-owned ErrReason code. Similar to ErrStatus but code might not be known by external systems.
	ErrReason string
	// ErrMessage error message.
	ErrMessage string
	// ErrLocalizedMessage locale-based error ErrMessage.
	ErrLocalizedMessage string
	// ErrMetadata custom parameters related to the error.
	ErrMetadata map[string]string
	// StaticError parent static error
	StaticError error
}

SystemError is a generic error structure that can be extended or directly used to generate errors in a unified way.

Implements Error (from this same package) and fmt.Unwrap interfaces.

func NewAlreadyExists

func NewAlreadyExists(reason, message string, metadata map[string]string) SystemError

NewAlreadyExists allocates a SystemError with StatusAlreadyExists and ErrAlreadyExists.

The element already exists.

func NewArgumentNotOneOf

func NewArgumentNotOneOf(argumentName string, expectedValues ...string) SystemError

NewArgumentNotOneOf allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

An invalid argument has been detected. Attaches 'NOT_ONE_OF' reason.

func NewArgumentOutOfRange

func NewArgumentOutOfRange(argumentName, n, k any) SystemError

NewArgumentOutOfRange allocates a SystemError with StatusOutOfRange and ErrOutOfRange.

Attaches 'ARGUMENT_OUT_OF_RANGE' reason and both, n and k factors, to metadata.

func NewArgumentOutOfRangeSingle

func NewArgumentOutOfRangeSingle(argumentName string, rangeType string, n any) SystemError

NewArgumentOutOfRangeSingle allocates a SystemError with StatusOutOfRange and ErrOutOfRange.

Attaches 'ARGUMENT_OUT_OF_RANGE' reason and n range factor to metadata.

func NewDomain

func NewDomain(reason, message string, metadata map[string]string) SystemError

NewDomain allocates a SystemError with StatusDomain and ErrDomain.

A domain error has been detected.

func NewInvalidArgument

func NewInvalidArgument[T any](argumentName string, gotValue, wantValue T) SystemError

NewInvalidArgument allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

An invalid argument has been detected.

func NewInvalidFormatArgument

func NewInvalidFormatArgument(argumentName, expectedFormat string) SystemError

NewInvalidFormatArgument allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

An invalid argument has been detected. Attaches 'INVALID_FORMAT' reason.

func NewInvalidNoPrefixArgument

func NewInvalidNoPrefixArgument(argumentName, excludedPrefix string) SystemError

NewInvalidNoPrefixArgument allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

An invalid argument has been detected. Attaches 'INVALID_FORMAT' reason and adds 'no prefix' as expected type in message.

func NewInvalidNoSuffixArgument

func NewInvalidNoSuffixArgument(argumentName, excludedSuffix string) SystemError

NewInvalidNoSuffixArgument allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

An invalid argument has been detected. Attaches 'INVALID_FORMAT' reason and adds 'no suffix' as expected type in message.

func NewInvalidPrefixArgument

func NewInvalidPrefixArgument(argumentName, expectedPrefix string) SystemError

NewInvalidPrefixArgument allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

Attaches 'INVALID_FORMAT' reason and adds 'prefix' as expected type in message.

func NewInvalidSuffixArgument

func NewInvalidSuffixArgument(argumentName, expectedSuffix string) SystemError

NewInvalidSuffixArgument allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

An invalid argument has been detected. Attaches 'INVALID_FORMAT' reason and adds 'suffix' as expected type in message.

func NewMissingArgument

func NewMissingArgument(argumentName string) SystemError

NewMissingArgument allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

An invalid argument has been detected. Attaches 'MISSING_ARGUMENT' reason.

func NewNotEqualsArgument

func NewNotEqualsArgument(argumentName, expectedValue string) SystemError

NewNotEqualsArgument allocates a new SystemError using StatusInvalidArgument and ErrInvalidArgument.

An invalid argument has been detected. Attaches 'NOT_EQUALS' reason.

func NewNotFound

func NewNotFound(reason, message string, metadata map[string]string) SystemError

NewNotFound allocates a SystemError with StatusNotFound and ErrNotFound.

The specified element was not found.

func NewOutOfRange

func NewOutOfRange(reason, name string, metadata map[string]string) SystemError

NewOutOfRange allocates a SystemError with StatusOutOfRange and ErrOutOfRange.

func NewPermissionDeniedAuthorities

func NewPermissionDeniedAuthorities(principalName string, authorities, expAuthorities []string) SystemError

NewPermissionDeniedAuthorities Allocates a SystemError with StatusPermissionDenied and ErrPermissionDenied.

Principal has no authority to perform action. Sets 'PRINCIPAL_MISSING_AUTHORITIES' as reason.

func NewPermissionDeniedInvalidOwner

func NewPermissionDeniedInvalidOwner(principalID string, expOwnerID string) SystemError

NewPermissionDeniedInvalidOwner allocates a SystemError with StatusPermissionDenied and ErrPermissionDenied.

Principal is not the owner of the resource. Sets 'PRINCIPAL_NOT_OWNER' as reason.

func NewResourceAlreadyExists

func NewResourceAlreadyExists[T any](key string) SystemError

NewResourceAlreadyExists allocates a SystemError with StatusAlreadyExists and ErrAlreadyExists.

The resource already exists. Aimed for resource-oriented systems.

func NewResourceNotFound

func NewResourceNotFound[T any](key string) SystemError

NewResourceNotFound allocates a SystemError with StatusNotFound and ErrNotFound.

The specified resource was not found. Aimed for resource-oriented systems.

  • T : Resource type.

func NewUnauthenticated

func NewUnauthenticated() SystemError

NewUnauthenticated allocates a new SystemError using StatusUnauthenticated and ErrUnauthenticated.

The principal was not found or its session has expired.

func (SystemError) Error

func (e SystemError) Error() string

Error returns error as string.

func (SystemError) LocalizedMessage

func (e SystemError) LocalizedMessage() string

LocalizedMessage returns locale-based error message. Returns Message as fallback value if localized error is empty.

func (SystemError) Message

func (e SystemError) Message() string

Message returns error message.

func (SystemError) Metadata

func (e SystemError) Metadata() map[string]string

Metadata returns custom parameters related to the error.

func (SystemError) Reason

func (e SystemError) Reason() string

Reason returns domain-owned ErrReason code. Similar to ErrStatus but code might not be known by external systems.

func (SystemError) Status

func (e SystemError) Status() Status

Status returns the system ErrStatus code of this error.

func (SystemError) Unwrap

func (e SystemError) Unwrap() error

Unwrap returns static error (if any). Use it to run error type checks.

For example:

ok := errors.Is(err, ErrOutOfRange) // returns true if StaticError is ErrOutOfRange.

Jump to

Keyboard shortcuts

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