Skip to content

gufo.err.err

Define Err class and err singleton.

Attributes:

Name Type Description
err

Err singletone.

Err

Bases: object

Error handling singleton.

Example
from gufo.err import err

err.setup()

__default_middleware(format=None, error_info_path=None, error_info_compress=None)

Get default middleware chain.

Parameters:

Name Type Description Default
format Optional[str]

traceback format. See TracebackMiddleware for details. Do not configure tracebacks if None.

None
error_info_path Optional[str]

Directory path to write error info. See ErrorInfoMiddleware for details. Do not configure middleware if None.

None
error_info_compress Optional[str]

Error info compression algorithm. Used along with error_info_path.

None

__fingerprint(t, v, stack)

Calculate the error fingerprint.

Calculate error fingerprint for given exception and the stack. Fingerprint is stable for repeating conditions.

Parameters:

Name Type Description Default
t Type[BaseException]

Exception type.

required
v BaseException

Exception instance:

required
stack List[FrameInfo]

Current stack.

required

Returns:

Type Description
UUID

Error fingerprint as UUID.

__must_die(t, v, tb)

Check if the error is fatal and the process must die.

Process fail-fast sequence and return True if the process must die quickly.

__process(t, v, tb=None)

Process given exception context.

Called either from .process() or as sys.excepthook for unhandled exceptions.

Parameters:

Name Type Description Default
t Type[BaseException]

Exception type.

required
v BaseException

Exception value.

required
tb Optional[TracebackType]

Traceback frame.

None

Raises:

Type Description
RuntimeError

If setup() is not called.

__run_middleware(err_info)

Process all the middleware.

Parameters:

Name Type Description Default
err_info ErrorInfo

Filled ErrorInfo structure

required

add_fail_fast(ff)

Add fail-fast handler to the end of the chain.

Parameters:

Name Type Description Default
ff BaseFailFast

BaseFailFast instance.

required

Raises:

Type Description
ValueError

If ff is not BaseFailFast instance.

add_middleware(mw)

Add middleware to the end of the chain.

Parameters:

Name Type Description Default
mw BaseMiddleware

BaseMiddleware instance

required

Raises:

Type Description
ValueError

If mw is not BaseMiddleware instance.

iter_fingerprint_parts(t, v, stack)

Iterate over the fingerprint parts.

Iterable to yield all fingerprint parts. May be overriden in subclasses.

Parameters:

Name Type Description Default
t Type[BaseException]

Exception type.

required
v BaseException

Exception instance:

required
stack List[FrameInfo]

Current stack.

required

Returns:

Type Description
Iterable[str]

Iterable of strings.

process()

Process current exception context in the fenced code block.

Example
from gufo.err import err

...
try:
    my_function()
except Exception:
    err.process()

setup(*, catch_all=False, root_module=None, name=DEFAULT_NAME, version=DEFAULT_VERSION, hash=DEFAULT_HASH, fail_fast=None, fail_fast_code=DEFAULT_EXIT_CODE, middleware=None, format='terse', error_info_path=None, error_info_compress=None)

Setup error handling singleton.

Must be called only once.

Parameters:

Name Type Description Default
catch_all bool

Install global system exception hook.

False
name str

Application or service name.

DEFAULT_NAME
version str

Application or service version.

DEFAULT_VERSION
root_module Optional[str]

Top-level application module/namespace for split stack fingerprinting. Topmost frame from the root or the nested modules will be considered in the error fingerprint.

None
hash str

Fingerprint hashing function name. Available functions are: sha256, sha3_512, blake2s, sha3_224, md5, sha384, sha3_256, shake_256, blake2b, sha224, shake_128, sha3_384, sha1, sha512. Refer to the Python's hashlib for details.

DEFAULT_HASH
fail_fast Optional[Iterable[BaseFailFast]]

Iterable of BaseFailFast instances for fail-fast detection. Process will terminate with fail_fast_code error code if any of instances in the chain will return True.

None
fail_fast_code int

System exit code on fail-fast termination.

DEFAULT_EXIT_CODE
middleware Optional[Iterable[BaseMiddleware]]

Iterable of BaseMiddleware instances for error processing middleware. Instances are evaluated in the order of appearance.

None
format Optional[str]

If not None install TracebackMiddleware for given output format.

'terse'
error_info_path Optional[str]

If not None install ErrorInfoMiddleware. error_info_path should point to a writable directories, in which the error info files to be written.

None
error_info_compress Optional[str]

Used only with error_info_path. Set error info compression method. One of:

  • None - do not compress
  • gz - GZip
  • bz2 - BZip2
  • xz - LZMA/xz
None

Returns:

Type Description
Err

Err instance.

Raises:

Type Description
RuntimeError

When called twice.

ValueError

On configuration parameters error.