浏览代码

Fix unhandled promise rejections errors.

Maciej Gołaszewski 6 年之前
父节点
当前提交
cf9318065f

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

@@ -188,15 +188,19 @@ export default class FileRepository extends Plugin {
 		if ( fileOrPromise instanceof Promise ) {
 			loader.file.then( file => {
 				this._loadersMap.set( file, loader );
+			} ).catch( () => {
+				// 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).
+				// The error will be handled by `FileLoader` so no action is required here.
+			} );
+		} else {
+			// 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.
 			} );
 		}
 
-		// 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;
 
@@ -441,6 +445,11 @@ class FileLoader {
 		return this._filePromiseWrapper.promise
 			.then( file => this._reader.read( file ) )
 			.then( data => {
+				// Edge case: reader was aborted after file was read - double check for proper status.
+				if ( this.status !== 'reading' ) {
+					throw this.status;
+				}
+
 				this.status = 'idle';
 
 				return data;

+ 2 - 1
packages/ckeditor5-upload/tests/adapters/base64uploadadapter.js

@@ -140,7 +140,8 @@ describe( 'Base64UploadAdapter', () => {
 				const adapter = fileRepository.createLoader( createNativeFileMock() );
 
 				expect( () => {
-					adapter.upload();
+					// Catch the upload error to prevent uncaught promise errors
+					adapter.upload().catch( () => {} );
 					adapter.abort();
 				} ).to.not.throw();