errors

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: MIT Imports: 8 Imported by: 12

README

errors

A Go package for dealing with generalized errors.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisplayError added in v0.1.3

func DisplayError(w io.Writer, to_display error) error

DisplayError displays the complete error to the writer.

Parameters:

  • w: The writer to write to.
  • to_display: The error to display.

Returns:

  • error: The error that occurred while displaying the error.

func Is

func Is[T ErrorCoder](err error, code T) bool

Is is function that checks if an error is of type T.

Parameters:

  • err: The error to check.
  • code: The error code to check.

Returns:

  • bool: true if the error is of type T, false otherwise (including if the error is nil).

func Merge added in v0.1.4

func Merge(outer, inner *internal.Info) *internal.Info

Merge merges the inner Info into the outer Info.

Parameters:

  • outer: The outer Info to merge.
  • inner: The inner Info to merge.

Returns:

  • *Info: A pointer to the new Info. Never returns nil.

Note:

  • The other Info is the inner info of the current Info and, as such, when conflicts occur, the outer Info takes precedence.

func MergeErrors added in v0.1.4

func MergeErrors(outer, inner error) error

func Panic added in v0.1.4

func Panic(w io.Writer, to_display error)

Panic is like DisplayError but panics afterwards.

Parameters:

  • w: The writer to write to.
  • to_display: The error to display.

func Value

func Value[C ErrorCoder, T any](e *Err, key string) (T, error)

Value is a function that returns the value of the context with the given key.

Parameters:

  • e: The error to get the value from.
  • key: The key of the context.

Returns:

  • T: The value of the context with the given key.
  • error: The error that occurred while getting the value.

Types

type Err added in v0.1.4

type Err struct {
	// Severity is the severity level of the error.
	Severity SeverityLevel

	// Code is the error code.
	Code ErrorCoder

	// Message is the error message.
	Message string

	*internal.Info
}

Err represents a generalized error.

func As

func As(err error) (*Err, bool)

As returns the error if it is of type T.

Parameters:

  • err: The error to check.
  • code: The error code to check.

Returns:

  • *Err: The error if it is of type T, nil otherwise.
  • bool: true if the error is of type T, false otherwise (including if the error is nil).

func AsWithCode

func AsWithCode[T ErrorCoder](err error, code T) (*Err, bool)

AsWithCode returns the error if it is of type T.

Parameters:

  • err: The error to check.
  • code: The error code to check.

Returns:

  • *Err: The error if it is of type T, nil otherwise.
  • bool: true if the error is of type T, false otherwise (including if the error is nil).

func New added in v0.1.4

func New[C ErrorCoder](code C, message string) *Err

New creates a new error.

Parameters:

  • code: The error code.
  • message: The error message.

Returns:

  • *Err: A pointer to the new error. Never returns nil.

func NewErrAfter

func NewErrAfter(before string, reason error) *Err

NewErrAfter creates a new error.Err error.

Parameters:

  • before: The operation after which the error occurred.
  • reason: The reason for the error.

Returns:

  • *error.Err: The new error. Never returns nil.

func NewErrAt

func NewErrAt(at string, reason error) *Err

NewErrAt creates a new error.Err error.

Parameters:

  • at: The operation at which the error occurred.
  • reason: The reason for the error.

Returns:

  • *error.Err: The new error. Never returns nil.

func NewErrBefore

func NewErrBefore(after string, reason error) *Err

NewErrBefore creates a new error.Err error.

Parameters:

  • after: The operation before which the error occurred.
  • reason: The reason for the error.

Returns:

  • *error.Err: The new error. Never returns nil.

func NewErrInvalidParameter

func NewErrInvalidParameter(frame, message string) *Err

NewErrInvalidParameter creates a new error.Err error.

Parameters:

  • frame: The frame of the error.
  • message: The message of the error.

Returns:

  • *error.Err: The new error. Never returns nil.

This function is mostly useless since it just wraps BadParameter.

func NewErrInvalidUsage

func NewErrInvalidUsage(frame, message, usage string) *Err

NewErrInvalidUsage creates a new error.Err error.

Parameters:

  • frame: The frame of the error.
  • message: The message of the error.
  • usage: The usage/suggestion to solve the problem.

Returns:

  • *error.Err: The new error. Never returns nil.

func NewErrNilParameter

func NewErrNilParameter(frame, parameter string) *Err

NewErrNilParameter creates a new error.Err error.

Parameters:

  • frame: The frame of the error.
  • parameter: the name of the invalid parameter.

Returns:

  • *error.Err: The new error. Never returns nil.

func NewErrNilReceiver added in v0.1.4

func NewErrNilReceiver(frame string) *Err

NewErrNilReceiver creates a new error.Err error with the code OperationFail.

Parameters:

  • frame: The frame of the error.

Returns:

  • *error.Err: The new error. Never returns nil.

func NewErrNoSuchKey

func NewErrNoSuchKey(frame, key string) *Err

NewErrNoSuchKey creates a new error.Err error.

Parameters:

  • frame: The frame of the error.
  • key: The key that does not exist.

Returns:

  • *error.Err: The new error. Never returns nil.

func NewFromError added in v0.1.4

func NewFromError[C ErrorCoder](code C, err error) *Err

NewFromError creates a new error from an error.

Parameters:

  • code: The error code.
  • err: The error to wrap.

Returns:

  • *Err: A pointer to the new error. Never returns nil.

func NewWithSeverity added in v0.1.4

func NewWithSeverity[C ErrorCoder](severity SeverityLevel, code C, message string) *Err

NewWithSeverity creates a new error.

Parameters:

  • severity: The severity level of the error.
  • code: The error code.
  • message: The error message.

Returns:

  • *Err: A pointer to the new error. Never returns nil.

func (*Err) AddContext added in v0.1.4

func (e *Err) AddContext(key string, value any)

AddContext adds a context to the error. Does nothing if the receiver is nil.

Parameters:

  • key: The key of the context.
  • value: The value of the context.

func (*Err) AddFrame added in v0.1.4

func (e *Err) AddFrame(frame string)

AddFrame prepends a frame to the stack trace. Does nothing if the receiver is nil or the trace is empty.

Parameters:

  • frame: The frame to add.

If prefix is empty, the call is used as the frame. Otherwise a dot is added between the prefix and the call.

func (*Err) AddSuggestion added in v0.1.4

func (e *Err) AddSuggestion(suggestion string)

AddSuggestion adds a suggestion to the error. Does nothing if the receiver is nil.

Parameters:

  • suggestion: The suggestion to add.

func (*Err) ChangeSeverity added in v0.1.4

func (e *Err) ChangeSeverity(new_severity SeverityLevel)

ChangeSeverity changes the severity level of the error. Does nothing if the receiver is nil.

Parameters:

  • new_severity: The new severity level of the error.

func (Err) Error added in v0.1.4

func (e Err) Error() string

Error implements the error interface.

func (*Err) IsNil added in v0.1.4

func (e *Err) IsNil() bool

IsNil implements the Pointer interface.

func (*Err) SetInner added in v0.1.4

func (e *Err) SetInner(inner error)

SetInner sets the inner error. Does nothing if the receiver is nil.

Parameters:

  • inner: The inner error.

func (Err) Value added in v0.1.4

func (e Err) Value(key string) (any, bool)

Value returns the value of the context with the given key.

Parameters:

  • key: The key of the context.

Returns:

  • any: The value of the context with the given key.
  • bool: true if the context contains the key, false otherwise.

type ErrorCode

type ErrorCode int

ErrorCode is the type of the error code.

const (
	// BadParameter occurs when a parameter is invalid or is not
	// valid for some reason. For example, a nil pointer when nil
	// pointers are not allowed.
	BadParameter ErrorCode = iota

	// InvalidUsage occurs when users call a function without
	// proper setups or preconditions.
	InvalidUsage

	// NoSuchKey occurs when a context key is requested but does
	// not exist.
	NoSuchKey

	// OperationFail occurs when an operation cannot be completed
	// due to an internal error.
	OperationFail
)

func (ErrorCode) Int

func (e ErrorCode) Int() int

Int implements the error.ErrorCoder interface.

func (ErrorCode) String

func (i ErrorCode) String() string

type ErrorCoder added in v0.1.4

type ErrorCoder interface {
	// Int returns the integer value of the error code.
	//
	// Returns:
	//   - int: The integer value of the error code.
	Int() int

	fmt.Stringer
}

ErrorCoder is an interface that all error codes must implement.

type Pointer added in v0.1.4

type Pointer interface {
	// IsNil checks whether the pointer is nil.
	//
	// Returns:
	//   - bool: True if the pointer is nil, false otherwise.
	IsNil() bool
}

Pointer is an interface that checks whether a pointer is nil.

type SeverityLevel added in v0.1.4

type SeverityLevel int

SeverityLevel represents the severity level of an error.

const (
	// INFO is the severity level for informational messages.
	// (i.e., errors that are not critical nor fatal)
	//
	// Mostly used for message passing.
	INFO SeverityLevel = iota

	// WARNING is the severity level for warning messages.
	// (i.e., errors that are not critical nor fatal, yet worthy of attention).
	WARNING

	// ERROR is the severity level for error messages.
	// (i.e., the standard error level).
	ERROR

	// FATAL is the severity level for fatal errors.
	// (i.e., the highest severity level).
	//
	// These are usually panic-level of errors.
	FATAL
)

func (SeverityLevel) String added in v0.1.4

func (i SeverityLevel) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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