From ffa4a27077e426e652abdf8390497a16dd9ad62c Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 28 Jun 2019 16:44:54 +0200 Subject: [PATCH] Add error message from api request. (#15657) * Add error message from api request. * Use sprintf for error message. * Add comment * Fix tests for new error message * More linting fixes * Fix phpunit tests * Improve the tests, to reflect message in error. * Fix text of message in e2e. * Update packages/editor/src/store/utils/notice-builder.js Change comment. Co-Authored-By: Andrew Duthie --- .../e2e-tests/specs/change-detection.test.js | 2 +- packages/editor/src/store/test/actions.js | 2 +- .../editor/src/store/utils/notice-builder.js | 17 +++++++++++------ .../src/store/utils/test/notice-builder.js | 10 +++++----- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/e2e-tests/specs/change-detection.test.js b/packages/e2e-tests/specs/change-detection.test.js index 7c9dab21d4d79..30895ce5590de 100644 --- a/packages/e2e-tests/specs/change-detection.test.js +++ b/packages/e2e-tests/specs/change-detection.test.js @@ -199,7 +199,7 @@ describe( 'Change detection', () => { // Ensure save update fails and presents button. page.waitForXPath( - '//*[contains(@class, "components-notice") and contains(@class, "is-error")]/*[text()="Updating failed"]' + '//*[contains(@class, "components-notice") and contains(@class, "is-error")]/*[text()="Updating failed. Error message: The response is not a valid JSON response."]' ), page.waitForSelector( '.editor-post-save-draft' ), ] ); diff --git a/packages/editor/src/store/test/actions.js b/packages/editor/src/store/test/actions.js index ab698ea82a731..dd78100a4385d 100644 --- a/packages/editor/src/store/test/actions.js +++ b/packages/editor/src/store/test/actions.js @@ -337,7 +337,7 @@ describe( 'Post generator actions', () => { dispatch( 'core/notices', 'createErrorNotice', - ...[ 'Updating failed', { id: 'SAVE_POST_NOTICE_ID' } ] + ...[ 'Updating failed.', { id: 'SAVE_POST_NOTICE_ID' } ] ) ); }, diff --git a/packages/editor/src/store/utils/notice-builder.js b/packages/editor/src/store/utils/notice-builder.js index 9732cff6fa7cf..eb8172e9801dd 100644 --- a/packages/editor/src/store/utils/notice-builder.js +++ b/packages/editor/src/store/utils/notice-builder.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; /** * Internal dependencies @@ -97,14 +97,19 @@ export function getNotificationArgumentsForSaveFail( data ) { // If the post was being published, we show the corresponding publish error message // Unless we publish an "updating failed" message const messages = { - publish: __( 'Publishing failed' ), - private: __( 'Publishing failed' ), - future: __( 'Scheduling failed' ), + publish: __( 'Publishing failed.' ), + private: __( 'Publishing failed.' ), + future: __( 'Scheduling failed.' ), }; - const noticeMessage = ! isPublished && publishStatus.indexOf( edits.status ) !== -1 ? + let noticeMessage = ! isPublished && publishStatus.indexOf( edits.status ) !== -1 ? messages[ edits.status ] : - __( 'Updating failed' ); + __( 'Updating failed.' ); + // Check if message string contains HTML. Notice text is currently only + // supported as plaintext, and stripping the tags may muddle the meaning. + if ( error.message && ! ( /<\/?[^>]*>/.test( error.message ) ) ) { + noticeMessage = sprintf( __( '%1$s Error message: %2$s' ), noticeMessage, error.message ); + } return [ noticeMessage, { id: SAVE_POST_NOTICE_ID, } ]; diff --git a/packages/editor/src/store/utils/test/notice-builder.js b/packages/editor/src/store/utils/test/notice-builder.js index e67215113c1f1..3e6abab360bca 100644 --- a/packages/editor/src/store/utils/test/notice-builder.js +++ b/packages/editor/src/store/utils/test/notice-builder.js @@ -93,7 +93,7 @@ describe( 'getNotificationArgumentsForSaveSuccess()', () => { } ); } ); describe( 'getNotificationArgumentsForSaveFail()', () => { - const error = { code: '42' }; + const error = { code: '42', message: 'Something went wrong.' }; const post = { status: 'publish' }; const edits = { status: 'publish' }; const defaultExpectedAction = { id: SAVE_POST_NOTICE_ID }; @@ -108,25 +108,25 @@ describe( 'getNotificationArgumentsForSaveFail()', () => { 'when post is not published and edits is published', '', [ 'draft', 'publish' ], - [ 'Publishing failed', defaultExpectedAction ], + [ 'Publishing failed. Error message: Something went wrong.', defaultExpectedAction ], ], [ 'when post is published and edits is privately published', '', [ 'draft', 'private' ], - [ 'Publishing failed', defaultExpectedAction ], + [ 'Publishing failed. Error message: Something went wrong.', defaultExpectedAction ], ], [ 'when post is published and edits is scheduled to be published', '', [ 'draft', 'future' ], - [ 'Scheduling failed', defaultExpectedAction ], + [ 'Scheduling failed. Error message: Something went wrong.', defaultExpectedAction ], ], [ 'when post is published and edits is published', '', [ 'publish', 'publish' ], - [ 'Updating failed', defaultExpectedAction ], + [ 'Updating failed. Error message: Something went wrong.', defaultExpectedAction ], ], ].forEach( ( [ description,