From c7fecd037cf86290b2aa6eaa38f7db1d4d182bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kup=C5=9B?= Date: Thu, 15 Mar 2018 16:07:06 +0100 Subject: [PATCH] Throwing unhandled errors in ImageUploadEditing. --- src/imageupload/imageuploadediting.js | 13 +++++++--- tests/imageupload/imageuploadediting.js | 33 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/imageupload/imageuploadediting.js b/src/imageupload/imageuploadediting.js index 487a94d3..ccf2e414 100644 --- a/src/imageupload/imageuploadediting.js +++ b/src/imageupload/imageuploadediting.js @@ -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; @@ -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 ); @@ -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' } ); diff --git a/tests/imageupload/imageuploadediting.js b/tests/imageupload/imageuploadediting.js index 9243446a..db5f93e4 100644 --- a/tests/imageupload/imageuploadediting.js +++ b/tests/imageupload/imageuploadediting.js @@ -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, '{}foo bar' ); + 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, '' );