Sfoglia il codice sorgente

Merge branch 'master' into t/ckeditor5/924

Kamil Piechaczek 6 anni fa
parent
commit
13361c2cba

+ 16 - 2
packages/ckeditor-cloud-services-core/src/token/token.js

@@ -7,6 +7,7 @@
 
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 const DEFAULT_OPTIONS = { refreshInterval: 3600000, autoRefresh: true };
 
@@ -30,7 +31,15 @@ class Token {
 	 */
 	constructor( tokenUrlOrRefreshToken, options = DEFAULT_OPTIONS ) {
 		if ( !tokenUrlOrRefreshToken ) {
-			throw new Error( 'A `tokenUrl` must be provided as the first constructor argument.' );
+			/**
+			 * A `tokenUrl` must be provided as the first constructor argument.
+			 *
+			 * @error token-missing-token-url
+			 */
+			throw new CKEditorError(
+				'token-missing-token-url: A `tokenUrl` must be provided as the first constructor argument.',
+				this
+			);
 		}
 
 		/**
@@ -167,7 +176,12 @@ function defaultRefreshToken( tokenUrl ) {
 			const xhrResponse = xhr.response;
 
 			if ( statusCode < 200 || statusCode > 299 ) {
-				return reject( new Error( 'Cannot download new token!' ) );
+				/**
+				 * Cannot download new token from the provided url.
+				 *
+				 * @error token-cannot-download-new-token
+				 */
+				return reject( new CKEditorError( 'token-cannot-download-new-token: Cannot download new token from the provided url.', null ) );
 			}
 
 			return resolve( xhrResponse );

+ 36 - 8
packages/ckeditor-cloud-services-core/src/uploadgateway/fileuploader.js

@@ -7,13 +7,14 @@
 
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 const BASE64_HEADER_REG_EXP = /^data:(\S*?);base64,/;
 
 /**
  * FileUploader class used to upload single file.
  */
-class FileUploader {
+export default class FileUploader {
 	/**
 	 * Creates `FileUploader` instance.
 	 *
@@ -23,15 +24,30 @@ class FileUploader {
 	 */
 	constructor( fileOrData, token, apiAddress ) {
 		if ( !fileOrData ) {
-			throw new Error( 'File must be provided' );
+			/**
+			 * File must be provided as the first argument.
+			 *
+			 * @error fileuploader-missing-file
+			 */
+			throw new CKEditorError( 'fileuploader-missing-file: File must be provided as the first argument', null );
 		}
 
 		if ( !token ) {
-			throw new Error( 'Token must be provided' );
+			/**
+			 * Token must be provided as the second argument.
+			 *
+			 * @error fileuploader-missing-token
+			 */
+			throw new CKEditorError( 'fileuploader-missing-token: Token must be provided as the second argument.', null );
 		}
 
 		if ( !apiAddress ) {
-			throw new Error( 'Api address must be provided' );
+			/**
+			 * Api address must be provided as the third argument.
+			 *
+			 * @error fileuploader-missing-api-address
+			 */
+			throw new CKEditorError( 'fileuploader-missing-api-address: Api address must be provided as the third argument.', null );
 		}
 
 		/**
@@ -175,7 +191,16 @@ class FileUploader {
 
 				if ( statusCode < 200 || statusCode > 299 ) {
 					if ( xhrResponse.message ) {
-						return reject( new Error( xhrResponse.message ) );
+						/**
+						 * Uploading file failed.
+						 *
+						 * @error fileuploader-uploading-data-failed
+						 */
+						return reject( new CKEditorError(
+							'fileuploader-uploading-data-failed: Uploading file failed.',
+							this,
+							{ message: xhrResponse.message }
+						) );
 					}
 
 					return reject( xhrResponse.error );
@@ -236,7 +261,12 @@ function _base64ToBlob( base64, sliceSize = 512 ) {
 
 		return new Blob( byteArrays, { type: contentType } );
 	} catch ( error ) {
-		throw new Error( 'Problem with decoding Base64 image data.' );
+		/**
+		 * Problem with decoding Base64 image data.
+		 *
+		 * @error fileuploader-decoding-image-data-error
+		 */
+		throw new CKEditorError( 'fileuploader-decoding-image-data-error: Problem with decoding Base64 image data.', null );
 	}
 }
 
@@ -255,5 +285,3 @@ function _isBase64( string ) {
 	const match = string.match( BASE64_HEADER_REG_EXP );
 	return !!( match && match.length );
 }
-
-export default FileUploader;

+ 13 - 2
packages/ckeditor-cloud-services-core/src/uploadgateway/uploadgateway.js

@@ -4,6 +4,7 @@
  */
 
 import FileUploader from './fileuploader';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 /**
  * UploadGateway abstracts file uploads to CKEditor Cloud Services.
@@ -17,11 +18,21 @@ export default class UploadGateway {
 	 */
 	constructor( token, apiAddress ) {
 		if ( !token ) {
-			throw new Error( 'Token must be provided' );
+			/**
+			 * Token must be provided.
+			 *
+			 * @error uploadgateway-missing-token
+			 */
+			throw new CKEditorError( 'uploadgateway-missing-token: Token must be provided.', null );
 		}
 
 		if ( !apiAddress ) {
-			throw new Error( 'Api address must be provided' );
+			/**
+			 * Api address must be provided.
+			 *
+			 * @error uploadgateway-missing-api-address
+			 */
+			throw new CKEditorError( 'uploadgateway-missing-api-address: Api address must be provided.', null );
 		}
 
 		/**

+ 5 - 2
packages/ckeditor-cloud-services-core/tests/token/token.js

@@ -6,6 +6,7 @@
 /* eslint-env commonjs, browser */
 
 import Token from '../../src/token/token';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 describe( 'Token', () => {
 	let requests;
@@ -25,7 +26,8 @@ describe( 'Token', () => {
 	describe( 'constructor()', () => {
 		it( 'should throw error when no tokenUrl provided', () => {
 			expect( () => new Token() ).to.throw(
-				'A `tokenUrl` must be provided as the first constructor argument.'
+				CKEditorError,
+				'token-missing-token-url'
 			);
 		} );
 
@@ -157,7 +159,8 @@ describe( 'Token', () => {
 			return promise.then( () => {
 				throw new Error( 'Promise should be rejected' );
 			}, error => {
-				expect( error ).to.match( /Cannot download new token!/ );
+				expect( error.constructor ).to.equal( CKEditorError );
+				expect( error ).to.match( /token-cannot-download-new-token/ );
 			} );
 		} );
 

+ 5 - 4
packages/ckeditor-cloud-services-core/tests/uploadgateway/fileuploader.js

@@ -7,6 +7,7 @@
 
 import FileUploader from '../../src/uploadgateway/fileuploader';
 import Token from '../../src/token/token';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 const API_ADDRESS = 'https://example.dev';
 const BASE_64_FILE = 'data:image/gif;base64,R0lGODlhCQAJAPIAAGFhYZXK/1FRUf///' +
@@ -23,20 +24,20 @@ describe( 'FileUploader', () => {
 
 	describe( 'constructor()', () => {
 		it( 'should throw error when no fileOrData provided', () => {
-			expect( () => new FileUploader() ).to.throw( 'File must be provided' );
+			expect( () => new FileUploader() ).to.throw( CKEditorError, 'fileuploader-missing-file' );
 		} );
 
 		it( 'should throw error when no token provided', () => {
-			expect( () => new FileUploader( 'file' ) ).to.throw( 'Token must be provided' );
+			expect( () => new FileUploader( 'file' ) ).to.throw( CKEditorError, 'fileuploader-missing-token' );
 		} );
 
 		it( 'should throw error when no api address provided', () => {
-			expect( () => new FileUploader( 'file', token ) ).to.throw( 'Api address must be provided' );
+			expect( () => new FileUploader( 'file', token ) ).to.throw( CKEditorError, 'fileuploader-missing-api-address' );
 		} );
 
 		it( 'should throw error when wrong Base64 file is provided', () => {
 			expect( () => new FileUploader( 'data:image/gif;base64,R', token, API_ADDRESS ) )
-				.to.throw( 'Problem with decoding Base64 image data.' );
+				.to.throw( CKEditorError, 'fileuploader-decoding-image-data-error' );
 		} );
 
 		it( 'should convert base64 to file', done => {

+ 3 - 2
packages/ckeditor-cloud-services-core/tests/uploadgateway/uploadgateway.js

@@ -6,17 +6,18 @@
 import FileUploader from '../../src/uploadgateway/fileuploader';
 import UploadGateway from '../../src/uploadgateway/uploadgateway';
 import Token from '../../src/token/token';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 describe( 'UploadGateway', () => {
 	const token = new Token( 'url', { initValue: 'token', autoRefresh: false } );
 
 	describe( 'constructor()', () => {
 		it( 'should throw error when no token provided', () => {
-			expect( () => new UploadGateway( undefined, 'test' ) ).to.throw( 'Token must be provided' );
+			expectToThrowCKEditorError( () => new UploadGateway( undefined, 'test' ), 'uploadgateway-missing-token' );
 		} );
 
 		it( 'should throw error when no apiAddress provided', () => {
-			expect( () => new UploadGateway( token ) ).to.throw( 'Api address must be provided' );
+			expectToThrowCKEditorError( () => new UploadGateway( token ), 'uploadgateway-missing-api-address' );
 		} );
 	} );