errors

package module
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: 4 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 As

func As(err error) (*gcers.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:

  • *gcers.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 gcers.ErrorCoder](err error, code T) (*gcers.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:

  • *gcers.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 Assert

func Assert(cond bool, msg string)

Assert asserts that a condition is true.

If the condition is false, it calls panic() with an error.Err with the code AssertFail and the given message.

Parameters:

  • cond: The condition to assert.
  • msg: The message to use if the condition is false.

func AssertConv

func AssertConv[T any](obj any, name string) T

AssertConv asserts that the given object can be converted to type T.

If the object can be converted to type T, it returns the converted value. Otherwise, it calls panic() with an error.Err with the code AssertFail and a message that includes the original object and its expected type.

Parameters:

  • obj: The object to convert to type T.
  • name: The name of the object to use for the error message.

Returns:

  • T: The converted value.

func AssertErr

func AssertErr(err error, format string, args ...any)

AssertErr asserts that an error is nil.

If the error is not nil, it calls panic() with an error.Err with the code AssertFail and a message that includes the original error.

Parameters:

  • err: The error to check.
  • format: The format string to use for the message.
  • args: The arguments to pass to the format string.

func AssertF

func AssertF(cond bool, format string, args ...any)

AssertF asserts that a condition is true.

If the condition is false, it calls panic() with an error.Err with the code AssertFail and a message that includes the formatted string.

Parameters:

  • cond: The condition to assert.
  • format: The format string to use for the message.
  • args: The arguments to pass to the format string.

func AssertNotNil

func AssertNotNil(obj any, name string)

AssertNotNil asserts that the given object is not nil.

If the object is nil, it calls panic() with an error.Err with the code AssertFail and a message that includes the original object.

Parameters:

  • obj: The object to assert is not nil.
  • name: The name of the object to use for the error message.

func AssertNotOk

func AssertNotOk(ok bool, format string, args ...any)

AssertNotOk asserts that a condition is false.

If the condition is true, it calls panic() with an error.Err with the code AssertFail and a message that includes the original condition.

Parameters:

  • ok: The condition to assert.
  • format: The format string to use for the message.
  • args: The arguments to pass to the format string.

func AssertNotZero

func AssertNotZero[T comparable](obj T, name string)

AssertNotZero asserts that the given object is not its zero value.

If the object is its zero value, it calls panic() with an error.Err with the code AssertFail and a message that includes the original object and its zero value.

Parameters:

  • obj: The object to assert is not its zero value.
  • name: The name of the object to use for the error message.

func AssertOk

func AssertOk(ok bool, format string, args ...any)

AssertOk asserts that a condition is true.

If the condition is false, it calls panic() with an error.Err with the code AssertFail and a message that includes the original condition.

Parameters:

  • ok: The condition to assert.
  • format: The format string to use for the message.
  • args: The arguments to pass to the format string.

func AssertType

func AssertType[T any](obj any, name string, allow_nil bool)

AssertType asserts that the given object is of type T.

If the object is not of type T, it calls panic() with an error.Err with the code AssertFail and a message that includes the original object and its expected type.

Parameters:

  • obj: The object to assert is of type T.
  • name: The name of the object to use for the error message.
  • allow_nil: Whether to allow the object to be nil.

func Fix

func Fix(name string, obj Fixer, allow_nil bool) error

Fix fixes the object.

Parameters:

  • name: The name of the object.
  • obj: The object to fix.
  • allow_nil: Whether to allow the object to be nil.

Returns:

  • error: An error that occurred while fixing the object.

func Is

func Is[T gcers.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 NewErrAfter

func NewErrAfter(before string, reason error) *gcers.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 NewErrAssertFail

func NewErrAssertFail(msg string) *gcers.Err

NewErrAssertFail creates a new error.Err error.

Parameters:

  • msg: The message of the error.

Returns:

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

func NewErrAt

func NewErrAt(at string, reason error) *gcers.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) *gcers.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 NewErrFix

func NewErrFix(message string, reason error) *gcers.Err

NewErrFix creates a new error.Err error.

Parameters:

  • message: The message of the error.
  • reason: The reason for the error.

Returns:

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

func NewErrInvalidParameter

func NewErrInvalidParameter(message string) *gcers.Err

NewErrInvalidParameter creates a new error.Err error.

Parameters:

  • message: The message of the error.

Returns:

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

func NewErrInvalidUsage

func NewErrInvalidUsage(message string, usage string) *gcers.Err

NewErrInvalidUsage creates a new error.Err error.

Parameters:

  • 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(parameter string) *gcers.Err

NewErrNilParameter creates a new error.Err error.

Parameters:

  • parameter: the name of the invalid parameter.

Returns:

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

func NewErrNoSuchKey

func NewErrNoSuchKey(key string) *gcers.Err

NewErrNoSuchKey creates a new error.Err error.

Parameters:

  • key: The key that does not exist.

Returns:

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

func Value

func Value[C gcers.ErrorCoder, T any](e *gcers.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.

func ZeroOf

func ZeroOf[T any]() T

ZeroOf returns a zero value of type T.

This is useful when you want to compare a value with its zero value.

Example:

if x == (errors.ZeroOf[struct{}]()) {
    // x is the zero value of struct{}
}

Types

type ErrorCode

type ErrorCode int
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

	// FailFix occurs when a struct cannot be fixed or resolved
	// due to an invalid internal state.
	FailFix

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

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

	// AssertFail occurs when an assertion fails.
	AssertFail
)

func (ErrorCode) Int

func (e ErrorCode) Int() int

Int implements the error.ErrorCoder interface.

func (ErrorCode) String

func (i ErrorCode) String() string

type Fixer

type Fixer interface {
	// Fix fixes the object.
	//
	// Returns:
	//   - error: An error that occurred while fixing the object.
	Fix() error
}

Fixer is defines the behavior of an object that can be fixed. This must not have a non-pointer receiver.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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