Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

UN-1819 [FIX] - Surface status codes from third-party clients in services to UI (#1702)

Praveen Kumar3adef8fe7d22
diff --git a/backend/backend/exceptions.py b/backend/backend/exceptions.py
index 8159771c1..caf4df575 100644
--- a/backend/backend/exceptions.py
+++ b/backend/backend/exceptions.py
@@ -4,11 +4,12 @@ from rest_framework.exceptions import APIException
from rest_framework.response import Response
from rest_framework.views import exception_handler
-from unstract.connectors.exceptions import ConnectorBaseException
+from unstract.connectors.exceptions import ConnectorBaseException, ConnectorError
class UnstractBaseException(APIException):
default_detail = "Error occurred"
+ default_status_code = 500
def __init__(
self,
@@ -28,6 +29,12 @@ class UnstractBaseException(APIException):
super().__init__(detail=detail, **kwargs)
self._core_err = core_err
+ # Preserve status code from ConnectorError if available
+ if isinstance(core_err, ConnectorError) and core_err.status_code is not None:
+ self.status_code = core_err.status_code
+ else:
+ self.status_code = self.default_status_code
+
class LLMHelperError(Exception):
pass
@@ -51,4 +58,4 @@ class UnstractFSException(UnstractBaseException):
"""
default_detail = "Error testing connection. "
- status_code = 500
+ default_status_code = 500
diff --git a/backend/connector_processor/exceptions.py b/backend/connector_processor/exceptions.py
index 36508038c..2deb36469 100644
--- a/backend/connector_processor/exceptions.py
+++ b/backend/connector_processor/exceptions.py
@@ -28,4 +28,6 @@ class TestConnectorInputError(UnstractBaseException):
def __init__(self, core_err: ConnectorError) -> None:
super().__init__(detail=core_err.message, core_err=core_err)
self.default_detail = core_err.message
- self.status_code = 400
+ # Preserve status code from connector error if available, otherwise default to 400
+ if not core_err.status_code:
+ self.status_code = 400
diff --git a/backend/workflow_manager/endpoint_v2/database_utils.py b/backend/workflow_manager/endpoint_v2/database_utils.py
index 7f0aeb7ab..a89594e18 100644
--- a/backend/workflow_manager/endpoint_v2/database_utils.py
+++ b/backend/workflow_manager/endpoint_v2/database_utils.py
@@ -59,7 +59,7 @@ class DatabaseUtils:
try:
return conn_cls.get_information_schema(table_name=table_name)
except ConnectorError as e:
- raise UnstractDBException(detail=e.message) from e
+ raise UnstractDBException(detail=e.message, status_code=e.status_code) from e
@staticmethod
def get_sql_values_for_query(
diff --git a/backend/workflow_manager/endpoint_v2/exceptions.py b/backend/workflow_manager/endpoint_v2/exceptions.py
index dc2ac95c3..b8664e458 100644
--- a/backend/workflow_manager/endpoint_v2/exceptions.py
+++ b/backend/workflow_manager/endpoint_v2/exceptions.py
@@ -96,17 +96,21 @@ class BigQueryTableNotFound(APIException):
class UnstractDBException(APIException):
default_detail = "Error creating/inserting to database. "
- def __init__(self, detail: str = default_detail) -> None:
- status_code = 500
- super().__init__(detail=detail, code=status_code)
+ def __init__(
+ self, detail: str = default_detail, status_code: int | None = None
+ ) -> None:
+ self.status_code = status_code or 500
+ super().__init__(detail=detail, code=self.status_code)
class UnstractQueueException(APIException):
default_detail = "Error creating/inserting to Queue. "
- def __init__(self, detail: str = default_detail) -> None:
- status_code = 500
- super().__init__(detail=detail, code=status_code)
+ def __init__(
+ self, detail: str = default_detail, status_code: int | None = None
+ ) -> None:
+ self.status_code = status_code or 500
+ super().__init__(detail=detail, code=self.status_code)
class SourceFileOrInfilePathNotFound(APIException):
diff --git a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py
index 1ff223eeb..da7753080 100644
--- a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py
+++ b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py
@@ -181,7 +181,9 @@ def prompt_processor() -> Any:
RunLevel.RUN,
"Unable to obtain LLM / embedding / vectorDB",
)
- raise APIError(message=msg)
+ # Preserve the status code from the SDK error
+ status_code = getattr(e, "status_code", None) or 500
+ raise APIError(message=msg, code=status_code) from e
if output[PSKeys.TYPE] == PSKeys.TABLE:
adapter_parent_data = ToolAdapter.get_adapter_config(util, output[PSKeys.LLM])
diff --git a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
index df2c0daed..9f8cbf9c2 100644
--- a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
+++ b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
@@ -263,13 +263,13 @@ class AnswerPromptService:
word_confidence_data
)
return answer
- # TODO: Catch and handle specific exception here
except SdkRateLimitError as e:
raise RateLimitError(f"Rate limit error. {str(e)}") from e
except SdkError as e:
logger.error(f"Error fetching response for prompt: {e}.")
- # TODO: Publish this error as a FE update
- raise APIError(str(e)) from e
+ # Preserve the status code from the SDK error for proper HTTP response
+ status_code = getattr(e, "status_code", None) or 500
+ raise APIError(message=str(e), code=status_code) from e
@staticmethod
def extract_table(
diff --git a/prompt-service/src/unstract/prompt_service/utils/request.py b/prompt-service/src/unstract/prompt_service/utils/request.py
index 4c94506e7..8a5a82d46 100644
--- a/prompt-service/src/unstract/prompt_service/utils/request.py
+++ b/prompt-service/src/unstract/prompt_service/utils/request.py
@@ -43,7 +43,14 @@ def make_http_request(
return return_val
except RequestException as e:
app.logger.error(f"HTTP request error: {e}")
- raise APIError(f"Error occured while invoking POST API Variable : {str(e)}")
+ # Extract status code from requests exception if available
+ status_code = None
+ if getattr(e, "response", None) is not None:
+ status_code = getattr(e.response, "status_code", None)
+ raise APIError(
+ f"HTTP {verb.value} request to {url} failed: {e!s}",
+ code=status_code or 500,
+ ) from e
except Exception as e:
app.logger.error(f"An unexpected error occurred: {e}")
raise e
diff --git a/unstract/connectors/src/unstract/connectors/exceptions.py b/unstract/connectors/src/unstract/connectors/exceptions.py
index d3433a0cc..3b9c683e5 100644
--- a/unstract/connectors/src/unstract/connectors/exceptions.py
+++ b/unstract/connectors/src/unstract/connectors/exceptions.py
@@ -23,11 +23,13 @@ class ConnectorError(ConnectorBaseException):
message: str,
*args: Any,
treat_as_user_message: bool = False,
+ status_code: int | None = None,
**kwargs: Any,
) -> None:
user_message = message if treat_as_user_message else None
super().__init__(*args, user_message=user_message, **kwargs)
self.message = message
+ self.status_code = status_code
def __str__(self) -> str:
return f"{self.message}"
diff --git a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py
index ded2f9ce0..519fe1394 100644
--- a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py
+++ b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py
@@ -26,7 +26,9 @@ def parse_azure_error(e: Exception) -> ConnectorError:
f"Authentication failed. Please check your connection credentials. \n"
f"Error: \n```\n{client_error}\n```"
)
- return ConnectorError(error_message)
+ # ClientAuthenticationError typically indicates 401 Unauthorized
+ status_code = getattr(e, "status_code", None) or 401
+ return ConnectorError(error_message, status_code=status_code)
elif isinstance(e, AzureException.ServiceRequestError):
client_error = e.message if hasattr(e, "message") else str(e)
error_message += (
@@ -39,7 +41,9 @@ def parse_azure_error(e: Exception) -> ConnectorError:
f"Azure service returned an error response. \n"
f"Error: \n```\n{client_error}\n```"
)
- return AzureHttpError(error_message)
+ # Preserve the HTTP status code from Azure's response
+ status_code = getattr(e, "status_code", None)
+ return AzureHttpError(error_message, status_code=status_code)
else:
error_message += (
f"Error from Azure Cloud Storage. \n" f"Error: \n```\n{str(e)}\n```"