|
1 | 1 | from typing import Callable, List, Union |
2 | 2 | from urllib.parse import urljoin |
3 | 3 |
|
| 4 | +import re |
4 | 5 | import httpx |
5 | 6 | import pandas as pd |
6 | 7 | import requests |
7 | 8 | import uvicorn |
8 | | -from fastapi import FastAPI, Request, testclient, status |
9 | | -from fastapi.encoders import jsonable_encoder |
| 9 | +from fastapi import FastAPI, Request, testclient |
10 | 10 | from fastapi.exceptions import RequestValidationError |
11 | 11 | from fastapi.openapi.utils import get_openapi |
12 | | -from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse |
| 12 | +from fastapi.responses import HTMLResponse, RedirectResponse |
13 | 13 | from fastapi.responses import PlainTextResponse |
14 | | -from starlette.exceptions import HTTPException as StarletteHTTPException |
15 | 14 | from warnings import warn |
16 | 15 |
|
17 | 16 | from .utils import _jupyter_nb |
@@ -143,17 +142,8 @@ async def rapidoc(): |
143 | 142 | """ |
144 | 143 |
|
145 | 144 | @app.exception_handler(RequestValidationError) |
146 | | - async def validation_exception_handler( |
147 | | - request: Request, exc: RequestValidationError |
148 | | - ): |
149 | | - return JSONResponse( |
150 | | - status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, |
151 | | - content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}), |
152 | | - ) |
153 | | - |
154 | | - @app.exception_handler(StarletteHTTPException) |
155 | | - async def http_exception_handler(request, exc): |
156 | | - return PlainTextResponse(str(exc.detail), status_code=exc.status_code) |
| 145 | + async def validation_exception_handler(request, exc): |
| 146 | + return PlainTextResponse(str(exc), status_code=422) |
157 | 147 |
|
158 | 148 | return app |
159 | 149 |
|
@@ -282,9 +272,7 @@ def predict(endpoint, data: Union[dict, pd.DataFrame, pd.Series], **kw) -> pd.Da |
282 | 272 | response.raise_for_status() |
283 | 273 | except (requests.exceptions.HTTPError, httpx.HTTPStatusError) as e: |
284 | 274 | if response.status_code == 422: |
285 | | - raise TypeError( |
286 | | - PlainTextResponse(str(response), status_code=response.status_code) |
287 | | - ) |
| 275 | + raise TypeError(re.sub(r"\n", ": ", response.text)) |
288 | 276 | raise requests.exceptions.HTTPError( |
289 | 277 | f"Could not obtain data from endpoint with error: {e}" |
290 | 278 | ) |
|
0 commit comments