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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ietf/doc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ def pdfized(self):
)
except AssertionError:
pdf = None
except Exception as e:
log.log('weasyprint failed:'+str(e))
raise
if pdf:
cache.set(cache_key, pdf, settings.PDFIZER_CACHE_TIME)
return pdf
Expand Down
10 changes: 9 additions & 1 deletion ietf/doc/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright The IETF Trust 2012-2020, All Rights Reserved
# Copyright The IETF Trust 2012-2023, All Rights Reserved
# -*- coding: utf-8 -*-


Expand Down Expand Up @@ -31,6 +31,8 @@

from tastypie.test import ResourceTestCaseMixin

from weasyprint.urls import URLFetchingError

import debug # pyflakes:ignore

from ietf.doc.models import ( Document, DocRelationshipName, RelatedDocument, State,
Expand Down Expand Up @@ -2844,6 +2846,12 @@ def test_pdfized(self):
self.should_succeed(dict(name=draft.name,rev=f'{r:02d}',ext=ext))
self.should_404(dict(name=draft.name,rev='02'))

with mock.patch('ietf.doc.models.DocumentInfo.pdfized', side_effect=URLFetchingError):
url = urlreverse(self.view, kwargs=dict(name=rfc.name))
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
self.assertContains(r, "Error while rendering PDF")

class NotifyValidationTests(TestCase):
def test_notify_validation(self):
valid_values = [
Expand Down
6 changes: 4 additions & 2 deletions ietf/doc/views_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
from django import forms
from django.contrib.staticfiles import finders


import debug # pyflakes:ignore

from ietf.doc.models import ( Document, DocHistory, DocEvent, BallotDocEvent, BallotType,
Expand Down Expand Up @@ -1064,7 +1063,10 @@ 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.

except Exception:
return render(request, "doc/weasyprint_failed.html")
if pdf:
return HttpResponse(pdf,content_type='application/pdf')
else:
Expand Down
17 changes: 17 additions & 0 deletions ietf/templates/doc/weasyprint_failed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{# Copyright The IETF Trust 2023, All Rights Reserved #}
{% extends "base.html" %}
{% load origin %}
{% block title %}Error while rendering PDF{% endblock %}
{% block content %}
{% origin %}
<h1>Error while rendering PDF</h1>
<p>
An error was encountered while trying to render your document as PDF.
In case this was a temporary error, you may want to try again in a
little while.
</p>
<p>
A failure report with details about what happened has been sent to the
server administrators.
</p>
{% endblock %}
Loading