File: _errors.py

package info (click to toggle)
python-eliot 1.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 8,641; makefile: 151
file content (58 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Error-handling utility code.
"""

from inspect import getmro


class ErrorExtraction(object):
    """
    Extract fields from exceptions for failed-action messages.

    @ivar registry: Map exception class to function that extracts fields.
    """

    def __init__(self):
        self.registry = {}

    def register_exception_extractor(self, exception_class, extractor):
        """
        Register a function that converts exceptions to fields.

        @param exception_class: Class to register for.

        @param extractor: Single-argument callable that takes an exception
            of the given class (or a subclass) and returns a dictionary,
            fields to include in a failed action message.
        """
        self.registry[exception_class] = extractor

    def get_fields_for_exception(self, logger, exception):
        """
        Given an exception instance, return fields to add to the failed action
        message.

        @param logger: ``ILogger`` currently being used.
        @param exception: An exception instance.

        @return: Dictionary with fields to include.
        """
        for klass in getmro(exception.__class__):
            if klass in self.registry:
                extractor = self.registry[klass]
                try:
                    return extractor(exception)
                except:
                    from ._traceback import write_traceback

                    write_traceback(logger)
                    return {}
        return {}


_error_extraction = ErrorExtraction()
register_exception_extractor = _error_extraction.register_exception_extractor
get_fields_for_exception = _error_extraction.get_fields_for_exception

# Default handler for OSError and IOError by registered EnvironmentError:
register_exception_extractor(EnvironmentError, lambda e: {"errno": e.errno})