Err

Err

class Err[source]

Bases: abc.ABC

Describes either an error or a list of errors

Methods

check

Returns an error if the given predicate is false, otherwise returns None.

collect

Collect a possibly empty sequence of (optional) errors into an optional single error

collect1

Collect a non-empty sequence of errors into a single error

errors

Returns a sequence of all contained errors

in_context

Adds to this error information about the context in which it occurred

make

Creates a single error

markdown

Returns a Markdown-formatted summary of this error (or list of errors)

pretty_print

Pretty prints an error on the console

List of members of Err

abstract markdown()[source]

Returns a Markdown-formatted summary of this error (or list of errors)

Return type

Sequence[str]

abstract errors()[source]

Returns a sequence of all contained errors

If this is not a collection of errors ManyErr, returns a sequence with a single item, this instance itself.

Return type

Sequence[Err1]

abstract in_context(**contexts)[source]

Adds to this error information about the context in which it occurred

Parameters

contexts (Any) – Contexts (given as key/value pairs) to append to the context list

Return type

Err

Returns

Updated error

pretty_print()[source]

Pretty prints an error on the console

Return type

None

static collect1(first_error, *additional_errors)[source]

Collect a non-empty sequence of errors into a single error

Example

We can also collect errors coming from a list using the following syntax

>>> errors = [Err.make('err 1'), Err.make('err 2'), Err.make('err 3')]
>>> Err.collect1(*errors)
ManyErr(errs=...)
Parameters
  • first_error (Err) – First error to collect

  • additional_errors (Err) – Errors to collect into a single error, at least one must be provided

Raises

ValueError – If no error is provided

Return type

Err

Returns

A consolidated error

static collect(*errs)[source]

Collect a possibly empty sequence of (optional) errors into an optional single error

Parameters

errs (Optional[Err]) – (Optional) errors

Return type

Optional[Err]

Returns

An error or None

static make(msg, **contexts)[source]

Creates a single error

Example

>>> Err.make("test error")
Err1(msg='test error', contexts=[])
Parameters
  • msg (str) – Error message

  • contexts (Any) – Contexts (given as key/value pairs) to append to the context list

Return type

Err1

Returns

An error

static check(predicate, msg, **contexts)[source]

Returns an error if the given predicate is false, otherwise returns None.

Example

>>> a = -1
>>> b = 2
>>> Err.collect(Err.check(a >= 0, "a < 0"), Err.check(b >= 0, "b < 0"))
Err1(msg='a < 0', contexts=[])
Parameters
  • predicate (bool) – Predicate to check

  • msg (str) – Error message

  • contexts (Any) – Contexts (given as key/value pairs) to append to the context list

Return type

Optional[Err1]

Returns

An error or None