Skip to content

Commit

Permalink
Add error message from api request. (#15657)
Browse files Browse the repository at this point in the history
* 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 <andrew@andrewduthie.com>
  • Loading branch information
spacedmonkey and aduth committed Jun 28, 2019
1 parent 95e91ce commit ffa4a27
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/e2e-tests/specs/change-detection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
] );
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } ]
)
);
},
Expand Down
17 changes: 11 additions & 6 deletions packages/editor/src/store/utils/notice-builder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -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,
} ];
Expand Down
10 changes: 5 additions & 5 deletions packages/editor/src/store/utils/test/notice-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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,
Expand Down

0 comments on commit ffa4a27

Please sign in to comment.