Skip to main content

py-exceptions PyPI version

A simple python exception reporter

Description

This library provides great stacktrace and web request information like Django does It can save it to html, return html to your code or even response in AWS lambda format

The library nicely covers all your secret variables and request data in its report

Screenshots

Beautiful image

Another beautiful image

Quickstart

Installation

pip install py-exception

Simple example

Add decorator to function

from pyexceptions import handle_exceptions


def divide(a, b):
    return a / b


@handle_exceptions
def main():
    i = 5
    j = 0
    c = divide(i, j)
    print(c)


if __name__ == '__main__':
    main()

You can also override folder for exception reports

from pyexceptions import handle_exceptions


def divide(a, b):
    return a / b


@handle_exceptions(exceptions_folder=f'./SomeFolderPath')
def main():
    i = 5
    j = 0
    c = divide(i, j)
    print(c)


if __name__ == '__main__':
    main()

AWS Lambda example

It is hard to determine what's went wrong when you are using AWS lambda. So you can use the example not only to get full stacktrace but to get lambda event and context information:

from pyexceptions import handle_exceptions


@handle_exceptions(is_lambda=True)
def lambda_handler(event, context):
    message = f"Hello {event['first_name']} {event['last_name']}!"
    return {
        'message': message
    }

Exclude from stacktrace

There may be situations when you don't want to see part of stacktrace

So if your application looks like this:

from pyexceptions import handle_exceptions

def divide(a, b):
    return a / b

def real_main():
    i = 5
    j = 0
    c = divide(i, j)
    print(c)

def wrapper():
    real_main()

@handle_exceptions(exclude = 3)
def main():
    wrapper()

if __name__ == '__main__':
    main()

and you want to exclude all stacktrace from main to wrapper you need to pass file_name.function_name as exclude argument

Other functions

You can also want to use these functions:

Make function that returns HTML and don't save the result:

from pyexceptions import handle_exceptions


@handle_exceptions(save=False)
def main():
    ...

Make function return production html:

from pyexceptions import handle_exceptions


@handle_exceptions(production=True)
def main():
    ...

Beautiful image

Or you may want to write your own logic To do so you need to import the ExceptionHandler class

from pyexceptions import ExceptionHandler

That's how it looks like:

class ExceptionHandler:
    """Organize and coordinate reporting on exceptions."""

    def __init__(self, lambda_event: dict = None, context: object = None, exclude: int = 1, production: bool = False):
        """Exception reporter initializer

        Args:
            lambda_event (dict, optional): AWS lambda event. Defaults to None.
            context (object, optional): AWS lambda context. Defaults to None.
            exclude (int, optional): Determines how many frames of traceback to exclude. Defaults None.
            production (bool, optional): Determines if handler should be enabled. Defaults False.
        """
        self.__reporter = ExceptionReporter(lambda_event=lambda_event, context=context, exclude=exclude, # noqa
                                            production=production)

    def get_traceback_html(self):
        """Return HTML version of debug 500 HTTP error page."""
        return self.__reporter.get_traceback_html()

    def get_traceback_lambda(self):
        """Return AWS lambda version of debug 500 HTTP error page."""
        return self.__reporter.get_lambda_response()

Attribution

This implementation draws upon work from:

Metadata

Release files for py-exceptions 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-exceptions 1.2.2
File Size Uploaded
py-exceptions-1.2.2.tar.gz 19.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-exceptions 1.2.2
File Interpreter ABI Platform
py_exceptions-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 41.0 kB

Release files / py-exceptions-1.2.2.tar.gz

Download URL py-exceptions-1.2.2.tar.gz
Size 19.5 kB
Tags Source
SHA-256 checksum
How to use checksums
356915486ad6fb60c5d27b5ce7753c20a02d55b244cc77a005b7e82bed122a6c
BLAKE2b-256 checksum
How to use checksums
545205bab7974a43362072cdf75f8cecbfed9eaba1d315aedf5e3184c7727bd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

Release files / py_exceptions-1.2.2-py3-none-any.whl

Download URL py_exceptions-1.2.2-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
780ea895136fd04f940ac329639ebb4b801f98f89abf3acf24e4dc827cc90d65
BLAKE2b-256 checksum
How to use checksums
f3e9ff0e7cbdf73bf9874541b50cf932e25d48469cc9f031d4251fba857dd77f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page