Merge pull request #17374 from strapi/chore/cleanup-rw-error-handling

Chore: Cleanup review-workflow API error handling
This commit is contained in:
Gustav Hansen 2023-07-19 17:43:40 +02:00 committed by GitHub
commit 9872632b0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 30 deletions

View File

@ -86,20 +86,12 @@ export function ReviewWorkflowsCreateView() {
return workflow; return workflow;
} catch (error) { } catch (error) {
// TODO: the current implementation of `formatAPIError` prints all error messages of all details, // TODO: this would benefit from a utility to get a formik error
// which get's hairy when we have duplicated-name errors, because the same error message is printed // representation from an API error
// several times. What we want instead in these scenarios is to print only the error summary and
// display the individual error messages for each field. This is a workaround, until we change the
// implementation of `formatAPIError`.
if ( if (
error.response.data?.error?.name === 'ValidationError' && error.response.data?.error?.name === 'ValidationError' &&
error.response.data?.error?.details?.errors?.length > 0 error.response.data?.error?.details?.errors?.length > 0
) { ) {
toggleNotification({
type: 'warning',
message: error.response.data.error.message,
});
setInitialErrors( setInitialErrors(
error.response.data?.error?.details?.errors.reduce((acc, error) => { error.response.data?.error?.details?.errors.reduce((acc, error) => {
set(acc, error.path, error.message); set(acc, error.path, error.message);
@ -107,12 +99,12 @@ export function ReviewWorkflowsCreateView() {
return acc; return acc;
}, {}) }, {})
); );
} else { }
toggleNotification({ toggleNotification({
type: 'warning', type: 'warning',
message: formatAPIError(error), message: formatAPIError(error),
}); });
}
return null; return null;
} }

View File

@ -97,20 +97,12 @@ export function ReviewWorkflowsEditView() {
return res; return res;
} catch (error) { } catch (error) {
// TODO: the current implementation of `formatAPIError` prints all error messages of all details, // TODO: this would benefit from a utility to get a formik error
// which get's hairy when we have duplicated-name errors, because the same error message is printed // representation from an API error
// several times. What we want instead in these scenarios is to print only the error summary and
// display the individual error messages for each field. This is a workaround, until we change the
// implementation of `formatAPIError`.
if ( if (
error.response.data?.error?.name === 'ValidationError' && error.response.data?.error?.name === 'ValidationError' &&
error.response.data?.error?.details?.errors?.length > 0 error.response.data?.error?.details?.errors?.length > 0
) { ) {
toggleNotification({
type: 'warning',
message: error.response.data.error.message,
});
setInitialErrors( setInitialErrors(
error.response.data?.error?.details?.errors.reduce((acc, error) => { error.response.data?.error?.details?.errors.reduce((acc, error) => {
set(acc, error.path, error.message); set(acc, error.path, error.message);
@ -118,12 +110,12 @@ export function ReviewWorkflowsEditView() {
return acc; return acc;
}, {}) }, {})
); );
} else { }
toggleNotification({ toggleNotification({
type: 'warning', type: 'warning',
message: formatAPIError(error), message: formatAPIError(error),
}); });
}
return null; return null;
} }