Skip to content

Commit

Permalink
fix: catch exceptions due to failure to create XML from text (#242)
Browse files Browse the repository at this point in the history
I don't know what causes this, but we got a few 5xx errors from the
Heroku logs with this trace:
```
   File "/app/readalongs/web_api.py", line 209, in assemble
     parsed = parse_xml(
              ^^^^^^^^^^
   File "/app/readalongs/text/util.py", line 138, in parse_xml
     return etree.fromstring(
            ^^^^^^^^^^^^^^^^^
 lxml.etree.XMLSyntaxError: PCDATA invalid Char value 3, line 8, column 26
```

While it makes no sense we should fail to parse XML we just generated
in this broader block:
```
    parsed = parse_xml(
        create_ras_from_text(
            (request.input_text or "").splitlines(keepends=True),
            text_languages=request.text_languages,
        )
    )
```
let's just put a try/except in there since we saw it live on Heroku.

Whatever the error in the input is, though I cannot reproduce it, should
generate a 422 HTTP code.
  • Loading branch information
joanise authored Oct 1, 2024
1 parent 791e0b9 commit 3cab240
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions readalongs/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,18 @@ async def assemble(
),
)
elif request.mime_type == InputFormat.TEXT:
parsed = parse_xml(
create_ras_from_text(
(request.input_text or "").splitlines(keepends=True),
text_languages=request.text_languages,
try:
parsed = parse_xml(
create_ras_from_text(
(request.input_text or "").splitlines(keepends=True),
text_languages=request.text_languages,
)
)
)
except etree.ParseError as e:
raise HTTPException(
status_code=422,
detail="XML could not be created from the provided text. This is an unexpected error, if you are willing to e-mail the text and the process that triggered it to [email protected], we would appreciate the opportunity to try to figure out what causes it.",
) from e

else: # pragma: no cover
raise HTTPException(
Expand Down

0 comments on commit 3cab240

Please sign in to comment.