la-catch
Decorator and context manager to catch exception(s) and call a function to handle the error.
install
pip install la-catch
usage
There is two ways to use catch:
- Context Manager
- Decorator
context manager
Catch exception and do nothing:
from la_catch import Catch
def func():
with Catch(TypeError):
raise TypeError("example")
Catch exception and call callback passing exception:
from la_catch import Catch
def on_error(exception):
print(exception)
def func():
with Catch(TypeError, on_error):
raise TypeError("example")
Catch exception and call callback passing keywords arguments and exception:
from la_catch import Catch
def on_error(message, exception):
print(message, exception)
def func():
with Catch(TypeError, on_error, "WARNING:"):
raise TypeError("example")
Catch multiple exceptions:
from la_catch import Catch
def func():
with Catch((TypeError, FileNotFoundError)):
raise FileNotFoundError("example")
decorator
Make sure that the callback signature identical to the function signature.
Catch exception and return None:
from la_catch import Catch
@Catch(TypeError)
def func():
raise TypeError("example")
Catch exception and return a value:
from la_catch import Catch
@Catch(TypeError, 10)
def func():
raise TypeError("example")
Catch exception and call callback passing exception:
from la_catch import Catch
def on_error(exception):
print(exception)
@Catch(TypeError, on_error)
def func():
raise TypeError("example")
Catch exception and call callback passing decorated function arguments and exception:
from la_catch import Catch
def on_error(message, exception):
print(message, exception)
@Catch(TypeError, on_error)
def func(message="WARNING:"):
raise TypeError("example")
Chain catchs:
from la_catch import Catch
def on_file_not_found_error(exception):
print(exception)
def on_typerror(exception):
print(exception)
@Catch(FileNotFoundError, on_file_not_found_error)
@Catch(TypeError, on_typerror)
def func():
raise FileNotFoundError("example")
raise TypeError("example")
notes
Initialization arguments are used by context manager and ignored by decorators, because:
- Would have to decide a priority between intialization arguments and function arguments
- Passing default values to callback would need me to know the function arguments
- When chaining decorators, wasn't possible to discover the function parameters because decorators would give
(*args, **kwargs) - When chaining decorators, wasn't possible to discover the default values because decorators would give
(*args, **kwargs)
- When chaining decorators, wasn't possible to discover the function parameters because decorators would give
Release files for la-catch 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| la-catch-0.0.6.tar.gz | 5.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| la_catch-0.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.9 kB
Release files / la-catch-0.0.6.tar.gz
| Download URL | la-catch-0.0.6.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
072a37c4b7458b90adc8fe3a381d36ef81c906eee449160b76ca33bf99fdc459
|
|
BLAKE2b-256 checksum How to use checksums |
0406435caf4d753d9f2e9adbd69b6f5857e48e9502dc17225c97b6e0d366fb8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.1.3 CPython/3.10.6
|
Release files / la_catch-0.0.6-py3-none-any.whl
| Download URL | la_catch-0.0.6-py3-none-any.whl |
|---|---|
| Size | 3.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
96e59940452ea35485838eba7c0c4fe49ea397cf1b753785d63814343cedd6ce
|
|
BLAKE2b-256 checksum How to use checksums |
6b6df4d77d1f9e35b327280940979ee65585c3ba3c04b0de6a54192c2d80d2e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.1.3 CPython/3.10.6
|