|
4 | 4 | import json |
5 | 5 | import logging |
6 | 6 | from functools import partial |
| 7 | +from shutil import ReadError |
7 | 8 | from typing import Any, Callable, Optional, Union |
8 | 9 |
|
9 | 10 | from fastapi import FastAPI, status |
10 | | -from fastapi.responses import StreamingResponse |
| 11 | +from fastapi.responses import JSONResponse, StreamingResponse |
11 | 12 | from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor |
12 | 13 | from pydantic import BaseModel, Field, create_model |
| 14 | +from requests import ReadTimeout |
13 | 15 | from starlette.responses import RedirectResponse |
| 16 | +from unstructured_client.models.errors import HTTPValidationError |
14 | 17 | from unstructured_ingest.data_types.file_data import BatchFileData, FileData, file_data_from_dict |
15 | 18 | from uvicorn.config import LOG_LEVELS |
16 | 19 | from uvicorn.importer import import_from_string |
@@ -208,6 +211,22 @@ async def _stream_response(): |
208 | 211 | output=output, |
209 | 212 | file_data=request_dict.get("file_data", None), |
210 | 213 | ) |
| 214 | + except ReadError as exc: |
| 215 | + return JSONResponse( |
| 216 | + status_code=400, |
| 217 | + content={"detail": f"File read error: {str(exc)}"}, |
| 218 | + ) |
| 219 | + except ReadTimeout as exc: |
| 220 | + return JSONResponse( |
| 221 | + status_code=504, |
| 222 | + content={"detail": f"Partition service timeout: {str(exc)}"}, |
| 223 | + ) |
| 224 | + except HTTPValidationError as exc: |
| 225 | + logger.error(f"HTTP validation error: {exc}", exc_info=True) |
| 226 | + return JSONResponse( |
| 227 | + status_code=422, |
| 228 | + content={"detail": f"HTTP validation error: {str(exc)}"}, |
| 229 | + ) |
211 | 230 | except UnrecoverableException as ex: |
212 | 231 | logger.info("Unrecoverable error occurred during plugin invocation") |
213 | 232 | return InvokeResponse( |
|
0 commit comments