Skip to content

Commit 5e7a5de

Browse files
authored
[DH-5517] Create a CustomError to send a description (#416)
1 parent 71e51d0 commit 5e7a5de

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

dataherald/sql_database/base.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,26 @@
1212

1313
from dataherald.sql_database.models.types import DatabaseConnection
1414
from dataherald.utils.encrypt import FernetEncrypt
15+
from dataherald.utils.error_codes import CustomError
1516
from dataherald.utils.s3 import S3
1617

1718
logger = logging.getLogger(__name__)
1819

1920

2021
# Define a custom exception class
21-
class SQLInjectionError(Exception):
22+
class SQLInjectionError(CustomError):
2223
pass
2324

2425

25-
class InvalidDBConnectionError(Exception):
26+
class InvalidDBConnectionError(CustomError):
2627
pass
2728

2829

29-
class EmptyDBError(Exception):
30+
class EmptyDBError(CustomError):
3031
pass
3132

3233

33-
class SSHInvalidDatabaseConnectionError(Exception):
34+
class SSHInvalidDatabaseConnectionError(CustomError):
3435
pass
3536

3637

@@ -89,7 +90,7 @@ def get_sql_engine(
8990
return engine
9091
except Exception as e:
9192
raise SSHInvalidDatabaseConnectionError(
92-
f"Invalid SSH connection, {e}"
93+
"Invalid SSH connection", description=str(e)
9394
) from e
9495
try:
9596
db_uri = unquote(fernet_encrypt.decrypt(database_info.connection_uri))
@@ -107,7 +108,7 @@ def get_sql_engine(
107108
DBConnections.add(database_info.id, engine)
108109
except Exception as e:
109110
raise InvalidDBConnectionError( # noqa: B904
110-
f"Unable to connect to db: {database_info.alias}, {e}"
111+
f"Unable to connect to db: {database_info.alias}", description=str(e)
111112
)
112113
return engine
113114

dataherald/utils/error_codes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
}
1919

2020

21+
class CustomError(Exception):
22+
def __init__(self, message, description=None):
23+
super().__init__(message)
24+
self.description = description
25+
26+
2127
def error_response(error, detail: dict, default_error_code=""):
2228
return JSONResponse(
2329
status_code=400,
@@ -26,6 +32,9 @@ def error_response(error, detail: dict, default_error_code=""):
2632
error.__class__.__name__, default_error_code
2733
),
2834
"message": str(error),
35+
"description": error.description
36+
if isinstance(error, CustomError)
37+
else None,
2938
"detail": detail,
3039
},
3140
)

0 commit comments

Comments
 (0)