Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

Add additional context to warnings and exceptions

Project description

extra-context

Sometimes, when you get warnings and exeptions, backtraces are just not enough. For example, you have a loop with 10.000 iterations. In the 1000th iteration there is an exception in a function you called. So you would like to have more context for the error message.

There are 2 options

  • give more context at the site the exception occured

    • Only possible if you control the exception/warning generation
    • Sometimes, there is not enough context to begin with (i.e. you are calling some common utility function)
  • Provide additional context as the exception/warning is passed up the call stack

    • Can be "opt-in" in user-code, no changes in libraries required.
    • Also works for warnings (which normally don't provide backtrace information)

Examples

The main example that inspired writing this library:

to_process = { "file1": {...}, "file2": {...}, ...}

for k, v in to_process.items():
    cleaned = clean_data(v)
    write_output(k, cleaned)

If there is an exception or warning somewhere in clean_data(v), the "context" i.e. from which file the data came is lost. The backtrace could look something like:

  File "clean.py", line 35, in clean_data
ZeroDivisionError: division by zero

Now, with this library you can give extra context to your error messages

from extra_context import provide_extra_context

to_process = { "file1": {...}, "file2": {...}, ...}

for k, v in to_process.items():
    with provide_extra_context(filename=k):
        cleaned = clean_data(v)
        write_output(k, cleaned)

This would report the same error as:

  File "clean.py", line 35, in clean_data
ZeroDivisionError: division by zero
  |- In context: filename='file4'

Decorator

There is also a decorator that reports the function arguments in case of an error

import warnings
from extra_context import provide_call_context

@provide_call_context
def w(x):
    warnings.warn("Oops")

w(42)

This reports:

UserWarning: Oops
  |- In context: w(42)
  warnings.warn("Oops")

License

Copyright 2018 Moritz Wanzenböck

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Release history Release notifications

This version
History Node

1.0.0

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
extra_context-1.0.0-py3-none-any.whl (4.7 kB) Copy SHA256 hash SHA256 Wheel py3
extra-context-1.0.0.tar.gz (4.3 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page