Skip to main content

Package for customizing exceptions and warnings messages.

Project description

Custom exception and warning formatter PyPI


Exceptions

Define exception format by subclassing CustomTraceback

class MyTracebackFormatter(CustomTraceback):
    _EXC_FORMAT = '| {traceback}>> {type}: {exception}.\n'
    _TB_FORMAT = '[{file}::{func}]@{line} "{code}" | '
    _EXC_HOOK_HEAD_TEXT = 'Error:'

1. _EXC_FORMAT used for formatting single exception in exceptions chain:
_EXC_FORMAT should be python formatted string literal with values in {} brackets. Available values for _EXC_FORMAT formatted string literal:

  • traceback: formatted traceback goes here (see below)
  • type: exception class name
  • exception: exception message

2. _TB_FORMAT used for formatting single traceback frame in traceback chain:
_TB_FORMAT should be python formatted string literal with values in {} brackets. Available values for _TB_FORMAT formatted string literal:

  • file: python .py file stem where error occurred
  • func: function or module where error occurred
  • line: number of the line of code where error occurred
  • code: line of code itself

3. _EXC_HOOK_HEAD_TEXT used as header for exceptions
_EXC_HOOK_HEAD_TEXT should be python string

4. Additional variables to define:

  • _EXC_OUT: output for exceptions, sys.stderr by default
  • _EXC_CHAIN: True - chain exceptions, False - only last exception, by default True
  • _EXC_REVERSE: order of chained exceptions, True - show like default python exception hook, by default False

Usage:

  1. Definition
class MyTracebackFormatter(CustomTraceback):
    _TB_FORMAT = '[{file}::{func}]@{line} "{code}" | '
    _EXC_FORMAT = '| {traceback}>> {type}: {exception}.\n'
    _EXC_HOOK_HEAD_TEXT = 'Error:'
    _EXC_OUT = sys.stderr
    _EXC_CHAIN = True
    _EXC_REVERSE = False
  1. Get formatted exception string
exc_str = MyTracebackFormatter(exc)
  1. Print formatted exception
MyTracebackFormatter.print_exception(exc)
  1. Use actvate() method to alter way of presenting tracebacks for system, modules(i.e. logging) and apps:
MyTracebackFormatter.activate()

It is equivalent of executing these statements:

sys.excepthook = MyTracebackFormatter.exception_hook
traceback.print_exception = MyTracebackFormatter.traceback_print_exception_hook
traceback.format_exception = MyTracebackFormatter.traceback_format_exception_hook

Predefined traceback formatters

  1. compact_tb
from mwk_traceback import compact_tb

compact_tb.activate()

main()  # ! check test_main.py for code !
test_tb_pr_exc()  # ! check test_traceback_print_exception.py for code !

Output:

! Error(s):
| Error in [test_main.py] in [<module>] at line (27) while executing "main()"
| Error in [test_main.py] in [main] at line (22) while executing "func()"
| Error in [test_main.py] in [func] at line (17) while executing "raise"
   >> NameError: error in func.
| Error in [test_main.py] in [func] at line (15) while executing "func_func()"
| Error in [test_main.py] in [func_func] at line (10) while executing "raise"
   >> AttributeError: error in func_func.
| Error in [test_main.py] in [func_func] at line (8) while executing "func_func_func()"
| Error in [test_main.py] in [func_func_func] at line (3) while executing "x = 1 / 0"
   >> ZeroDivisionError: division by zero.
 ----------------------------------------------------------------------------------------------------------  
ERROR:root:logging error
ERROR:root:logging exception
| Error in [test_traceback_print_exception.py] in [test_tb_pr_exc] at line (13) while executing "x = 1 / 0"
   >> ZeroDivisionError: division by zero.

ERROR:root:There was no exception
No exception is being handled.
  1. super_compact_tb
from mwk_traceback import super_compact_tb

super_compact_tb.activate()

main()  # ! check test_main.py for code !
test_tb_pr_exc()  # ! check test_traceback_print_exception.py for code !

Output:

! Error:
| [test_main::<module>]@31 "main()" | [test_main::main]@22 "func()" | [test_main::func]@17 "raise" | >> NameError: error in func.
| [test_main::func]@15 "func_func()" | [test_main::func_func]@10 "raise" | >> AttributeError: error in func_func.
| [test_main::func_func]@8 "func_func_func()" | [test_main::func_func_func]@3 "x = 1 / 0" | >> ZeroDivisionError: division by zero.
 ----------------------------------------------------------------------------------------------------------
ERROR:root:logging error
ERROR:root:logging exception
| [test_traceback_print_exception::test_tb_pr_exc]@14 "x = 1 / 0" | >> ZeroDivisionError: division by zero.

ERROR:root:There was no exception
No exception is being handled

Warnings

Define warning format by subclassing CustomWarningFormatter:

class MyWarningFormatter(CustomWarningFormatter):
    _WARN_FORMAT = '[{file}]@{line} >> {type}: {message}\n'

_WARN_FORMAT should be python formatted string literal with values in {} brackets.
Available values for _WARN_FORMAT formatted string literal:

  • message: warning message
  • type: warning class name
  • file: python .py file stem where warning occurred
  • line: number of the line of code where warning occurred

Usage:

import warnings
warnings.formatwarning = MyWarningFormatter

warnings.warn('This is warning', UserWarning)

Predefined warning formatters (classes)

  1. compact_warn
import warnings
from mwk_traceback import compact_warn
warnings.formatwarning = compact_warn

warnings.warn('This is warning', RuntimeWarning)

Output:

Warning in [test_warn.py] at line (9)
   >> RuntimeWarning: This is warning
  1. super_compact_warn
import warnings
from mwk_traceback import super_compact_warn
warnings.formatwarning = super_compact_warn

warnings.warn('This is another warning', UserWarning)

Output:

[test_warn]@13 >> UserWarning: This is another warning

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

mwk_traceback-2.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

mwk_traceback-2.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file mwk_traceback-2.0.tar.gz.

File metadata

  • Download URL: mwk_traceback-2.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for mwk_traceback-2.0.tar.gz
Algorithm Hash digest
SHA256 7cfd11386acbf6c58255b75f50bec62d9e745e6e6f2ee1e1a13c1b2a0e24eb24
MD5 8323020015f50c45ee2cd06c73b40246
BLAKE2b-256 1e83623242d71d8783f858ce6691d28714612bf13ffd9ec5e1c5a75122391484

See more details on using hashes here.

File details

Details for the file mwk_traceback-2.0-py3-none-any.whl.

File metadata

  • Download URL: mwk_traceback-2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for mwk_traceback-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 523376fa22f128ef8e4ae5f52bfa6e1461b8d401ec88d29e561e57264eb32bb6
MD5 684f0389349e88fb0585568324147c7b
BLAKE2b-256 08532b2abc7ca219485d8b4085bfc12275588fc40da1054a178e839ff23f2442

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