Skip to content

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

[FIX] - Preserve and surface 4xx error status codes in Single Pass Extraction (#1701)

Praveen Kumara1344ed00010
diff --git a/backend/middleware/exception.py b/backend/middleware/exception.py
index e31559ab7..f5ea1a6f7 100644
--- a/backend/middleware/exception.py
+++ b/backend/middleware/exception.py
@@ -9,6 +9,8 @@ from drf_standardized_errors.handler import exception_handler
from rest_framework.request import Request
from rest_framework.response import Response
+from unstract.sdk1.exceptions import SdkError
+
logger = logging.getLogger(__name__)
@@ -31,6 +33,19 @@ def drf_logging_exc_handler(exc: Exception, context: Any) -> Response | None:
handled by another method in the middleware
"""
request = context.get("request")
+
+ # Handle SdkError exceptions and preserve their status codes
+ if isinstance(exc, SdkError):
+ status_code = exc.status_code or 500
+ response = Response(
+ {"error": str(exc), "type": "sdk_error"},
+ status=status_code,
+ )
+ ExceptionLoggingMiddleware.format_exc_and_log(
+ request=request, response=response, exception=exc
+ )
+ return response
+
response: Response | None = exception_handler(exc=exc, context=context)
ExceptionLoggingMiddleware.format_exc_and_log(
request=request, response=response, exception=exc
diff --git a/unstract/sdk1/src/unstract/sdk1/exceptions.py b/unstract/sdk1/src/unstract/sdk1/exceptions.py
index c4082f38e..40889af87 100644
--- a/unstract/sdk1/src/unstract/sdk1/exceptions.py
+++ b/unstract/sdk1/src/unstract/sdk1/exceptions.py
@@ -38,6 +38,10 @@ class SdkError(Exception):
self.status_code = actual_err.status_code
elif hasattr(actual_err, "http_status"): # Few providers like Mistral
self.status_code = actual_err.http_status
+ elif hasattr(actual_err, "response") and hasattr(
+ actual_err.response, "status_code"
+ ): # requests.HTTPError
+ self.status_code = actual_err.response.status_code
def __str__(self) -> str:
"""Return string representation of the SdkError."""