From 0f9c906a22094f310ac288198d279dec5ec075dc Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Fri, 31 Jan 2020 14:10:04 -0800 Subject: [PATCH] add more robust handling of errors thrown by api --- awx/ui_next/src/components/ErrorDetail/ErrorDetail.jsx | 9 +++++++-- awx/ui_next/src/screens/Host/HostAdd/HostAdd.jsx | 3 ++- awx/ui_next/src/screens/Host/HostEdit/HostEdit.jsx | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/awx/ui_next/src/components/ErrorDetail/ErrorDetail.jsx b/awx/ui_next/src/components/ErrorDetail/ErrorDetail.jsx index 25d2c9d035..1ef3cabc99 100644 --- a/awx/ui_next/src/components/ErrorDetail/ErrorDetail.jsx +++ b/awx/ui_next/src/components/ErrorDetail/ErrorDetail.jsx @@ -53,8 +53,13 @@ class ErrorDetail extends Component { const { error } = this.props; const { response } = error; - const message = - typeof response.data === 'string' ? response.data : response.data.detail; + let message = ''; + if (response.data) { + message = + typeof response.data === 'string' + ? response.data + : response.data?.detail; + } return ( diff --git a/awx/ui_next/src/screens/Host/HostAdd/HostAdd.jsx b/awx/ui_next/src/screens/Host/HostAdd/HostAdd.jsx index b85f1130b2..b1b1d5e277 100644 --- a/awx/ui_next/src/screens/Host/HostAdd/HostAdd.jsx +++ b/awx/ui_next/src/screens/Host/HostAdd/HostAdd.jsx @@ -27,7 +27,8 @@ function HostAdd({ i18n }) { const { data: response } = await HostsAPI.create(values); history.push(`${url}/${response.id}/details`); } catch (error) { - if (error.response?.data) { + // check for field-specific errors from API + if (error.response?.data && typeof error.response.data === 'object') { throw error.response.data; } setFormError(error); diff --git a/awx/ui_next/src/screens/Host/HostEdit/HostEdit.jsx b/awx/ui_next/src/screens/Host/HostEdit/HostEdit.jsx index 401586d827..d3fad5fb49 100644 --- a/awx/ui_next/src/screens/Host/HostEdit/HostEdit.jsx +++ b/awx/ui_next/src/screens/Host/HostEdit/HostEdit.jsx @@ -35,7 +35,8 @@ function HostEdit({ host, i18n }) { await HostsAPI.update(host.id, values); history.push(detailsUrl); } catch (error) { - if (error.response?.data) { + // check for field-specific errors from API + if (error.response?.data && typeof error.response.data === 'object') { throw error.response.data; } setFormError(error);