Skip to content

Commit

Permalink
UPRN error handling now has propper user feedback for UPRN input
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixim0 committed Nov 21, 2024
1 parent 252161e commit c939d11
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
29 changes: 19 additions & 10 deletions src/aims_ui/page_controllers/a_single_matches/page_uprn.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from flask import render_template, request, session
from requests.exceptions import ConnectionError
from flask_login import login_required

from aims_ui import app
from aims_ui.page_helpers.cookie_utils import delete_input, load_save_store_inputs, save_epoch_number
from aims_ui.page_helpers.api.api_interaction import api
from aims_ui.page_helpers.security_utils import detect_xml_injection, check_user_has_access_to_page
from aims_ui.page_helpers.pages_location_utils import get_page_location
from aims_ui.page_helpers.error.error_utils import error_page_xml, error_page_api_request, error_page_api_response
from aims_ui.models.get_addresses import get_addresses
from aims_ui.models.get_endpoints import get_endpoints
from aims_ui.models.get_fields import get_fields
from aims_ui.models.get_addresses import get_addresses
from aims_ui.page_controllers.f_error_pages.page_error import page_error
from aims_ui.page_controllers.f_error_pages.page_error_annotation_single import page_error_annotation_single
from aims_ui.page_helpers.api.api_interaction import api
from aims_ui.page_helpers.cookie_utils import delete_input, load_save_store_inputs, save_epoch_number
from aims_ui.page_helpers.error.error_utils import error_page_api_request, error_page_api_response, error_page_xml
from aims_ui.page_helpers.pages_location_utils import get_page_location
from aims_ui.page_helpers.security_utils import check_user_has_access_to_page, detect_xml_injection
from aims_ui.page_helpers.validation_utils import validate_uprn

page_name = 'uprn'

Expand Down Expand Up @@ -52,16 +53,24 @@ def uprn():
page_name,
all_user_input,
)

# Deal with errors connecting to the API
except Exception as e:
return error_page_api_request(page_name, user_input, e)

try:
validate_uprn(user_input)
except Exception as e:
# Return annotation page directly
return page_error_annotation_single(page_name, user_input, e.args[1])

override_with_blank = False
# Errors after sucessful Response
if result.status_code != 200:
return error_page_api_response(page_name, user_input, result)

matched_addresses = get_addresses(result.json(), page_name)
# Only extract addresses if matched_addresses not a blank array
matched_addresses = [] if override_with_blank else get_addresses(
result.json(), page_name)

# Save epoch number
save_epoch_number(session, all_user_input.get('epoch', ''))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from aims_ui.models.get_fields import get_fields
from aims_ui.page_helpers.google_utils import get_current_group
from aims_ui.page_helpers.pages_location_utils import get_page_location

""" Manage errors specific to multiple match pages """


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from aims_ui.models.get_endpoints import get_endpoints
from aims_ui.models.get_fields import get_fields
from aims_ui.page_helpers.pages_location_utils import get_page_location

""" When an error message would be better shown next to an input in the Design System, manage that here """


Expand Down Expand Up @@ -59,9 +60,7 @@ def match_api_error_message_to_name_of_field(primary_error_message):
return 'limit'

# UPRN specific errors
if 'UPRNs must be numeric' in primary_error_message:
return 'uprn'
if 'UPRN request didn' in primary_error_message:
if 'UPRN' in primary_error_message:
return 'uprn'

if 'Postcode supplied is not valid according to the UK addresses' in primary_error_message:
Expand Down
4 changes: 3 additions & 1 deletion src/aims_ui/page_helpers/error/error_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ def error_page_api_response(page_name, user_input, result):
if status_code == 404:
log_err(page_name, user_input, f'Not Found Error: "{clean_result}"')
# 404 errors that relate to blank input fields, otherwise a generic error message is shown
if (page_name == 'uprn') or (page_name == 'postcode'):
primary_error_message = get_primary_error_message(result, page_name,
user_input)
if page_name == 'postcode':
primary_error_message = 'Not found error. This is likely due to a blank search feild. Please check your inputs.'

return page_error_annotation_single(page_name,
Expand Down
26 changes: 26 additions & 0 deletions src/aims_ui/page_helpers/validation_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
""" Validation for user inputs when it isn't handled by submitting to the API and dealing with the error """


def validate_uprn(uprn):
""" Check if a UPRN complies with valid format """
# Raise exception with title 'Limit Parameter Error' and description 'Limit parameter must be a positive integer'

basic_message = 'UPRN must be numeric. They can have up to 12 digits and have no leading zeros.'
additional_message = ''
if not uprn.isdigit():
if uprn != '':
additional_message = additional_message + '<br> The UPRN you entered is not numeric.'
else:
additional_message = additional_message + '<br> The UPRN you entered is blank.'

# Check not longer than 12
if len(uprn) > 12:
additional_message = additional_message + '<br> The UPRN you entered is longer than 12.'

# Check for leading zeros
if str(uprn).startswith('0'):
additional_message = additional_message + '<br> The UPRN you entered has leading zeros.'

if additional_message != '':
raise Exception('UPRN Parameter Error',
f'{basic_message} {additional_message}')

0 comments on commit c939d11

Please sign in to comment.