Skip to content

Commit 25efad6

Browse files
authored
feat: return 422 errors for unprocessable files (#328)
Closes #326. Differentiate unprocessable entity errors from incorrect parameters, etc.
1 parent e6b9f0e commit 25efad6

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

prepline_general/api/general.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def pipeline_api(
466466
raise e
467467
except zipfile.BadZipFile:
468468
raise HTTPException(
469-
status_code=400,
469+
status_code=422,
470470
detail="File is not a valid docx",
471471
)
472472

@@ -523,7 +523,7 @@ def _check_pdf(file):
523523
detail="File is encrypted. Please decrypt it with password.",
524524
)
525525
except pypdf.errors.PdfReadError:
526-
raise HTTPException(status_code=400, detail="File does not appear to be a valid PDF")
526+
raise HTTPException(status_code=422, detail="File does not appear to be a valid PDF")
527527

528528

529529
def _validate_strategy(m_strategy):
@@ -807,7 +807,7 @@ def join_responses(responses):
807807
)
808808
else:
809809
raise HTTPException(
810-
detail='Request parameter "files" is required.\n',
810+
detail='Request parameter "files" is required.',
811811
status_code=status.HTTP_400_BAD_REQUEST,
812812
)
813813

test_general/api/test_app.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,9 @@ def test_general_api_returns_400_unsupported_file(example_filename):
398398
assert response.status_code == 400
399399

400400

401-
def test_general_api_returns_400_bad_pdf():
401+
def test_general_api_returns_422_bad_pdf():
402402
"""
403-
Verify that we get a 400 for invalid PDF files
403+
Verify that we get a 422 for invalid PDF files
404404
"""
405405
tmp = tempfile.NamedTemporaryFile(suffix=".pdf")
406406
tmp.write(b"This is not a valid PDF")
@@ -409,7 +409,7 @@ def test_general_api_returns_400_bad_pdf():
409409
MAIN_API_ROUTE, files=[("files", (str(tmp.name), open(tmp.name, "rb"), "application/pdf"))]
410410
)
411411
assert response.json() == {"detail": "File does not appear to be a valid PDF"}
412-
assert response.status_code == 400
412+
assert response.status_code == 422
413413
tmp.close()
414414

415415
# Don't blow up if this isn't actually a pdf
@@ -420,7 +420,7 @@ def test_general_api_returns_400_bad_pdf():
420420
)
421421

422422
assert response.json() == {"detail": "File does not appear to be a valid PDF"}
423-
assert response.status_code == 400
423+
assert response.status_code == 422
424424

425425

426426
def test_general_api_returns_503(monkeypatch):
@@ -757,7 +757,7 @@ def test_encrypted_pdf():
757757
assert response.status_code == 200
758758

759759

760-
def test_general_api_returns_400_bad_docx():
760+
def test_general_api_returns_422_bad_docx():
761761
"""
762762
Verify that we get a 400 for invalid docx files
763763
"""
@@ -777,7 +777,7 @@ def test_general_api_returns_400_bad_docx():
777777
],
778778
)
779779
assert response.json().get("detail") == "File is not a valid docx"
780-
assert response.status_code == 400
780+
assert response.status_code == 422
781781

782782

783783
def test_general_api_returns_400_bad_json(tmpdir):

0 commit comments

Comments
 (0)