Skip to content

Commit f306062

Browse files
authored
fix: handle argument of type 'int' is not iterable (#324)
Closes #323 Certain OSErrors come back with an errno in the first arg. We were searching for a substring in here, so we just need to skip the check if e.args[0] is not a string. The new test case verifies this by raising an error with a errno.
1 parent c5f7119 commit f306062

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

prepline_general/api/general.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def pipeline_api(
433433
elements = partition(**partition_kwargs)
434434

435435
except OSError as e:
436-
if (
436+
if isinstance(e.args[0], str) and (
437437
"chipper-fast-fine-tuning is not a local folder" in e.args[0]
438438
or "ved-fine-tuning is not a local folder" in e.args[0]
439439
):
@@ -442,7 +442,11 @@ def pipeline_api(
442442
detail="The Chipper model is not available for download. It can be accessed via the official hosted API.",
443443
)
444444

445-
raise e
445+
# OSError isn't caught by our top level handler, so convert it here
446+
raise HTTPException(
447+
status_code=500,
448+
detail=str(e),
449+
)
446450
except ValueError as e:
447451
if "Invalid file" in e.args[0]:
448452
raise HTTPException(

test_general/api/test_app.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,48 @@ def test_invalid_strategy_for_image_file():
828828
)
829829
assert resp.status_code == 400
830830
assert "fast strategy is not available for image files" in resp.text
831+
832+
833+
@pytest.mark.parametrize(
834+
("exception", "status_code", "message"),
835+
[
836+
(
837+
OSError("chipper-fast-fine-tuning is not a local folder"),
838+
400,
839+
"The Chipper model is not available for download. "
840+
"It can be accessed via the official hosted API.",
841+
),
842+
(
843+
OSError("ved-fine-tuning is not a local folder"),
844+
400,
845+
"The Chipper model is not available for download. "
846+
"It can be accessed via the official hosted API.",
847+
),
848+
(OSError(1, "An error happened"), 500, "[Errno 1] An error happened"),
849+
],
850+
)
851+
def test_chipper_not_available_errors(monkeypatch, mocker, exception, status_code, message):
852+
"""
853+
Assert that we return the right error if Chipper is not downloaded.
854+
OSError can have an int as the first arg, do not blow up if that happens.
855+
"""
856+
857+
mock_partition = Mock(side_effect=exception)
858+
859+
monkeypatch.setattr(
860+
general,
861+
"partition",
862+
mock_partition,
863+
)
864+
865+
client = TestClient(app)
866+
test_file = Path("sample-docs") / "layout-parser-paper-fast.pdf"
867+
868+
resp = client.post(
869+
MAIN_API_ROUTE,
870+
files=[("files", (str(test_file), open(test_file, "rb"), "application/pdf"))],
871+
data={"strategy": "hi_res", "hi_res_model_name": "chipper"},
872+
)
873+
874+
assert resp.status_code == status_code
875+
assert resp.json().get("detail") == message

0 commit comments

Comments
 (0)