Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #189 from ckeditor/t/186
Browse files Browse the repository at this point in the history
Fix: ImageUploadEditing throws unhandled async errors. Closes #186.
  • Loading branch information
oskarwrobel authored Mar 16, 2018
2 parents 25c17ad + c7fecd0 commit 4357336
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/imageupload/imageuploadediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export default class ImageUploadEditing extends Plugin {
* @private
* @param {module:upload/filerepository~FileLoader} loader
* @param {module:engine/model/element~Element} imageElement
* @returns {Promise}
*/
_load( loader, imageElement ) {
const editor = this.editor;
Expand All @@ -128,7 +129,7 @@ export default class ImageUploadEditing extends Plugin {
writer.setAttribute( 'uploadStatus', 'reading', imageElement );
} );

loader.read()
return loader.read()
.then( data => {
const viewFigure = editor.editing.mapper.toViewElement( imageElement );
const viewImg = viewFigure.getChild( 0 );
Expand Down Expand Up @@ -178,10 +179,16 @@ export default class ImageUploadEditing extends Plugin {

clean();
} )
.catch( msg => {
.catch( error => {
// If status is not 'error' nor 'aborted' - throw error because it means that something else went wrong,
// it might be generic error and it would be real pain to find what is going on.
if ( loader.status !== 'error' && loader.status !== 'aborted' ) {
throw error;
}

// Might be 'aborted'.
if ( loader.status == 'error' ) {
notification.showWarning( msg, {
notification.showWarning( error, {
title: t( 'Upload failed' ),
namespace: 'upload'
} );
Expand Down
33 changes: 33 additions & 0 deletions tests/imageupload/imageuploadediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,39 @@ describe( 'ImageUploadEditing', () => {
}, 0 );
} );

it( 'should throw when other error happens during upload', done => {
const file = createNativeFileMock();
const error = new Error( 'Foo bar baz' );
const uploadEditing = editor.plugins.get( ImageUploadEditing );
const loadSpy = sinon.spy( uploadEditing, '_load' );
const catchSpy = sinon.spy();

// Throw an error when async attribute change occur.
editor.editing.downcastDispatcher.on( 'attribute:uploadStatus:image', ( evt, data ) => {
if ( data.attributeNewValue == 'uploading' ) {
throw error;
}
} );

setModelData( model, '<paragraph>{}foo bar</paragraph>' );
editor.execute( 'imageUpload', { file } );

sinon.assert.calledOnce( loadSpy );

const promise = loadSpy.returnValues[ 0 ];

// Check if error can be caught.
promise.catch( catchSpy );

nativeReaderMock.mockSuccess();

setTimeout( () => {
sinon.assert.calledOnce( catchSpy );
sinon.assert.calledWithExactly( catchSpy, error );
done();
}, 0 );
} );

it( 'should do nothing if image does not have uploadId', () => {
setModelData( model, '<image src="image.png"></image>' );

Expand Down

0 comments on commit 4357336

Please sign in to comment.