-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.py
80 lines (62 loc) · 2.7 KB
/
error.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sys
import traceback
from functools import wraps
from flask import make_response
from log import logger
def error_handler(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
return result
except NotFoundError as e:
error_message = f"An error occurred: {str(e)}"
logger.exception(error_message)
return make_response({"error": e.message}, e.status_code)
except AuthError as e:
error_message = f"An error occurred: {str(e)}"
logger.exception(error_message)
return make_response({"error": e.message}, e.status_code)
except InvalidInputError as e:
error_message = f"An error occurred: {str(e)}"
logger.exception(error_message)
return make_response({"error": e.message}, e.status_code)
except CustomError as e:
error_message = f"An error occurred: {str(e)}"
logger.exception(error_message)
return make_response({"error": e.message}, e.status_code)
except Exception as e:
error_message = f"An error occurred: {str(e)}"
logger.exception(error_message)
return make_response({'error': error_message}, 500)
return wrapper
class CustomError(Exception):
"""Base class for custom exceptions."""
def __init__(self, status_code, message):
super().__init__(message)
self.status_code = status_code
self.message = message
class DatabaseConnectionError(CustomError):
"""Raised when there is an issue connecting to the database."""
def __init__(self, message="Database connection error"):
super().__init__(status_code=503, message=message)
class InvalidInputError(CustomError):
"""Raised when the input provided is invalid."""
def __init__(self, message="Invalid input"):
super().__init__(status_code=400, message=message)
class NotFoundError(CustomError):
"""Raised when the input provided is invalid."""
def __init__(self, message="Invalid input"):
super().__init__(status_code=404, message=message)
class ConflictError(CustomError):
"""Raised when the input provided is invalid."""
def __init__(self, message="Invalid input"):
super().__init__(status_code=409, message=message)
class AuthError(CustomError):
"""Raised when the input provided is invalid."""
def __init__(self, message="Invalid input"):
super().__init__(status_code=401, message=message)
class ForbiddenError(CustomError):
"""Raised when the input provided is invalid."""
def __init__(self, message="Invalid input"):
super().__init__(status_code=403, message=message)