Skip to content

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

5.1 KB
Raw Blame
import json
import logging
import traceback
from typing import Any

from django.conf import settings
from django.http import HttpRequest, HttpResponse
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__)


# Set via settings.REST_FRAMEWORK.EXCEPTION_HANDLER.
def drf_logging_exc_handler(exc: Exception, context: Any) -> Response | None:
    """Custom exception handler for DRF.

    DRF's exception handler takes care of Http404, PermissionDenied and
    APIExceptions by default. This function helps log the traceback for
    all types of exception and then handles it with DRF or Django

    Args:
        exc (Exception): Implementation of Exception raised
        context (dict[str, Any]): Dictionary containing additional
            metadata on the request itself

    Returns:
        Optional[Response]: Returns either a Response if handled, \
            if None it will be
            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)
    _enrich_not_found_detail(response=response, context=context)
    ExceptionLoggingMiddleware.format_exc_and_log(
        request=request, response=response, exception=exc
    )
    return response


def _enrich_not_found_detail(response: Response | None, context: Any) -> None:
    """Replace DRF's generic "Not found." with the resource's name.

    Derives a human label from the view's `queryset` model so every 404
    reads "<Resource> not found." app-wide, no per-view work. Uses the
    static `queryset` attr (not `get_queryset()`) to avoid running view
    logic during error handling; views without it keep the generic message.
    Override `Meta.verbose_name` to tune a model's label.
    """
    if response is None or getattr(response, "status_code", None) != 404:
        return
    data = getattr(response, "data", None)
    if not isinstance(data, dict):
        return
    model = getattr(getattr(context.get("view"), "queryset", None), "model", None)
    if model is None:
        return
    label = str(model._meta.verbose_name).capitalize()
    for err in data.get("errors", []):
        # Guard: a raise here would mask the original error with a 500.
        if isinstance(err, dict) and err.get("code") == "not_found":
            err["detail"] = f"{label} not found."


class ExceptionLoggingMiddleware:
    """Custom middleware to log unhandled errors.

    DRF middleware handles most exception types, Django takes care of
    handling the rest.
    """

    def __init__(self, get_response: Any) -> None:
        self.get_response = get_response

    def __call__(self, request: HttpRequest) -> HttpResponse:
        response = self.get_response(request)
        return response

    def process_exception(
        self, request: HttpRequest, exception: Exception
    ) -> HttpResponse | None:
        """Django hook to handle exceptions by a middleware.

        Args:
            request (HttpRequest): Request that was made
            exception (Exception): Exception that was raised

        Returns:
            Optional[HttpResponse]: Returns either a HttpResponse if handled
                If None it will be handled by another method in the middleware
        """
        # Handle only when running in production, for debug mode
        # Django takes care of displaying a detailed HTML page.
        if not settings.DEBUG and exception:
            logger.error(f"Unhandled exception by DRF for {request}, logging error...")
            ExceptionLoggingMiddleware.format_exc_and_log(
                request=request, exception=exception
            )
            detail = {"detail": "Error processing the request."}
            return HttpResponse(json.dumps(detail), status=500)
        return None

    @staticmethod
    def format_exc_and_log(
        request: Request, exception: Exception, response: Response | None = None
    ) -> None:
        """Format the exception to be logged and logs it.

        Args:
            request (HttpRequest): Request to get API endpoint hit
            exception (Exception): Exception that was raised to be logged
        """
        status_code = 500
        if response:
            status_code = response.status_code
        if status_code >= 500:
            message = f"{request.method} {request.build_absolute_uri()} {status_code}\n\n{repr(exception)}\n\n````{traceback.format_exc()}````"
        else:
            message = f"{request.method} {request.build_absolute_uri()} {status_code} {repr(exception)}"
        logger.error(message)