Skip to content

Commit

Permalink
fix: where there are no words to align, return 422, not 500
Browse files Browse the repository at this point in the history
  • Loading branch information
joanise authored and roedoejet committed Feb 27, 2023
1 parent d80d8ad commit 61d45e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions readalongs/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ async def assemble(

# tokenize
tokenized = tokenize_xml(parsed)

if not tokenized.xpath(".//w"):
raise HTTPException(
status_code=422,
detail="Could not find any words to align in the text.",
)

# add ids
ids_added = add_ids(tokenized)

Expand Down
12 changes: 12 additions & 0 deletions test/test_web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ def test_g2p_faiture(self):
content = response.json()
self.assertIn("No valid g2p conversion", content["detail"])

def test_no_words(self):
# Test the assemble endpoint with no actual words in the text
request = {
"input": ".!",
"type": "text/plain",
"text_languages": ["eng"],
}
response = API_CLIENT.post("/api/v1/assemble", json=request)
self.assertEqual(response.status_code, 422)
content = response.json()
self.assertIn("Could not find any words", content["detail"])

def test_langs(self):
# Test the langs endpoint
response = API_CLIENT.get("/api/v1/langs")
Expand Down

0 comments on commit 61d45e0

Please sign in to comment.