Skip to main content

Pretty and helpful exceptions, automatically

Project description

better-exceptions

Pretty and more helpful exceptions in Python, automatically.

Example screenshot of exceptions

Usage

Install better_exceptions via pip:

pip install better_exceptions

And set the BETTER_EXCEPTIONS environment variable to any value:

export BETTER_EXCEPTIONS=1  # Linux / OSX
setx BETTER_EXCEPTIONS 1    # Windows

That's it!

Python REPL (Interactive Shell)

In order to use better_exceptions in the Python REPL, first install the package (as instructed above) and run:

$ python -m better_exceptions
Type "help", "copyright", "credits" or "license" for more information.
(BetterExceptionsConsole)
>>>

in order to drop into a better_exceptions-enabled Python interactive shell.

Advanced Usage

If you want to allow the entirety of values to be outputted instead of being truncated to a certain amount of characters:

import better_exceptions
better_exceptions.MAX_LENGTH = None

While using better_exceptions in production, do not forget to unset the BETTER_EXCEPTIONS variable to avoid leaking sensitive data in your logs.

Use with unittest

If you want to use better_exceptions to format unittest's exception output, you can use the monkey patch below:

import unittest
import better_exceptions

def patch(self, err, test):
    lines = better_exceptions.format_exception(*err)
    return "".join(lines)

unittest.result.TestResult._exc_info_to_string = patch

Note that this uses an undocumented method override, so it is not guaranteed to work on all platforms or versions of Python.

Django Usage

In settings.py, add your new class to the MIDDLEWARE setting and update your logging configuration:

# ...

MIDDLEWARE = [
    # ...
    "better_exceptions.integrations.django.BetterExceptionsMiddleware",
]

# ...

from better_exceptions.integrations.django import skip_errors_filter

# if you don't want to override LOGGING because you want to change the default,
# you can vendor Django's default logging configuration and update it for
# better-exceptions. the default for Django 3.1.4 can be found here:
# https://github.com/django/django/blob/3.1.4/django/utils/log.py#L13-L63
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'skip_errors': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': skip_errors_filter,
        }
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            # without the 'filters' key, Django will log errors twice:
            # one time from better-exceptions and one time from Django.
            # with the 'skip_errors' filter, we remove the repeat log
            # from Django, which is unformatted.
            'filters': ['skip_errors'],
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        'django': {
            'handlers': [
                'console',
            ],
        }
    }
}

example output:

image

Troubleshooting

If you do not see beautiful exceptions, first make sure that the environment variable does exist. You can try echo $BETTER_EXCEPTIONS (Linux / OSX) or echo %BETTER_EXCEPTIONS% (Windows). On Linux and OSX, the export command does not add the variable permanently, you will probably need to edit the ~/.profile file to make it persistent. On Windows, you need to open a new terminal after the setx command.

Check that there is no conflict with another library, and that the sys.excepthook function has been correctly replaced with the better_exceptions's one. Sometimes other components can set up their own exception handlers, such as the python3-apport Ubuntu package that you may need to uninstall.

Make sure that you have not inadvertently deleted the better_exceptions_hook.pth file that should be in the same place as the better_exceptions folder where all of your Python packages are installed. Otherwise, try re-installing better_exceptions.

You can also try to manually activate the hook by adding import better_exceptions; better_exceptions.hook() at the beginning of your script.

Finally, if you still can not get this module to work, open a new issue by describing your problem precisely and detailing your configuration (Python and better_exceptions versions, OS, code snippet, interpreter, etc.) so that we can reproduce the bug you are experiencing.

License

Copyright © 2017, Josh Junon. Licensed under the MIT license.

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

better_exceptions_xl0-0.4.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

better_exceptions_xl0-0.4.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file better_exceptions_xl0-0.4.0.tar.gz.

File metadata

  • Download URL: better_exceptions_xl0-0.4.0.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for better_exceptions_xl0-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a78756694230ef0fb3fda42b7847855e88c9e90441bfe49ed19b29c326ab4f0c
MD5 9a41e4743a246509ce3bcd6ca291318d
BLAKE2b-256 51c299f48938dc73c81e7257c70addd332746dc9412985144e9b2b60abf5abde

See more details on using hashes here.

Provenance

The following attestation bundles were made for better_exceptions_xl0-0.4.0.tar.gz:

Publisher: publish.yml on xl0/better-exceptions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file better_exceptions_xl0-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for better_exceptions_xl0-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95fbe75b0fa30b20b630ffa605ba1762bf283c34a04a93e0b3797357851091ff
MD5 f73b58d81410f57e5a877be142db18af
BLAKE2b-256 f6705b1d2b03981762a53c41adf5f444bea2e0074ddbb5b2d880a2568628ea1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for better_exceptions_xl0-0.4.0-py3-none-any.whl:

Publisher: publish.yml on xl0/better-exceptions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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