Skip to content

Commit

Permalink
Display server error message when one exists (#7434)
Browse files Browse the repository at this point in the history
* Display server error message when one exists

* Use `has` and `get`
  • Loading branch information
danielbachhuber authored Jun 22, 2018
1 parent 58c96ee commit b16a8b2
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions utils/mediaupload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External Dependencies
*/
import { compact, flatMap, forEach, get, includes, map, noop, startsWith } from 'lodash';
import { compact, flatMap, forEach, get, has, includes, map, noop, startsWith } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -122,16 +122,22 @@ export function mediaUpload( {
};
setAndUpdateFiles( idx, mediaObject );
},
() => {
( response ) => {
// Reset to empty on failure.
setAndUpdateFiles( idx, null );
onError( {
code: 'GENERAL',
message: sprintf(
let message;
if ( has( response, [ 'responseJSON', 'message' ] ) ) {
message = get( response, [ 'responseJSON', 'message' ] );
} else {
message = sprintf(
// translators: %s: file name
__( 'Error while uploading file %s to the media library.' ),
mediaFile.name
),
);
}
onError( {
code: 'GENERAL',
message,
file: mediaFile,
} );
}
Expand Down

0 comments on commit b16a8b2

Please sign in to comment.