Skip to content

Commit

Permalink
fix: Wrap weasyprint to catch exceptions (ietf-tools#6324)
Browse files Browse the repository at this point in the history
  • Loading branch information
pselkirk committed Dec 3, 2023
1 parent c1627ed commit c9d587f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ietf/doc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2843,6 +2843,12 @@ def test_pdfized(self):
self.should_succeed(dict(name=rfc.name,rev=f'{r:02d}',ext=ext))
self.should_404(dict(name=rfc.name,rev='02'))

import socket
socket.socket = lambda *args, **kwargs: None
url = urlreverse(self.view, kwargs=dict(name=rfc.name))
r = self.client.get(url)
self.assertEqual(r.status_code, 504)

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

from weasyprint.urls import URLFetchingError

import debug # pyflakes:ignore

Expand Down Expand Up @@ -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()
except URLFetchingError:
# retry once, then give up
try:
pdf = doc.pdfized()
except URLFetchingError as exception:
return render(request, "doc/weasyprint_failed.html",
dict(error=f'{type(exception).__name__}: {exception}'),
status=504)
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 %}Network error while rendering PDF{% endblock %}
{% block content %}
{% origin %}
<h1>Network error while rendering PDF</h1>
<p>
A network error was encountered while trying to render your document as PDF.
</p>
<p>
We have retried the operation, and it failed again. You may want to wait a bit, and try again.
</p>
<p>
The error was: <code>{{ error }}</code>
</p>
{% endblock %}

0 comments on commit c9d587f

Please sign in to comment.