-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomException.py
More file actions
29 lines (23 loc) · 1 KB
/
CustomException.py
File metadata and controls
29 lines (23 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from termcolor import colored
import logging
logging.basicConfig(filename='exceptions.log', level=logging.ERROR,
format='%(asctime)s:%(levelname)s:%(message)s')
class AdvancedCustomException(Exception):
def __init__(self, original_exception, color="red"):
self.original_exception = original_exception
self.color = color
self.message = str(original_exception)
self.code = self.extract_code(original_exception)
def extract_code(self, exception):
if hasattr(exception, 'response') and hasattr(exception.response, 'status_code'):
return exception.response.status_code
return 500
def __str__(self):
color1 = "red"
color2 = "green"
color3 = "blue"
part0 = colored("[Error ", color1)
part1 = colored(f"{self.code}", color2)
part2 = colored("]:", color1)
part3 = colored(f" {self.message}", color3)
return part0 + part1 + part2 + part3