Added some error handling if API returns an API error.

Caught the error and displayed an error PNG so it is obvious something
failed. Currently it displayed a thumbnail of a png over the top of the
loading page, and therefore it wasn't obvious of the state.
This commit is contained in:
Richard Chapman
2018-03-01 13:01:54 +00:00
committed by Ken Tsang
parent 64e706ec76
commit 1f69d882c2
4 changed files with 25 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

BIN
app/assets/images/team.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

View File

@@ -2,6 +2,7 @@
import base64 import base64
from datetime import datetime from datetime import datetime
import os
from flask import ( from flask import (
Response, Response,
abort, abort,
@@ -12,6 +13,7 @@ from flask import (
url_for, url_for,
) )
from flask_login import login_required from flask_login import login_required
from notifications_python_client.errors import APIError
from app import ( from app import (
current_service, current_service,
@@ -55,6 +57,7 @@ def view_notification(service_id, notification_id):
show_recipient=True, show_recipient=True,
redact_missing_personalisation=True, redact_missing_personalisation=True,
) )
template.values = get_all_personalisation_from_notification(notification) template.values = get_all_personalisation_from_notification(notification)
if notification['job']: if notification['job']:
job = job_api_client.get_job(service_id, notification['job']['id'])['data'] job = job_api_client.get_job(service_id, notification['job']['id'])['data']
@@ -108,14 +111,20 @@ def view_letter_notification_as_preview(service_id, notification_id, filetype):
template.values = notification['personalisation'] template.values = notification['personalisation']
preview = notification_api_client.get_notification_letter_preview( try:
service_id, preview = notification_api_client.get_notification_letter_preview(
notification_id, service_id,
filetype, notification_id,
page=request.args.get('page') filetype,
) page=request.args.get('page')
)
return base64.b64decode(preview['content']) display_file = base64.b64decode(preview['content'])
except APIError:
with open(os.path.join(os.path.dirname(__file__), "../../assets/images/preview_error.png"), "rb") as file:
display_file = file.read()
return display_file
@main.route("/services/<service_id>/notification/<notification_id>.json") @main.route("/services/<service_id>/notification/<notification_id>.json")

View File

@@ -35,11 +35,17 @@
Estimated delivery date: {{ estimated_letter_delivery_date|string|format_date_short }} Estimated delivery date: {{ estimated_letter_delivery_date|string|format_date_short }}
</p> </p>
<p class="bottom-gutter"> <p class="bottom-gutter">
<a href="{{ url_for('main.view_letter_notification_as_preview', service_id=current_service.id, notification_id=notification_id, filetype='pdf') }}" download>Download as a PDF</a> {% if not error %}
</p> <a href="{{ url_for('main.view_letter_notification_as_preview', service_id=current_service.id, notification_id=notification_id, filetype='pdf') }}" download>Download as a PDF</a>
{% endif %}
</p>
{% endif %} {% endif %}
{{ template|string }} {% if error %}
Error Creating PDF Preview
{% else %}
{{ template|string }}
{% endif %}
{% if template.template_type != 'letter' %} {% if template.template_type != 'letter' %}
{{ ajax_block(partials, updates_url, 'status', finished=finished) }} {{ ajax_block(partials, updates_url, 'status', finished=finished) }}