Skip to content

Commit a8472df

Browse files
committed
pretty plaintext errors
1 parent 22d88cd commit a8472df

3 files changed

Lines changed: 15 additions & 21 deletions

File tree

vetiver/server.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from typing import Callable, List, Union
22
from urllib.parse import urljoin
33

4+
import re
45
import httpx
56
import pandas as pd
67
import requests
78
import uvicorn
8-
from fastapi import FastAPI, Request, testclient, status
9-
from fastapi.encoders import jsonable_encoder
9+
from fastapi import FastAPI, Request, testclient
1010
from fastapi.exceptions import RequestValidationError
1111
from fastapi.openapi.utils import get_openapi
12-
from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
12+
from fastapi.responses import HTMLResponse, RedirectResponse
1313
from fastapi.responses import PlainTextResponse
14-
from starlette.exceptions import HTTPException as StarletteHTTPException
1514
from warnings import warn
1615

1716
from .utils import _jupyter_nb
@@ -143,17 +142,8 @@ async def rapidoc():
143142
"""
144143

145144
@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)
157147

158148
return app
159149

@@ -282,9 +272,7 @@ def predict(endpoint, data: Union[dict, pd.DataFrame, pd.Series], **kw) -> pd.Da
282272
response.raise_for_status()
283273
except (requests.exceptions.HTTPError, httpx.HTTPStatusError) as e:
284274
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))
288276
raise requests.exceptions.HTTPError(
289277
f"Could not obtain data from endpoint with error: {e}"
290278
)

vetiver/tests/test_pytorch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_torch_predict_ptype_error(vetiver_client_prototype):
106106

107107
data = {"0": "bad"}
108108

109-
with pytest.raises(requests.exceptions.HTTPError):
109+
with pytest.raises(TypeError):
110110
predict(endpoint=vetiver_client_prototype, data=data)
111111

112112

vetiver/tests/test_sklearn.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ def test_predict_sklearn_series_check_ptype(vetiver_client):
8080
assert len(response) == 1
8181

8282

83-
@pytest.mark.parametrize("data", [(0, 0), 0, 0.0, "0"])
83+
@pytest.mark.parametrize("data", [(0), 0, 0.0, "0"])
8484
def test_predict_sklearn_type_error(data, vetiver_client):
85-
msg = f"Predict expects DataFrame, Series, or dict. Given type is {type(data)}"
85+
import re
86+
87+
msg = re.sub(
88+
r"\n",
89+
": ",
90+
"1 validation error for Request\nbody\n value is not a valid list \(type=type_error.list\)", # noqa
91+
)
8692

8793
with pytest.raises(TypeError, match=msg):
8894
predict(endpoint=vetiver_client, data=data)

0 commit comments

Comments
 (0)