Skip to content

Commit 96bf60d

Browse files
authored
feat/add error codes mapped to ingest library (#38)
1 parent bf36143 commit 96bf60d

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.19
2+
3+
* **Add more granular error response texts and codes**
4+
15
## 0.0.18
26

37
* **Receive ingest 0.3.12**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.18" # pragma: no cover
1+
__version__ = "0.0.19" # pragma: no cover

unstructured_platform_plugins/etl_uvicorn/api_generator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from uvicorn.config import LOG_LEVELS
1616
from uvicorn.importer import import_from_string
1717

18+
from unstructured_platform_plugins.etl_uvicorn.errors import wrap_error
1819
from unstructured_platform_plugins.etl_uvicorn.otel import get_metric_provider, get_trace_provider
1920
from unstructured_platform_plugins.etl_uvicorn.utils import (
2021
get_func,
@@ -186,12 +187,12 @@ async def _stream_response():
186187
)
187188
except Exception as invoke_error:
188189
logger.error(f"failed to invoke plugin: {invoke_error}", exc_info=True)
190+
http_error = wrap_error(invoke_error)
189191
return InvokeResponse(
190192
usage=usage,
191193
filedata_meta=filedata_meta_model.model_validate(filedata_meta.model_dump()),
192-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
193-
status_code_text=f"failed to invoke plugin: "
194-
f"[{invoke_error.__class__.__name__}] {invoke_error}",
194+
status_code=http_error.status_code,
195+
status_code_text=f"[{invoke_error.__class__.__name__}] {invoke_error}",
195196
)
196197

197198
if input_schema_model.model_fields:
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from abc import ABC
2+
from typing import Any, Optional
3+
4+
import unstructured_ingest.v2.errors as ingest_errors
5+
from fastapi import HTTPException
6+
7+
8+
class BaseError(HTTPException, ABC):
9+
status_code: int
10+
11+
def __init__(self, detail: Any, headers: Optional[dict[str, str]] = None):
12+
super().__init__(status_code=self.status_code, detail=detail, headers=headers)
13+
14+
15+
class UserError(BaseError):
16+
status_code: int = 400
17+
18+
19+
class UserAuthError(UserError):
20+
status_code: int = 403
21+
22+
23+
class RateLimitError(UserError):
24+
status_code: int = 429
25+
26+
27+
class QuotaError(UserError):
28+
status_code: int = 402
29+
30+
31+
class ProviderError(BaseError):
32+
status_code: int = 500
33+
34+
35+
class CatchAllError(BaseError):
36+
status_code: int = 512
37+
38+
39+
def wrap_error(e: Exception) -> HTTPException:
40+
if isinstance(e, ingest_errors.UserAuthError):
41+
return UserAuthError(e)
42+
elif isinstance(e, ingest_errors.RateLimitError):
43+
return RateLimitError(e)
44+
elif isinstance(e, ingest_errors.QuotaError):
45+
return QuotaError(e)
46+
elif isinstance(e, ingest_errors.UserError):
47+
return UserError(e)
48+
elif isinstance(e, ingest_errors.ProviderError):
49+
return ProviderError(e)
50+
else:
51+
return CatchAllError(e)

0 commit comments

Comments
 (0)