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
from datetime import datetime
import os
from flask import (
Response,
abort,
@@ -12,6 +13,7 @@ from flask import (
url_for,
)
from flask_login import login_required
from notifications_python_client.errors import APIError
from app import (
current_service,
@@ -55,6 +57,7 @@ def view_notification(service_id, notification_id):
show_recipient=True,
redact_missing_personalisation=True,
)
template.values = get_all_personalisation_from_notification(notification)
if notification['job']:
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']
preview = notification_api_client.get_notification_letter_preview(
service_id,
notification_id,
filetype,
page=request.args.get('page')
)
try:
preview = notification_api_client.get_notification_letter_preview(
service_id,
notification_id,
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")

View File

@@ -35,11 +35,17 @@
Estimated delivery date: {{ estimated_letter_delivery_date|string|format_date_short }}
</p>
<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>
</p>
{% if not error %}
<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 %}
{{ template|string }}
{% if error %}
Error Creating PDF Preview
{% else %}
{{ template|string }}
{% endif %}
{% if template.template_type != 'letter' %}
{{ ajax_block(partials, updates_url, 'status', finished=finished) }}