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

Commit a2de5d5

Browse files
authored
Merge pull request #90 from ckeditor/t/ckeditor5-image/263
Other: Add catch block for failed file promise in `FileRepository`.
2 parents f6e27cb + 83b78bd commit a2de5d5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/filerepository.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ export default class FileRepository extends Plugin {
188188
} );
189189
}
190190

191+
// Catch the file promise rejection. If there are no `catch` clause, the browser
192+
// will throw an error (see https://github.com/ckeditor/ckeditor5-upload/pull/90).
193+
loader.file.catch( () => {
194+
// The error will be handled by `FileLoader` so no action is required here.
195+
} );
196+
191197
loader.on( 'change:uploaded', () => {
192198
let aggregatedUploaded = 0;
193199

tests/filerepository.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,24 @@ describe( 'FileRepository', () => {
200200
expect( fileRepository.uploadTotal ).to.equal( 500 );
201201
expect( fileRepository.uploadedPercent ).to.equal( 20 );
202202
} );
203+
204+
it( 'should catch if file promise rejected (file)', () => {
205+
const catchSpy = testUtils.sinon.spy( Promise.prototype, 'catch' );
206+
207+
fileRepository.createLoader( createNativeFileMock() );
208+
209+
// One call originates from `loader._createFilePromiseWrapper()` and other from `fileRepository.createLoader()`.
210+
expect( catchSpy.callCount ).to.equal( 2 );
211+
} );
212+
213+
it( 'should catch if file promise rejected (promise)', () => {
214+
const catchSpy = testUtils.sinon.spy( Promise.prototype, 'catch' );
215+
216+
fileRepository.createLoader( new Promise( () => {} ) );
217+
218+
// One call originates from `loader._createFilePromiseWrapper()` and other from `fileRepository.createLoader()`.
219+
expect( catchSpy.callCount ).to.equal( 2 );
220+
} );
203221
} );
204222

205223
describe( 'getLoader()', () => {

0 commit comments

Comments
 (0)