├── .vscode └── settings.json ├── README.md ├── py-error ├── README.md ├── examples │ ├── __init__.py │ └── example_usage.py ├── requirements.txt ├── setup.py ├── tests │ ├── __init__.py │ ├── test_error_handler.py │ └── test_logger.py └── toolkit │ ├── __init__.py │ ├── error_handler.py │ └── logger.py ├── setup.py └── structure.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[python]": { 3 | "editor.defaultFormatter": "ms-python.autopep8" 4 | }, 5 | "python.formatting.provider": "none" 6 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **`README.md`** 2 | # Error Handling and Logging Toolkit for Python 3 | 4 | A Python toolkit that provides enhanced error handling and logging capabilities, making it easier to identify and debug runtime errors when they occur. 5 | 6 | ## Features 7 | 8 | - Custom error handling with `CustomError` class. 9 | - Flexible error logging with customizable log formats. 10 | - Example usage and unit tests included. 11 | 12 | ## Installation 13 | 14 | You can install the toolkit using pip: 15 | 16 | ```bash 17 | pip install py-error-handling-toolkit 18 | ``` 19 | 20 | ## Usage 21 | 22 | Here's how you can use the toolkit in your Python code: 23 | 24 | ```python 25 | from toolkit.error_handler import CustomError, handle_error 26 | from toolkit.logger import setup_logger 27 | 28 | # Initialize the logger 29 | logger = setup_logger('error.log') 30 | 31 | try: 32 | # Code that may raise exceptions 33 | raise CustomError("This is a custom error.") 34 | except CustomError as ce: 35 | handle_error(ce, log_function=logger.error) 36 | except Exception as e: 37 | handle_error(e, log_function=logger.error) 38 | else: 39 | logger.info("No errors occurred.") 40 | ``` 41 | 42 | For more detailed usage instructions, see the [full documentation](https://github.com/yourusername/py-error-handling-toolkit). 43 | 44 | ## Contributing 45 | 46 | Contributions are welcome! Please check out the [contribution guidelines](CONTRIBUTING.md) for details on how to contribute to this project. 47 | 48 | ## License 49 | 50 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 51 | -------------------------------------------------------------------------------- /py-error/README.md: -------------------------------------------------------------------------------- 1 | The next file is `README.md`, which is used to provide documentation and instructions on how to use your toolkit. Here's a basic structure for your README.md file: 2 | 3 | **`README.md`** 4 | 5 | ```markdown 6 | # Error Handling and Logging Toolkit for Python 7 | 8 | [![Build Status](https://travis-ci.org/yourusername/py-error-handling-toolkit.svg?branch=master)](https://travis-ci.org/pb2204/py-error-handling-toolkit) 9 | [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/pb2204/py-error-handling-toolkit/blob/master/LICENSE) 10 | 11 | A Python toolkit that provides enhanced error handling and logging capabilities, making it easier to identify and debug runtime errors when they occur. 12 | 13 | ## Features 14 | 15 | - Custom error handling with `CustomError` class. 16 | - Flexible error logging with customizable log formats. 17 | - Example usage and unit tests included. 18 | 19 | ## Installation 20 | 21 | You can install the toolkit using pip: 22 | 23 | ```bash 24 | pip install py-error-handling-toolkit 25 | ``` 26 | 27 | ## Usage 28 | 29 | Here's how you can use the toolkit in your Python code: 30 | 31 | ```python 32 | from toolkit.error_handler import CustomError, handle_error 33 | from toolkit.logger import setup_logger 34 | 35 | # Initialize the logger 36 | logger = setup_logger('error.log') 37 | 38 | try: 39 | # Code that may raise exceptions 40 | raise CustomError("This is a custom error.") 41 | except CustomError as ce: 42 | handle_error(ce, log_function=logger.error) 43 | except Exception as e: 44 | handle_error(e, log_function=logger.error) 45 | else: 46 | logger.info("No errors occurred.") 47 | ``` 48 | 49 | For more detailed usage instructions, see the [full documentation](https://github.com/yourusername/py-error-handling-toolkit). 50 | 51 | ## Contributing 52 | 53 | Contributions are welcome! Please check out the [contribution guidelines](CONTRIBUTING.md) for details on how to contribute to this project. 54 | 55 | ## License 56 | 57 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. -------------------------------------------------------------------------------- /py-error/examples/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PB2204/Py-Error-Handling-Toolkit/7ee9e968d169d441a0809ab8d49bd9e7b31ca577/py-error/examples/__init__.py -------------------------------------------------------------------------------- /py-error/examples/example_usage.py: -------------------------------------------------------------------------------- 1 | # examples/example_usage.py 2 | 3 | from toolkit.error_handler import CustomError, handle_error 4 | from toolkit.logger import setup_logger 5 | 6 | def main(): 7 | # Initialize the logger 8 | logger = setup_logger('example.log') 9 | 10 | try: 11 | # Simulate an error 12 | simulate_error() 13 | except Exception as e: 14 | handle_error(e, log_function=logger.error) 15 | 16 | def simulate_error(): 17 | # Simulate a custom error 18 | try: 19 | # Code that may raise exceptions 20 | raise CustomError("This is a custom error.") 21 | except CustomError as ce: 22 | handle_error(ce) 23 | except Exception as e: 24 | handle_error(e) 25 | 26 | if __name__ == "__main__": 27 | main() -------------------------------------------------------------------------------- /py-error/requirements.txt: -------------------------------------------------------------------------------- 1 | # No external dependencies required at the moment -------------------------------------------------------------------------------- /py-error/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | import codecs 3 | import os 4 | 5 | here = os.path.abspath(os.path.dirname(__file__)) 6 | 7 | with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh: 8 | long_description = "\n" + fh.read() 9 | 10 | VERSION = '1.0.7' 11 | DESCRIPTION = 'Error Handling and Logging Toolkit for Python' 12 | LONG_DESCRIPTION = 'A toolkit for enhanced error handling and logging in Python applications.' 13 | 14 | # Setting up 15 | setup( 16 | name="python-error", 17 | version=VERSION, 18 | author="PB2204 (Pabitra Banerjee)", 19 | author_email="", 20 | description=DESCRIPTION, 21 | long_description_content_type="text/markdown", 22 | long_description=long_description, 23 | packages=find_packages(), 24 | install_requires=['requests'], 25 | url='https://github.com/pb2204/py-error-handling-toolkit', 26 | keywords=['python', 'error', 'log', 'error handling', 'debugging'], 27 | classifiers=[ 28 | "Development Status :: 4 - Beta", 29 | 'Intended Audience :: Developers', 30 | 'License :: OSI Approved :: MIT License', 31 | 'Programming Language :: Python :: 3', 32 | 'Programming Language :: Python :: 3.6', 33 | 'Programming Language :: Python :: 3.7', 34 | 'Programming Language :: Python :: 3.8', 35 | 'Programming Language :: Python :: 3.9', 36 | ], 37 | ) -------------------------------------------------------------------------------- /py-error/tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PB2204/Py-Error-Handling-Toolkit/7ee9e968d169d441a0809ab8d49bd9e7b31ca577/py-error/tests/__init__.py -------------------------------------------------------------------------------- /py-error/tests/test_error_handler.py: -------------------------------------------------------------------------------- 1 | # tests/test_error_handler.py 2 | 3 | from toolkit.error_handler import CustomError, handle_error 4 | import unittest 5 | from unittest.mock import patch 6 | from io import StringIO 7 | 8 | class TestErrorHandler(unittest.TestCase): 9 | 10 | def setUp(self): 11 | self.error_stream = StringIO() 12 | self.error_logger = CustomError("Test error message") 13 | self.generic_exception = Exception("Test generic exception") 14 | 15 | def test_custom_error_message(self): 16 | with patch('sys.stderr', self.error_stream): 17 | handle_error(self.error_logger) 18 | self.assertEqual( 19 | self.error_stream.getvalue().strip(), 20 | "Custom error occurred: Test error message" 21 | ) 22 | 23 | def test_generic_error_message(self): 24 | with patch('sys.stderr', self.error_stream): 25 | handle_error(self.generic_exception) 26 | self.assertEqual( 27 | self.error_stream.getvalue().strip(), 28 | "An unexpected error occurred: Test generic exception" 29 | ) 30 | 31 | if __name__ == '__main__': 32 | unittest.main() -------------------------------------------------------------------------------- /py-error/tests/test_logger.py: -------------------------------------------------------------------------------- 1 | # tests/test_logger.py 2 | 3 | import unittest 4 | from toolkit.logger import setup_logger 5 | import logging 6 | import os 7 | 8 | class TestLogger(unittest.TestCase): 9 | 10 | def test_setup_logger(self): 11 | log_file = 'test.log' 12 | 13 | # Ensure the log file does not exist initially 14 | if os.path.exists(log_file): 15 | os.remove(log_file) 16 | 17 | logger = setup_logger(log_file) 18 | 19 | # Check if the logger is an instance of logging.Logger 20 | self.assertIsInstance(logger, logging.Logger) 21 | 22 | # Log some messages 23 | logger.info("This is an info message.") 24 | logger.warning("This is a warning message.") 25 | logger.error("This is an error message.") 26 | 27 | # Ensure the log file exists after logging 28 | self.assertTrue(os.path.exists(log_file)) 29 | 30 | # Clean up: remove the log file 31 | os.remove(log_file) 32 | 33 | if __name__ == '__main__': 34 | unittest.main() -------------------------------------------------------------------------------- /py-error/toolkit/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PB2204/Py-Error-Handling-Toolkit/7ee9e968d169d441a0809ab8d49bd9e7b31ca577/py-error/toolkit/__init__.py -------------------------------------------------------------------------------- /py-error/toolkit/error_handler.py: -------------------------------------------------------------------------------- 1 | # toolkit/error_handler.py 2 | 3 | class CustomError(Exception): 4 | """ 5 | A custom exception class for handling specific types of errors. 6 | You can create instances of this class with a custom error message. 7 | """ 8 | def __init__(self, message): 9 | super().__init__(message) 10 | 11 | def handle_error(error, log_function=None): 12 | """ 13 | Handle errors based on their type. 14 | 15 | :param error: The error to handle. 16 | :param log_function: An optional logging function (e.g., logger.error) to log the error. 17 | """ 18 | if isinstance(error, CustomError): 19 | error_message = f"Custom error occurred: {error}" 20 | # Add custom error handling logic here 21 | if log_function: 22 | log_function(error_message) 23 | else: 24 | print(error_message) 25 | else: 26 | error_message = f"An unexpected error occurred: {error}" 27 | # Add generic error handling logic here 28 | if log_function: 29 | log_function(error_message) 30 | else: 31 | print(error_message) 32 | 33 | if __name__ == "__main__": 34 | # Example usage 35 | try: 36 | # Code that may raise exceptions 37 | raise CustomError("This is a custom error.") 38 | except CustomError as ce: 39 | handle_error(ce) 40 | except Exception as e: 41 | handle_error(e) 42 | else: 43 | print("No errors occurred.") -------------------------------------------------------------------------------- /py-error/toolkit/logger.py: -------------------------------------------------------------------------------- 1 | # toolkit/logger.py 2 | 3 | import logging 4 | 5 | def setup_logger(log_file): 6 | """ 7 | Set up a basic logger and return it. 8 | 9 | :param log_file: The path to the log file. 10 | :return: The configured logger. 11 | """ 12 | logger = logging.getLogger(__name__) 13 | logger.setLevel(logging.DEBUG) 14 | 15 | # Define a log format 16 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 17 | 18 | # Create a file handler and set the formatter 19 | file_handler = logging.FileHandler(log_file) 20 | file_handler.setFormatter(formatter) 21 | 22 | # Add the file handler to the logger 23 | logger.addHandler(file_handler) 24 | 25 | return logger 26 | 27 | if __name__ == "__main__": 28 | # Example usage 29 | logger = setup_logger('error.log') 30 | logger.info('This is an info message.') 31 | logger.warning('This is a warning message.') 32 | logger.error('This is an error message.') -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | setup( 4 | name='python-error', 5 | version='1.0.5', 6 | packages=find_packages(), 7 | install_requires=[ 8 | # List any external dependencies here 9 | ], 10 | author='Pabitra Banerjee', 11 | author_email='rockstarpabitra2204@gmail.com', 12 | description='Error Handling and Logging Toolkit for Python', 13 | long_description='A toolkit for enhanced error handling and logging in Python applications.', 14 | long_description_content_type='text/markdown', 15 | url='https://github.com/pb2204/py-error-handling-toolkit', 16 | classifiers=[ 17 | "Development Status :: 4 - Beta", 18 | 'Intended Audience :: Developers', 19 | 'License :: OSI Approved :: MIT License', 20 | 'Programming Language :: Python :: 3', 21 | 'Programming Language :: Python :: 3.6', 22 | 'Programming Language :: Python :: 3.7', 23 | 'Programming Language :: Python :: 3.8', 24 | 'Programming Language :: Python :: 3.9', 25 | ], 26 | ) -------------------------------------------------------------------------------- /structure.md: -------------------------------------------------------------------------------- 1 | py-error/ 2 | ├── toolkit/ 3 | │ ├── __init__.py 4 | │ ├── error_handler.py 5 | │ └── logger.py 6 | ├── examples/ 7 | │ ├── __init__.py 8 | │ └── example_usage.py 9 | ├── tests/ 10 | │ ├── __init__.py 11 | │ ├── test_error_handler.py 12 | │ └── test_logger.py 13 | ├── setup.py 14 | ├── README.md 15 | └── requirements.txt --------------------------------------------------------------------------------