Skip to content

Commit

Permalink
[DH-5517] Create a CustomError to send a description (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjc712 authored Mar 1, 2024
1 parent 71e51d0 commit 5e7a5de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
13 changes: 7 additions & 6 deletions dataherald/sql_database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@

from dataherald.sql_database.models.types import DatabaseConnection
from dataherald.utils.encrypt import FernetEncrypt
from dataherald.utils.error_codes import CustomError
from dataherald.utils.s3 import S3

logger = logging.getLogger(__name__)


# Define a custom exception class
class SQLInjectionError(Exception):
class SQLInjectionError(CustomError):
pass


class InvalidDBConnectionError(Exception):
class InvalidDBConnectionError(CustomError):
pass


class EmptyDBError(Exception):
class EmptyDBError(CustomError):
pass


class SSHInvalidDatabaseConnectionError(Exception):
class SSHInvalidDatabaseConnectionError(CustomError):
pass


Expand Down Expand Up @@ -89,7 +90,7 @@ def get_sql_engine(
return engine
except Exception as e:
raise SSHInvalidDatabaseConnectionError(
f"Invalid SSH connection, {e}"
"Invalid SSH connection", description=str(e)
) from e
try:
db_uri = unquote(fernet_encrypt.decrypt(database_info.connection_uri))
Expand All @@ -107,7 +108,7 @@ def get_sql_engine(
DBConnections.add(database_info.id, engine)
except Exception as e:
raise InvalidDBConnectionError( # noqa: B904
f"Unable to connect to db: {database_info.alias}, {e}"
f"Unable to connect to db: {database_info.alias}", description=str(e)
)
return engine

Expand Down
9 changes: 9 additions & 0 deletions dataherald/utils/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
}


class CustomError(Exception):
def __init__(self, message, description=None):
super().__init__(message)
self.description = description


def error_response(error, detail: dict, default_error_code=""):
return JSONResponse(
status_code=400,
Expand All @@ -26,6 +32,9 @@ def error_response(error, detail: dict, default_error_code=""):
error.__class__.__name__, default_error_code
),
"message": str(error),
"description": error.description
if isinstance(error, CustomError)
else None,
"detail": detail,
},
)

0 comments on commit 5e7a5de

Please sign in to comment.