|
| 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. |
0 commit comments