Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Wrap weasyprint to catch exceptions #6728

Merged
merged 6 commits into from Dec 27, 2023
Merged

Conversation

pselkirk
Copy link
Collaborator

@pselkirk pselkirk commented Dec 3, 2023

Fixes #6324

@rjsparks
Copy link
Member

rjsparks commented Dec 5, 2023

Per real-time discussion - try using mock in the test.

Copy link

codecov bot commented Dec 6, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f3a574c) 88.78% compared to head (26cc393) 88.80%.
Report is 2 commits behind head on main.

❗ Current head 26cc393 differs from pull request most recent head 3421ea4. Consider uploading reports for the commit 3421ea4 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6728      +/-   ##
==========================================
+ Coverage   88.78%   88.80%   +0.01%     
==========================================
  Files         285      285              
  Lines       40324    40286      -38     
==========================================
- Hits        35800    35774      -26     
+ Misses       4524     4512      -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -974,7 +975,16 @@ def document_pdfized(request, name, rev=None, ext=None):
if not os.path.exists(doc.get_file_name()):
raise Http404("File not found: %s" % doc.get_file_name())

pdf = doc.pdfized()
try:
pdf = doc.pdfized()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when this fails, it's after a fairly long timeout - I wonder if two timeouts will take us past the cloudflare timeout window?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a log to indicate how often a retry occurs? (Assuming that general failures are also logged, this would let us evaluate whether it's worth trying a second time)

Longer term, this would be a candidate to make into an async response and defer to celery.

ietf/templates/doc/weasyprint_failed.html Outdated Show resolved Hide resolved
pdf = doc.pdfized()
try:
pdf = doc.pdfized()
except URLFetchingError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what we've seen it throw, but I don't want it to emit some other exception and float it all the way back to a 5xx crash either. I think a general catch here, logging the error, and returning a "something went wrong, and someone will be looking into it. please try again later" message would be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also suggest not bubbling weasyprint dependencies up into the view. The pdfize() method should really handle those and raise its own error class (or classes if it needs to distinguish, say, temporary from permanent problems or other classifications).

except URLFetchingError as exception:
return render(request, "doc/weasyprint_failed.html",
dict(error=f'{type(exception).__name__}: {exception}'),
status=504)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While 504 here is one technically correct to represent the situation , the key point of the issue is to not return any 5xx.

I would suggest a 200.

Copy link
Member

@jennifer-richards jennifer-richards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small (but I think important) nit.

return render(request, "doc/weasyprint_failed.html",
dict(error=f'{type(exception).__name__}: {exception}'),
status=504)
except:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be except Exception - a bare except catches low-level non-Exception interruptions that shouldn't normally be caught without re-raising.

@rjsparks rjsparks merged commit 748bcc3 into ietf-tools:main Dec 27, 2023
7 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wrap weasyprint to catch exceptions
3 participants