error

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2024 License: MIT Imports: 6 Imported by: 0

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, err error) error

DisplayError displays the complete error to the writer.

Parameters:

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

Returns:

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

Types

type Err

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

	*Info
}

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 NewFromError added in v0.1.3

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

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(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

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) IsNil added in v0.1.3

func (e *Err) IsNil() bool

IsNil checks whether the error is nil.

Returns:

  • bool: True if the error is nil, false otherwise.

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 Info added in v0.1.3

type Info struct {
	// Suggestions is a list of suggestions for the user.
	Suggestions []string

	// 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
}

Info contains additional information about the error.

func NewInfo added in v0.1.3

func NewInfo() *Info

NewInfo creates a new Info.

Returns:

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

func (Info) DisplayInfo added in v0.1.3

func (info Info) DisplayInfo(w io.Writer) error

DisplayInfo displays the info to the writer.

Parameters:

  • w: The writer to write to.

Returns:

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

func (*Info) IsNil added in v0.1.3

func (info *Info) IsNil() bool

IsNil checks whether the info is nil.

Returns:

  • bool: True if the info is nil, false otherwise.

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