Selaa lähdekoodia

Added usage of CKEdtiorError class, added simple error descriptions.

Maciej Bukowski 6 vuotta sitten
vanhempi
commit
dc22bada67

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

@@ -9,6 +9,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 };
 
@@ -32,7 +33,12 @@ 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.' );
 		}
 
 		/**
@@ -169,7 +175,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.' ) );
 			}
 
 			return resolve( xhrResponse );

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

@@ -15,7 +15,7 @@ const BASE64_HEADER_REG_EXP = /^data:(\S*?);base64,/;
 /**
  * FileUploader class used to upload single file.
  */
-class FileUploader {
+export default class FileUploader {
 	/**
 	 * Creates `FileUploader` instance.
 	 *
@@ -25,15 +25,25 @@ class FileUploader {
 	 */
 	constructor( fileOrData, token, apiAddress ) {
 		if ( !fileOrData ) {
-			throw new Error( 'File must be provided' );
+			throw new CKEditorError( 'File must be provided' );
 		}
 
 		if ( !token ) {
-			throw new Error( 'Token must be provided' );
+			/**
+			 * Token must be provided.
+			 *
+			 * @error fileuploader-missing-token
+			 */
+			throw new CKEditorError( 'fileuploader-missing-token: Token must be provided.' );
 		}
 
 		if ( !apiAddress ) {
-			throw new Error( 'Api address must be provided' );
+			/**
+			 * Api address must be provided.
+			 *
+			 * @error fileuploader-missing-api-address
+			 */
+			throw new CKEditorError( 'fileuploader-missing-api-address: Api address must be provided.' );
 		}
 
 		/**
@@ -177,7 +187,12 @@ 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.', { message: xhrResponse.message } ) );
 					}
 
 					return reject( xhrResponse.error );
@@ -238,7 +253,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.' );
 	}
 }
 
@@ -257,5 +277,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

@@ -6,6 +6,7 @@
 'use strict';
 
 import FileUploader from './fileuploader';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 /**
  * UploadGateway abstracts file uploads to CKEditor Cloud Services.
@@ -19,11 +20,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.' );
 		}
 
 		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.' );
 		}
 
 		/**