Show error when user cannot be archived

A user can't be archived if they are the only member of their service
with `manage_settings` permission. `notifications-api` returns a `400`
and an error message if that is the case, however this PR to remove the
`400` error handler
https://github.com/alphagov/notifications-admin/pull/3320 stopped the
error message from showing. This meant that instead of seeing a message
about why a user couldn't be archived, we would just show a `500` error
page instead. This change checks the response from `notifications-api`
and shows an error banner with a message if the user can't be archived.
This commit is contained in:
Katie Smith
2020-04-21 17:40:47 +01:00
parent f91aba212d
commit ffdcda02bf
2 changed files with 42 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
from flask import flash, redirect, render_template, request, url_for
from flask_login import current_user
from notifications_python_client.errors import HTTPError
from app import user_api_client
from app.event_handlers import create_archive_user_event
@@ -36,7 +37,13 @@ def user_information(user_id):
@user_is_platform_admin
def archive_user(user_id):
if request.method == 'POST':
user_api_client.archive_user(user_id)
try:
user_api_client.archive_user(user_id)
except HTTPError as e:
if e.status_code == 400 and 'manage_settings' in e.message:
flash('User cant be removed from a service - '
'check all services have another team member with manage_settings')
return redirect(url_for('main.user_information', user_id=user_id))
create_archive_user_event(str(user_id), current_user.id)
return redirect(url_for('.user_information', user_id=user_id))