add more robust handling of errors thrown by api

This commit is contained in:
Keith Grant 2020-01-31 14:10:04 -08:00
parent b8226109a7
commit 0f9c906a22
3 changed files with 11 additions and 4 deletions

View File

@ -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 (
<Fragment>

View File

@ -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);

View File

@ -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);