better-exceptions

repository·master·Indexed 26 days ago

https://github.com/qix-/better-exceptions

A Python library that provides pretty and more helpful exception formatting automatically via environment variables or manual hooks. It includes integrations for Django and unittest, as well as a way to run the Python REPL with enhanced exception formatting.

Tokens
1.1K
Snippets
5
Records
6
Agent score
39%

What's inside better-exceptions

  1. Integrate better-exceptions with Django

    master

    To use better_exceptions in Django, add better_exceptions.integrations.django.BetterExceptionsMiddleware to your MIDDLEWARE setting. To prevent duplicate logs (one from better_exceptions and one from Django), configure your LOGGING dictionary to use the skip_errors_filter from better_exceptions.integrations.django.

    # settings.py
    
    MIDDLEWARE = [
        # ...
        "better_exceptions.integrations.django.BetterExceptionsMiddleware",
    ]
    
    # ...
    
    from better_exceptions.integrations.django import skip_errors_filter
    
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'filters': {
            'skip_errors': {
                '()': 'django.utils.log.CallbackFilter',
                'callback': skip_errors_filter,
            }
        },
        'handlers': {
            'console': {
                'level': 'INFO',
                'filters': ['skip_errors'],
                'class': 'logging.StreamHandler',
            }
        },
        'loggers': {
            'django': {
                'handlers': [
                    'console',
                ],
            }
        }
    }
  2. Install and enable better-exceptions

    master

    Install better_exceptions via pip and enable it by setting the BETTER_EXCEPTIONS environment variable to any value. This automatically provides pretty and helpful exception formatting.

    pip install better_exceptions
    export BETTER_EXCEPTIONS=1  # Linux / OSX
    setx BETTER_EXCEPTIONS 1    # Windows
  3. Troubleshoot better-exceptions

    master

    If exceptions are not being formatted correctly, check the following:

    1. Environment Variable: Ensure BETTER_EXCEPTIONS is set. Use echo $BETTER_EXCEPTIONS (Linux/OSX) or echo %BETTER_EXCEPTIONS% (Windows) to verify.
    2. Conflicts: Check for other libraries overriding sys.excepthook. On Ubuntu, the python3-apport package may cause conflicts.
    3. Missing Hook File: Ensure better_exceptions_hook.pth exists in your Python package directory. If missing, reinstall the package.
    4. Manual Activation: You can manually trigger the hook at the start of your script using:
      import better_exceptions
      better_exceptions.hook()
  4. Use better-exceptions with unittest

    master

    To format unittest exception output using better_exceptions, you can monkey patch unittest.result.TestResult._exc_info_to_string.

    import sys
    import unittest
    import better_exceptions
    
    def patch(self, err, test):
        lines = better_exceptions.format_exception(*err)
        if sys.version_info[0] == 2:
            return u"".join(lines).encode("utf-8")
        return "".join(lines)
    
    unittest.result.TestResult._exc_info_to_string = patch
  5. Configure maximum output length

    master

    By default, better_exceptions truncates values. To allow the entirety of values to be outputted without truncation, set better_exceptions.MAX_LENGTH to None.

    import better_exceptions
    better_exceptions.MAX_LENGTH = None