UNPKG

3.34 kBJavaScriptView Raw
1/**
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20/* eslint no-proto: 0 */
21
22var EOL = require('os').EOL;
23
24/**
25 * A derived exception class. See usage example in cli.js
26 * Based on:
27 * stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript/8460753#8460753
28 * @param {String} message Error message
29 * @param {Number} [code=0] Error code
30 * @param {CordovaExternalToolErrorContext} [context] External tool error context object
31 * @constructor
32 */
33function CordovaError (message, code, context) {
34 Error.captureStackTrace(this, this.constructor);
35 this.name = this.constructor.name;
36 this.message = message;
37 this.code = code || CordovaError.UNKNOWN_ERROR;
38 this.context = context;
39}
40CordovaError.prototype.__proto__ = Error.prototype;
41
42// TODO: Extend error codes according the projects specifics
43CordovaError.UNKNOWN_ERROR = 0;
44CordovaError.EXTERNAL_TOOL_ERROR = 1;
45
46/**
47 * Translates instance's error code number into error code name, e.g. 0 -> UNKNOWN_ERROR
48 * @returns {string} Error code string name
49 */
50CordovaError.prototype.getErrorCodeName = function () {
51 for (var key in CordovaError) {
52 if (CordovaError.hasOwnProperty(key)) {
53 if (CordovaError[key] === this.code) {
54 return key;
55 }
56 }
57 }
58};
59
60/**
61 * Converts CordovaError instance to string representation
62 * @param {Boolean} [isVerbose] Set up verbose mode. Used to provide more
63 * details including information about error code name and context
64 * @return {String} Stringified error representation
65 */
66CordovaError.prototype.toString = function (isVerbose) {
67 var message = '';
68 var codePrefix = '';
69
70 if (this.code !== CordovaError.UNKNOWN_ERROR) {
71 codePrefix = 'code: ' + this.code + (isVerbose ? (' (' + this.getErrorCodeName() + ')') : '') + ' ';
72 }
73
74 if (this.code === CordovaError.EXTERNAL_TOOL_ERROR) {
75 if (typeof this.context !== 'undefined') {
76 if (isVerbose) {
77 message = codePrefix + EOL + this.context.toString(isVerbose) + '\n failed with an error: ' +
78 this.message + EOL + 'Stack trace: ' + this.stack;
79 } else {
80 message = codePrefix + '\'' + this.context.toString(isVerbose) + '\' ' + this.message;
81 }
82 } else {
83 message = 'External tool failed with an error: ' + this.message;
84 }
85 } else {
86 message = isVerbose ? codePrefix + this.stack : codePrefix + this.message;
87 }
88
89 return message;
90};
91
92module.exports = CordovaError;