faults

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: MIT Imports: 4 Imported by: 1

README

go-fault

A go package that implements a more complex way to handle errors in Go. (Alternative to the standard library)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Faults faultT

Faults is the namespace for operating on faults.

Functions

This section is empty.

Types

type Descriptor added in v0.1.1

type Descriptor interface {
	// New creates a new instance of the fault.
	//
	// Returns:
	//   - Fault: The new fault. Never returns nil.
	New() Fault

	fmt.Stringer
}

Descriptor is the interface for a fault descriptor.

var (
	// ErrPanic is the descriptor for a panic error.
	ErrPanic Descriptor

	// ErrNoSuchKey is the descriptor for a no such key error.
	ErrNoSuchKey Descriptor

	// ErrWrongKey is the descriptor for a wrong key error.
	ErrWrongKey Descriptor
)

func FromErr added in v0.1.1

func FromErr[C Enum](code C, err error) Descriptor

FromErr creates a new Fault with the given code and the given error. If the error is nil, "something went wrong" is used.

Parameters:

  • code: The code of the fault.
  • err: The error to create the fault from.

Returns:

  • Descriptor: The new fault.

func New

func New[C Enum](code C, msg string) Descriptor

New creates a new Fault with the given message.

Parameters:

  • code: The code of the fault.
  • msg: The message of the fault.

Returns:

  • Descriptor: The new fault. Never returns nil.

type Enum added in v0.1.1

type Enum interface {
	~int

	fmt.Stringer
}

Enum is an interface that must be implemented by all enums.

type Fault

type Fault interface {
	// Error returns the message of the fault.
	//
	// Returns:
	//   - string: The message of the fault.
	Error() string

	// Embeds returns the base of the fault.
	//
	// Returns:
	//   - Fault: The base of the fault. Nil if the fault is the base.
	Embeds() Fault

	// Lines returns the info lines of the fault, if any.
	//
	// Returns:
	//   - []string: The info lines of the fault.
	Lines() []string
}

Fault is the interface for a fault.

func Get added in v0.1.1

func Get[T any](fault Fault, key string) (T, Fault)

Get returns the value associated with the given key in the context of the given fault, or a zero value of type T if the key is not in the context. If the fault is nil, or if the key is not in the context, then the second return value is nil. Otherwise, the second return value is the fault.

Returns:

  • T: The value associated with the key, or a zero value of type T if the key is not in the context.
  • Fault: The fault, or nil if the fault is nil or if the key is not in the context.

func NewErrNoSuchKey added in v0.1.1

func NewErrNoSuchKey(key string) Fault

NewErrNoSuchKey creates a new Fault indicating that a specified key does not exist.

Parameters:

  • key: The key that was not found.

Returns:

  • Fault: A Fault with context indicating the missing key.

func NewErrPanic added in v0.1.1

func NewErrPanic(value any) Fault

NewErrPanic creates a new Fault indicating a panic.

Parameters:

  • value: The value of the panic.

Returns:

  • Fault: A Fault with context indicating a panic.

func NewErrWrongKey added in v0.1.1

func NewErrWrongKey(key string, expectedType, actualType any) Fault

NewErrWrongKey creates a new Fault indicating that a specified key refers to a value of a different type.

Parameters:

  • key: The key that was not found.
  • expectedType: The expected type of the value.
  • actualType: The actual type of the value.

Returns:

  • Fault: A Fault with context indicating the wrong key.

type StdCode added in v0.1.1

type StdCode int

StdCode is the type of a standard error code.

const (
	// Unknown is the standard error code for an unknown error.
	Unknown StdCode = iota // Unknown

	// FatalError is the standard error code for a fatal error.
	FatalError // Fatal Error

	// OperationFail occurs when a function fails.
	OperationFail // Operation Failed
)

func (StdCode) String added in v0.1.1

func (i StdCode) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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