Handle exception is org name already exists

Previously this would return a 500 error, as the 400 exception was
not handled from the API [1]. Note that:

- We tend to rely on exception messages to identify the error that
occurred [2][3], with services being a notable deviation [4]. I've
used the exception message approach, as this is more granular and
broadly consistent with the rest of the app.

- There is already code to cover this scenario when a user changes
the name of an existing organisation or service, but the mechanism
is different [5][6]. It makes sense to just get any error from the
call to try and create the organisation.

- The API mock is based on one for services [7], but I've chosen to
have it inline with the test, since we're unlikely to reuse it, and
it's clearer to have the test setup as part of the test.

[1]: 8f99da525d/app/organisation/rest.py (L34-L47)
[2]: 70b606a2d4/app/main/views/manage_users.py (L166)
[3]: 70b606a2d4/app/main/views/templates.py (L499)
[4]: 70b606a2d4/app/main/views/add_service.py (L30)
[5]: 70b606a2d4/app/main/views/service_settings.py (L102-L104)
[6]: 70b606a2d4/app/main/views/organisations.py (L264-L266)
[7]: 0abc143147/tests/conftest.py (L590-L606)
This commit is contained in:
Ben Thorner
2021-02-11 12:02:42 +00:00
parent 0199709adb
commit 52a5da4d17
2 changed files with 42 additions and 4 deletions

View File

@@ -62,10 +62,17 @@ def add_organisation():
form = NewOrganisationForm()
if form.validate_on_submit():
return redirect(url_for(
'.organisation_settings',
org_id=Organisation.create_from_form(form).id,
))
try:
return redirect(url_for(
'.organisation_settings',
org_id=Organisation.create_from_form(form).id,
))
except HTTPError as e:
msg = 'Organisation name already exists'
if e.status_code == 400 and msg in e.message:
form.name.errors.append("This organisation name is already in use")
else:
raise e
return render_template(
'views/organisations/add-organisation.html',