Skip to content

Commit

Permalink
fix: catch exceptions due to failure to create XML from text
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 committed Oct 1, 2024
1 parent 791e0b9 commit f545ebb
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(

Check warning on line 217 in readalongs/web_api.py

View check run for this annotation

Codecov / codecov/patch

readalongs/web_api.py#L216-L217

Added lines #L216 - L217 were not covered by tests
status_code=422,
detail="XML could not be created from the provided text",
) from e

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

0 comments on commit f545ebb

Please sign in to comment.