Skip to content

Commit 39c4536

Browse files
committed
Handle errors in the API
1 parent 4dfb580 commit 39c4536

File tree

4 files changed

+722
-629
lines changed

4 files changed

+722
-629
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies = [
2424
"fastapi",
2525
"click",
2626
"unstructured-ingest",
27+
"unstructured-client",
2728
"opentelemetry-instrumentation-fastapi",
2829
"opentelemetry-exporter-otlp-proto-grpc",
2930
"dataclasses-json"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.30" # pragma: no cover
1+
__version__ = "0.0.31" # pragma: no cover

unstructured_platform_plugins/etl_uvicorn/api_generator.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import json
55
import logging
66
from functools import partial
7+
from shutil import ReadError
78
from typing import Any, Callable, Optional, Union
89

910
from fastapi import FastAPI, status
10-
from fastapi.responses import StreamingResponse
11+
from fastapi.responses import JSONResponse, StreamingResponse
1112
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
1213
from pydantic import BaseModel, Field, create_model
14+
from requests import ReadTimeout
1315
from starlette.responses import RedirectResponse
16+
from unstructured_client.models.errors import HTTPValidationError
1417
from unstructured_ingest.data_types.file_data import BatchFileData, FileData, file_data_from_dict
1518
from uvicorn.config import LOG_LEVELS
1619
from uvicorn.importer import import_from_string
@@ -208,6 +211,22 @@ async def _stream_response():
208211
output=output,
209212
file_data=request_dict.get("file_data", None),
210213
)
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+
)
211230
except UnrecoverableException as ex:
212231
logger.info("Unrecoverable error occurred during plugin invocation")
213232
return InvokeResponse(

0 commit comments

Comments
 (0)