errors

package
v0.0.0-...-dc70fb0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: GPL-3.0, LGPL-3.0 Imports: 6 Imported by: 1

Documentation

Overview

A concise error handling package that provides the mechanisms for creating a "root" error that is a basic instance of String/Uint/Int, and the ability to add stacktrace and context to the root error. Existing "root" errors already present in the Go standard library can be used equally well.

Ideally, following properties are desired:

  1. uniquely-identifying error instances
  2. constant/read-only instances, i.e. can be addressed but not changed
  3. composable/extendable into dedicated error type/series for specific use cases (without overhead)

TODO above three properties cannot be satisfied yet. TODO do we need predefined errors? Errors such as `os.ErrInvalid` are defined specifically for the filesystem use-case. However, we need generic errors representing the various classes of incorrectness.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aggregate

func Aggregate(cause error, message string, errs ...error) error

Aggregate creates a context for a root cause, based on any number of errors. TODO consider if we need to embed the actual errors. This touches on a tangential consideration: how to handle errors crossing abstraction boundaries. Do we want to make them available, i.e. leak the abstraction?

func Context

func Context(cause error, message string) error

Context wraps an error to provide additional context information. TODO consider defining different variations of context: with-message, with-key-values-pairs, ... We can consider this basic context type as key-value pair with key 'message'. Then as we extract key-value pairs, we can include base contexts.

func Is

func Is(err error, target error) bool

Is repeatedly unwraps an error and compares to target on each unwrapping. Is uses the implementation from std/errors.

func JoinMessages

func JoinMessages(errs []error, sep string) string

JoinMessages joins the error messages of each error, using the provided separator.

func Stack

func Stack(err error) []byte

Stack extracts the first stacktrace encountered in a wrapped error, or nil if no stack is present/found. It is assumed that, generally, at most one stacktrace is present.

func Stacktrace

func Stacktrace(cause error) error

func Unwrap

func Unwrap(err error) error

Unwrap unwraps an error if `Unwrap() error` exists, or returns nil otherwise. Unwrap uses the implementation from std/errors.

Types

type IntError

type IntError struct {
	// contains filtered or unexported fields
}

IntError as a base type for const errors.

Similar to StringError, this type can be used to declare const errors. This type is based on int, therefore most suitable for errors that are signaled through a numeric code, such as with HTTP-like protocols.

func NewIntError

func NewIntError(v int) *IntError

NewIntError creates a new int-based error instance. If used as-is, the pointer that is returned is uniquely-identifying and therefore immediately useable. In case the IntError is a basis for a custom error type, the pointer can be dereferenced to include the value itself in the newly defined struct-type, for efficiency.

func (*IntError) Error

func (e *IntError) Error() string

type StringError

type StringError struct {
	// contains filtered or unexported fields
}

StringError as a base type for const errors.

This type is intended to be used as replacement for errors.New(..) from std, such that you can define an error as const (constant). The idea being that the "root" error type is just the type and the circumstances within which the error occurs are dictated by any number of contexts wrapped around the root error.

func NewStringError

func NewStringError(msg string) *StringError

NewStringError creates a new string-based error instance. If used as-is, the pointer that is returned is uniquely-identifying and therefore immediately useable. In case the StringError is a basis for a custom error type, the pointer can be dereferenced to include the value itself in the newly defined struct-type, for efficiency.

func (*StringError) Error

func (e *StringError) Error() string

type UintError

type UintError struct {
	// contains filtered or unexported fields
}

UintError as a base type for const errors.

Similar to StringError, this type can be used to declare const errors. This type is based on uint therefore is most suitable for errors that are signaled through a numeric code, such as with HTTP-like protocols.

func NewUintError

func NewUintError(value uint) *UintError

NewUintError creates a new uint-based error instance. If used as-is, the pointer that is returned is uniquely-identifying and therefore immediately useable. In case UintError is a basis for a custom error type, the pointer can be dereferenced as to include the value itself in the newly defined struct-type, for efficiency.

func (*UintError) Error

func (e *UintError) Error() string

Jump to

Keyboard shortcuts

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