error

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Err

type Err struct {
	// Code is the error code.
	Code ErrorCoder

	// Message is the error message.
	Message string

	// Suggestions is a list of suggestions for the user.
	Suggestions []string

	// Severity is the severity level of the error.
	Severity SeverityLevel

	// Timestamp is the timestamp of the error.
	Timestamp time.Time

	// Context is the context of the error.
	Context map[string]any

	// StackTrace is the stack trace of the error.
	StackTrace *internal.StackTrace

	// Inner is the inner error of the error.
	Inner error
}

Err represents a generalized error.

func New

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 NewWithSeverity

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

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

func (e *Err) AddFrame(prefix, call string)

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

Parameters:

  • prefix: The prefix of the frame.
  • call: The call of the frame.

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

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

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

func (e Err) Error() string

Error implements the error interface.

func (*Err) SetInner

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

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 ErrorCoder

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 SeverityLevel

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

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