diff --git a/readalongs/web_api.py b/readalongs/web_api.py index 08e4af3f..c4ac5280 100644 --- a/readalongs/web_api.py +++ b/readalongs/web_api.py @@ -36,7 +36,7 @@ from fastapi import Body, FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware -from fastapi.responses import FileResponse +from fastapi.responses import FileResponse, JSONResponse from lxml import etree from pydantic import BaseModel, Field from starlette.background import BackgroundTask @@ -272,16 +272,20 @@ async def assemble( if not valid: if non_convertible_words: logs = ( - "These words could not be converted from text to phonemes by g2p: '" - + "', '".join(non_convertible_words) - + "'." + "These words could not be converted from text to phonemes by g2p: ' " + + " ', ' ".join(non_convertible_words) + + " '." ) else: logs = "Logs: " + captured_logs.getvalue() - raise HTTPException( + return JSONResponse( status_code=422, - detail="g2p could not be performed, please check your text or your language code. " - + logs, + content={ + "detail": "g2p could not be performed, please check your text or your language code. " + + logs, + "g2p_error_words": non_convertible_words, + "partial_ras": xml_to_string(g2ped), + }, ) # create grammar dict_data, text_input = create_grammar(g2ped) diff --git a/tests/test_web_api.py b/tests/test_web_api.py index 6215209e..c8ba4c20 100755 --- a/tests/test_web_api.py +++ b/tests/test_web_api.py @@ -219,10 +219,14 @@ def test_empty_g2p(self): with redirect_stderr(StringIO()): response = self.API_CLIENT.post("/api/v1/assemble", json=request) self.assertEqual(response.status_code, 422) - content_log = response.json()["detail"] + content = response.json() + content_log = content["detail"] for message_part in ["These words could not", "24", "23"]: self.assertIn(message_part, content_log) + self.assertEqual(content["g2p_error_words"], ["24", "23", "99", "1234"]) + self.assertIn("partial_ras", content) + def test_langs(self): # Test the langs endpoint with redirect_stderr(StringIO()):