Crashtest
Crashtest is a Python library designed to simplify the handling and inspection of exceptions, providing tools to manage Python errors with ease. It is currently at version 0.4.1 and has a moderate release cadence, with recent updates focused on stability and type hinting.
Common errors
-
ModuleNotFoundError: No module named 'crashtest'
cause The 'crashtest' library is not installed in your Python environment or is not accessible on the Python path.fixInstall the library using pip: `pip install crashtest` -
TypeError: Crash() missing 1 required positional argument: 'exception_or_frame'
cause The `crashtest.Crash` class was instantiated without providing the mandatory `exception_or_frame` argument, which expects an exception instance or a frame object.fixWhen creating a `crashtest.Crash` object, provide an exception instance (e.g., caught in an `except` block) or a frame object: `crashtest.Crash(e)` or `crashtest.Crash(sys.exc_info()[2].tb_frame)`. -
AttributeError: 'Crash' object has no attribute 'some_invalid_attribute'
cause You are attempting to access an attribute or method on a `crashtest.Crash` instance that does not exist or is misspelled.fixConsult the `crashtest` documentation for the correct attributes and methods available on the `Crash` object, or use `dir(crash_instance)` to inspect its members.
Warnings
- breaking Crashtest versions 0.4.0 and newer require Python 3.7 or higher. Users on Python 3.6 or older will encounter compatibility issues when upgrading.
- gotcha Version 0.4.0 introduced stricter type hinting. A 'too narrow type' in the `Inspector` class was fixed in 0.4.1, suggesting that `0.4.0` might have introduced new type-related errors if existing code passed unexpected types.
- gotcha Prior to version 0.4.0, `crashtest` could encounter decoding errors when opening files due to the lack of explicit 'utf-8' encoding.
- gotcha In versions prior to 0.3.1, there was an error when attempting to retrieve the line of a frame if it had no context.
- breaking The `Inspector` class was moved from being directly available under the `crashtest` package to `crashtest.inspector` in version 0.4.0. Attempts to import it directly via `from crashtest import Inspector` will result in an `ImportError` in versions 0.4.0 and newer.
Install
-
pip install crashtest
Imports
- Inspector
from crashtest import Inspector
- ErrorHandler
from crashtest import ErrorHandler
Quickstart
import sys
from crashtest import Inspector
def might_fail():
a = 1
b = 0
return a / b
try:
might_fail()
except Exception as e:
inspector = Inspector(e)
print(f"Exception type: {inspector.type}")
print(f"Exception message: {inspector.message}")
print("Stack Trace:")
for frame in inspector.frames:
print(f" File: {frame.file}, Line: {frame.lineno}, Function: {frame.name}")
print(f" Code: {frame.line.strip() if frame.line else 'N/A'}")