Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 7ecb236

Browse files
Add .gitignore, README, requirements, and update example imports
1 parent 50e9bcf commit 7ecb236

File tree

5 files changed

+210
-2
lines changed

5 files changed

+210
-2
lines changed

.gitignore

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
__pycache__/
2+
*.py[cod]
3+
*$py.class
4+
*.so
5+
.Python
6+
build/
7+
develop-eggs/
8+
dist/
9+
downloads/
10+
eggs/
11+
.eggs/
12+
lib/
13+
lib64/
14+
parts/
15+
sdist/
16+
var/
17+
wheels/
18+
share/python-wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
MANIFEST
23+
*.manifest
24+
*.spec
25+
pip-log.txt
26+
pip-delete-this-directory.txt
27+
htmlcov/
28+
.tox/
29+
.nox/
30+
.coverage
31+
.coverage.*
32+
.cache
33+
nosetests.xml
34+
coverage.xml
35+
*.cover
36+
*.py,cover
37+
.hypothesis/
38+
.pytest_cache/
39+
cover/
40+
*.mo
41+
*.pot
42+
*.log
43+
local_settings.py
44+
db.sqlite3
45+
db.sqlite3-journal
46+
instance/
47+
.webassets-cache
48+
.scrapy
49+
docs/_build/
50+
.pybuilder/
51+
target/
52+
.ipynb_checkpoints
53+
profile_default/
54+
ipython_config.py
55+
.pdm.toml
56+
.pdm-python
57+
.pdm-build/
58+
__pypackages__/
59+
celerybeat-schedule
60+
celerybeat.pid
61+
*.sage.py
62+
.env
63+
.venv
64+
env/
65+
venv/
66+
ENV/
67+
env.bak/
68+
venv.bak/
69+
.spyderproject
70+
.spyproject
71+
.ropeproject
72+
/site
73+
.mypy_cache/
74+
.dmypy.json
75+
dmypy.json
76+
.pyre/
77+
.pytype/
78+
cython_debug/

.idea/ExceptionHandler.iml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Exception Handler
2+
3+
A Python-based exception handling utility that provides customizable exception messages,
4+
formatting, and behavior.
5+
6+
This project allows developers to handle exceptions with enhanced readability and flexibility.
7+
It is designed to be easy to use and integrate into existing projects,
8+
providing a consistent way to manage exceptions across your codebase.
9+
10+
## Features
11+
12+
- **Customizable Exception Handling**: Configure global and local exception handling settings.
13+
- **Traceback and Line Number**: Optionally include traceback and line numbers in exception messages.
14+
- **Timestamp Support**: Add timestamps to exception messages with customizable formats.
15+
- **Color and Format Customization**: Customize colors and text formats for exception messages, tracebacks, and timestamps.
16+
- **Global and Local Settings**: Override global settings with local configurations for specific exceptions.
17+
- **Script Exit Control**: Control whether the script exits after handling an exception.
18+
- **Return or Print Messages**: Choose to return exception messages as strings or print them directly.
19+
20+
## Installation
21+
22+
Clone the repository and ensure you have Python installed.
23+
Recommended to use Python 3.11 or higher.
24+
25+
```bash
26+
git clone https://github.com/DefinetlyNotAI/ExceptionHandler.git
27+
cd ExceptionHandler
28+
```
29+
30+
## Usage
31+
32+
### Initialize the Handler
33+
34+
```python
35+
from exception import Handler, Colors, Format
36+
37+
# Initialize the Handler with custom settings - These are global settings
38+
# NOTE: return_string_rather_than_print has priority over exit_script both locally and globally!
39+
handler = Handler(
40+
show_line=True, # Show the line number where the exception occurred
41+
trace=True, # Include the traceback in the output
42+
use_timestamp=True, # Add a timestamp to the exception message
43+
exit_script=True, # Exit the script after handling the exception
44+
return_string_rather_than_print=True
45+
# Return the message instead of printing it,
46+
# if True it will override/ignore exit_script
47+
)
48+
49+
# Customize the formatter
50+
# NOTE: Specific settings always the defaults,
51+
# example if message_color is set it will override the main_color for the message part,
52+
handler.formatter(
53+
main_color=Colors.RED, # Set the main color to red
54+
message_color=Colors.YELLOW, # Set the message color to yellow
55+
trace_color=Colors.CYAN, # Set the traceback color to cyan
56+
timestamps_color=Colors.GREEN, # Set the timestamp color to green
57+
main_format=Format.BOLD, # Set the main format to bold
58+
message_format=Format.UNDERLINE, # Underline the message text
59+
trace_format=Format.DIM, # Dim the traceback text
60+
timestamps_format=Format.BLINK, # Blink the timestamp text
61+
datetime_format="%d-%m-%Y %H:%M:%S" # Customize the datetime format
62+
)
63+
```
64+
65+
### Example Function
66+
67+
```python
68+
# Bare Minimum
69+
from exception import Handler
70+
71+
handler = Handler() # Use default settings
72+
73+
def divide_numbers(a, b):
74+
try:
75+
return a / b
76+
except Exception as e:
77+
# Note: You can just use handler.exception() or handler.exception(msg=e) to rely on global settings
78+
handler.exception(
79+
# These are local settings, if they are different from the global, they take priority
80+
# so they will override the global settings only for this case.
81+
msg=e, # Detailed exception message, can be a custom string
82+
exit_script=True, # Exit the script after handling the exception
83+
quit_code=1, # Exit code to use when exiting the script, defaults to 1
84+
return_string_rather_than_print=False # Return the message instead of printing it
85+
# NOTE: If return_string_rather_than_print is set to True globally,
86+
# and you want to locally quit for this singular case,
87+
# then you HAVE TO explicitly set it to False here.
88+
)
89+
90+
divide_numbers(10, 0)
91+
```
92+
93+
## Customization
94+
95+
- **Global Settings**: Set during `Handler` initialization.
96+
- **Local Settings**: Override global settings in the `exception` method.
97+
- **Formatter**: Customize colors and formats using the `formatter` method.
98+
99+
Check the [`example.py`](example.py) file for a proper example.
100+
101+
### Override priority
102+
103+
- Local settings take precedence over global settings.
104+
- If both global and local settings are provided, the local settings will override the global ones.
105+
- To use the global settings, simply call `handler.exception()`/`handler.exception(msg=e)`.
106+
- Any settings you want to override for this specific use case can be passed as arguments.
107+
- `return_string_rather_than_print` has priority over `exit_script` both locally and globally.
108+
- If `return_string_rather_than_print` is set to `True` globally, and you want to locally quit for this singular case, then you HAVE TO explicitly set it to `False` in the local settings.
109+
110+
### Modify Colors, Format or Messages
111+
112+
If you want to modify the DataType for format's or colors ANSI, you can do so by modifying the `Format` and `Colors` classes in the [`exception.py`](exception/colors.py) file.
113+
114+
If you want to add/remove Messages from the dictionary, you can do so by modifying the `Messages` class in the [`messages.py`](exception/messages.py) file.
115+
116+
## Dependencies
117+
118+
- Datetime
119+
120+
## License
121+
122+
This project is licensed under the MIT License. See the [`LICENSE`](LICENSE) file for details.
123+
124+
## Contributing
125+
126+
Contributions are welcome! Please fork the repository and submit a pull request.

example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from exception.handler import Handler
2-
from exception.colors import Colors, Format
1+
from exception import Handler, Colors, Format
32

43
# ALL BOOLEAN SETTINGS ARE FALSE BY DEFAULT,
54
# THE DEFAULT PRINT FUNCTION IS 'print()',

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DateTime~=5.5

0 commit comments

Comments
 (0)