Skip to main content

A simple python exception reporter

Project description

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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-exceptions-1.2.2.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_exceptions-1.2.2-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file py-exceptions-1.2.2.tar.gz.

File metadata

  • Download URL: py-exceptions-1.2.2.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for py-exceptions-1.2.2.tar.gz
Algorithm Hash digest
SHA256 356915486ad6fb60c5d27b5ce7753c20a02d55b244cc77a005b7e82bed122a6c
MD5 cba38fac7163824c46d9ea3b5faa6a34
BLAKE2b-256 545205bab7974a43362072cdf75f8cecbfed9eaba1d315aedf5e3184c7727bd3

See more details on using hashes here.

File details

Details for the file py_exceptions-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: py_exceptions-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for py_exceptions-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 780ea895136fd04f940ac329639ebb4b801f98f89abf3acf24e4dc827cc90d65
MD5 e93598857389669bc5fb6a4fc0c72165
BLAKE2b-256 f3e9ff0e7cbdf73bf9874541b50cf932e25d48469cc9f031d4251fba857dd77f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page