diff --git a/source/changelog.txt b/source/changelog.txt index 6125179..76fd494 100644 --- a/source/changelog.txt +++ b/source/changelog.txt @@ -8,6 +8,7 @@ #### Deprecated #### Removed #### Fixed +- Show group not found error message in block #### Security - Escape translated strings to prevent HTML injections diff --git a/source/front/blocks/events-list/edit.js b/source/front/blocks/events-list/edit.js index d46b5ae..c24ac73 100644 --- a/source/front/blocks/events-list/edit.js +++ b/source/front/blocks/events-list/edit.js @@ -39,7 +39,14 @@ export default ({ attributes, setAttributes }) => { } container.querySelector('a').href = showMoreUrl await fetch(url) - .then((response) => response.text()) + .then(async (response) => { + if (!response.ok) { + // Reject if no 2xx response. + const data = await response.text() + return Promise.reject(data) + } + return response.text() + }) .then((data) => { const events = JSON.parse(data) displayEvents({ @@ -50,7 +57,8 @@ export default ({ attributes, setAttributes }) => { }) }) .catch((data) => { - displayErrorMessage({ data, container }) + const parsedData = JSON.parse(data) + displayErrorMessage({ data: parsedData, container }) }) } }, 500) diff --git a/source/front/events-displayer-test.js b/source/front/events-displayer-test.js index 994f855..8b22eae 100644 --- a/source/front/events-displayer-test.js +++ b/source/front/events-displayer-test.js @@ -83,13 +83,7 @@ test('#displayErrorMessage general error message display', (t) => { test('#displayErrorMessage group not found error message display', (t) => { const container = t.context.container const data = { - response: { - errors: [ - { - code: 'group_not_found', - }, - ], - }, + code: 'group_not_found', } displayErrorMessage({ data, container }) t.is(container.querySelector('.general-error').style.display, 'none') diff --git a/source/front/events-displayer.js b/source/front/events-displayer.js index c00b723..62fd252 100644 --- a/source/front/events-displayer.js +++ b/source/front/events-displayer.js @@ -72,13 +72,7 @@ export function displayEvents({ events, document, container, maxEventsCount }) { export function displayErrorMessage({ data, container }) { hideLoadingIndicator(container) - if ( - Object.prototype.hasOwnProperty.call(data, 'response') && - Object.prototype.hasOwnProperty.call(data.response, 'errors') && - data.response.errors.length > 0 && - Object.prototype.hasOwnProperty.call(data.response.errors[0], 'code') && - data.response.errors[0].code === 'group_not_found' - ) { + if (Object.hasOwn(data, 'code') && data.code === 'group_not_found') { const message = container.querySelector('.group-not-found') message.style.display = 'block' } else {