You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2019/10/10 14:57:48 UTC
[cordova-common] branch master updated: Convert CordovaError to ES6
class (#87)
This is an automated email from the ASF dual-hosted git repository.
raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-common.git
The following commit(s) were added to refs/heads/master by this push:
new cfa7b3e Convert CordovaError to ES6 class (#87)
cfa7b3e is described below
commit cfa7b3e5f809bc046d109f0b1f98666058e46cd1
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Thu Oct 10 16:57:43 2019 +0200
Convert CordovaError to ES6 class (#87)
---
src/CordovaError/CordovaError.js | 92 ++++++++++++++++++++--------------------
1 file changed, 45 insertions(+), 47 deletions(-)
diff --git a/src/CordovaError/CordovaError.js b/src/CordovaError/CordovaError.js
index 34230a7..c7ad82e 100644
--- a/src/CordovaError/CordovaError.js
+++ b/src/CordovaError/CordovaError.js
@@ -28,64 +28,62 @@ var EOL = require('os').EOL;
* @param {CordovaExternalToolErrorContext} [context] External tool error context object
* @constructor
*/
-function CordovaError (message, code, context) {
- Error.captureStackTrace(this, this.constructor);
- this.name = this.constructor.name;
- this.message = message;
- this.code = code || CordovaError.UNKNOWN_ERROR;
- this.context = context;
-}
-
-// FIXME __proto__ property has been deprecated as of ECMAScript 3.1
-// eslint-disable-next-line no-proto
-CordovaError.prototype.__proto__ = Error.prototype;
-
-// TODO: Extend error codes according the projects specifics
-CordovaError.UNKNOWN_ERROR = 0;
-CordovaError.EXTERNAL_TOOL_ERROR = 1;
+class CordovaError extends Error {
+ constructor (message, code, context) {
+ super(message);
+ Error.captureStackTrace(this, this.constructor);
+ this.name = this.constructor.name;
+ this.code = code || CordovaError.UNKNOWN_ERROR;
+ this.context = context;
+ }
-/**
- * Translates instance's error code number into error code name, e.g. 0 -> UNKNOWN_ERROR
- * @returns {string} Error code string name
- */
-CordovaError.prototype.getErrorCodeName = function () {
- for (const key of Object.keys(CordovaError)) {
- if (CordovaError[key] === this.code) {
- return key;
+ /**
+ * Translates instance's error code number into error code name, e.g. 0 -> UNKNOWN_ERROR
+ * @returns {string} Error code string name
+ */
+ getErrorCodeName () {
+ for (const key of Object.keys(CordovaError)) {
+ if (CordovaError[key] === this.code) {
+ return key;
+ }
}
}
-};
-/**
- * Converts CordovaError instance to string representation
- * @param {Boolean} [isVerbose] Set up verbose mode. Used to provide more
- * details including information about error code name and context
- * @return {String} Stringified error representation
- */
-CordovaError.prototype.toString = function (isVerbose) {
- var message = '';
- var codePrefix = '';
+ /**
+ * Converts CordovaError instance to string representation
+ * @param {Boolean} [isVerbose] Set up verbose mode. Used to provide more
+ * details including information about error code name and context
+ * @return {String} Stringified error representation
+ */
+ toString (isVerbose) {
+ var message = '';
+ var codePrefix = '';
- if (this.code !== CordovaError.UNKNOWN_ERROR) {
- codePrefix = 'code: ' + this.code + (isVerbose ? (' (' + this.getErrorCodeName() + ')') : '') + ' ';
- }
+ if (this.code !== CordovaError.UNKNOWN_ERROR) {
+ codePrefix = 'code: ' + this.code + (isVerbose ? (' (' + this.getErrorCodeName() + ')') : '') + ' ';
+ }
- if (this.code === CordovaError.EXTERNAL_TOOL_ERROR) {
- if (typeof this.context !== 'undefined') {
- if (isVerbose) {
- message = codePrefix + EOL + this.context.toString(isVerbose) + '\n failed with an error: ' +
- this.message + EOL + 'Stack trace: ' + this.stack;
+ if (this.code === CordovaError.EXTERNAL_TOOL_ERROR) {
+ if (typeof this.context !== 'undefined') {
+ if (isVerbose) {
+ message = codePrefix + EOL + this.context.toString(isVerbose) + '\n failed with an error: ' +
+ this.message + EOL + 'Stack trace: ' + this.stack;
+ } else {
+ message = codePrefix + '\'' + this.context.toString(isVerbose) + '\' ' + this.message;
+ }
} else {
- message = codePrefix + '\'' + this.context.toString(isVerbose) + '\' ' + this.message;
+ message = 'External tool failed with an error: ' + this.message;
}
} else {
- message = 'External tool failed with an error: ' + this.message;
+ message = isVerbose ? codePrefix + this.stack : codePrefix + this.message;
}
- } else {
- message = isVerbose ? codePrefix + this.stack : codePrefix + this.message;
+
+ return message;
}
+}
- return message;
-};
+// TODO: Extend error codes according the projects specifics
+CordovaError.UNKNOWN_ERROR = 0;
+CordovaError.EXTERNAL_TOOL_ERROR = 1;
module.exports = CordovaError;
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org