8
0
Просмотр исходного кода

Merge pull request #90 from ckeditor/t/ckeditor5-image/263

Other: Add catch block for failed file promise in `FileRepository`.
Maciej 7 лет назад
Родитель
Сommit
286bed6e8f

+ 6 - 0
packages/ckeditor5-upload/src/filerepository.js

@@ -188,6 +188,12 @@ export default class FileRepository extends Plugin {
 			} );
 		}
 
+		// Catch the file promise rejection. If there are no `catch` clause, the browser
+		// will throw an error (see https://github.com/ckeditor/ckeditor5-upload/pull/90).
+		loader.file.catch( () => {
+			// The error will be handled by `FileLoader` so no action is required here.
+		} );
+
 		loader.on( 'change:uploaded', () => {
 			let aggregatedUploaded = 0;
 

+ 18 - 0
packages/ckeditor5-upload/tests/filerepository.js

@@ -200,6 +200,24 @@ describe( 'FileRepository', () => {
 			expect( fileRepository.uploadTotal ).to.equal( 500 );
 			expect( fileRepository.uploadedPercent ).to.equal( 20 );
 		} );
+
+		it( 'should catch if file promise rejected (file)', () => {
+			const catchSpy = testUtils.sinon.spy( Promise.prototype, 'catch' );
+
+			fileRepository.createLoader( createNativeFileMock() );
+
+			// One call originates from `loader._createFilePromiseWrapper()` and other from `fileRepository.createLoader()`.
+			expect( catchSpy.callCount ).to.equal( 2 );
+		} );
+
+		it( 'should catch if file promise rejected (promise)', () => {
+			const catchSpy = testUtils.sinon.spy( Promise.prototype, 'catch' );
+
+			fileRepository.createLoader( new Promise( () => {} ) );
+
+			// One call originates from `loader._createFilePromiseWrapper()` and other from `fileRepository.createLoader()`.
+			expect( catchSpy.callCount ).to.equal( 2 );
+		} );
 	} );
 
 	describe( 'getLoader()', () => {