Skip to content

Commit 6602a09

Browse files
authored
feat: include the partial RAS XML with the 422 error when g2p fails in assemble (#292)
Squashes these commits: * feat: include the partial RAS XML with the 422 error when g2p fails in assemble * fix: make the g2p error words easier to see with spacing * feat: with g2p 422, send partially processed RAS file * test: add assertions about g2p errors in assemble call
1 parent f0000e9 commit 6602a09

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

readalongs/web_api.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
from fastapi import Body, FastAPI, HTTPException
3838
from fastapi.middleware.cors import CORSMiddleware
39-
from fastapi.responses import FileResponse
39+
from fastapi.responses import FileResponse, JSONResponse
4040
from lxml import etree
4141
from pydantic import BaseModel, Field
4242
from starlette.background import BackgroundTask
@@ -272,16 +272,20 @@ async def assemble(
272272
if not valid:
273273
if non_convertible_words:
274274
logs = (
275-
"These words could not be converted from text to phonemes by g2p: '"
276-
+ "', '".join(non_convertible_words)
277-
+ "'."
275+
"These words could not be converted from text to phonemes by g2p: ' "
276+
+ " ', ' ".join(non_convertible_words)
277+
+ " '."
278278
)
279279
else:
280280
logs = "Logs: " + captured_logs.getvalue()
281-
raise HTTPException(
281+
return JSONResponse(
282282
status_code=422,
283-
detail="g2p could not be performed, please check your text or your language code. "
284-
+ logs,
283+
content={
284+
"detail": "g2p could not be performed, please check your text or your language code. "
285+
+ logs,
286+
"g2p_error_words": non_convertible_words,
287+
"partial_ras": xml_to_string(g2ped),
288+
},
285289
)
286290
# create grammar
287291
dict_data, text_input = create_grammar(g2ped)

tests/test_web_api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,14 @@ def test_empty_g2p(self):
219219
with redirect_stderr(StringIO()):
220220
response = self.API_CLIENT.post("/api/v1/assemble", json=request)
221221
self.assertEqual(response.status_code, 422)
222-
content_log = response.json()["detail"]
222+
content = response.json()
223+
content_log = content["detail"]
223224
for message_part in ["These words could not", "24", "23"]:
224225
self.assertIn(message_part, content_log)
225226

227+
self.assertEqual(content["g2p_error_words"], ["24", "23", "99", "1234"])
228+
self.assertIn("partial_ras", content)
229+
226230
def test_langs(self):
227231
# Test the langs endpoint
228232
with redirect_stderr(StringIO()):

0 commit comments

Comments
 (0)