Skip to main content

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


Download files

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

Source Distribution

extra-context-1.0.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

extra_context-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file extra-context-1.0.0.tar.gz.

File metadata

  • Download URL: extra-context-1.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for extra-context-1.0.0.tar.gz
Algorithm Hash digest
SHA256 58efaf35bc1179593f5d2e03fe31e8d1646284417963bf30c9023298b8932314
MD5 40051bd9c0b6c89a152f01aa22816d49
BLAKE2b-256 c6eaf41e829743b564715e891c2a9749cc628eb16d952af0a197947e83fac2e2

See more details on using hashes here.

File details

Details for the file extra_context-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: extra_context-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for extra_context-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ee44bc2b6bfaf35815f24319b4cb40ac4cf004c0e6cb861cfe0c1d52dec21f6
MD5 cf65d8589e1f0a764ac74f4ee8cd68f4
BLAKE2b-256 b0d89f3d7e5ce680769faadb88349f3a39d22c374ebae5a0b167802773c20556

See more details on using hashes here.

Supported by

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