Skip to content

Commit 48fee1d

Browse files
committed
feat(CordovaError): support for error cause & more
This commit bases CordovaError on the popular joyent/node-verror. We actually use @netflix/nerror, a VError fork for now. That's because we do not want printf style error formatting support and that forks allows to disable it. There's [ongoing work][1] to integrate that change into the original VError. So basically CordovaError behaves like PError but with all the static methods from VError and different parameter ordering for its constructor. One change that could break some existing tests in repositories that use cordova-common is that `toString` (for errors without a cause argument) now behaves like the Error default again: new CordovaError('foo').toString(); // old result: 'foo' // new result: 'CordovaError: foo' [1]: TritonDataCenter/node-verror#63 (comment)
1 parent 641be1e commit 48fee1d

3 files changed

Lines changed: 45 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"cover": "nyc npm run test:unit"
2424
},
2525
"dependencies": {
26+
"@netflix/nerror": "^1.1.3",
2627
"ansi": "^0.3.1",
2728
"bplist-parser": "^0.2.0",
2829
"cross-spawn": "^6.0.5",

spec/CordovaError/CordovaError.spec.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
under the License.
1818
*/
1919

20-
var CordovaError = require('../../src/CordovaError/CordovaError');
20+
const endent = require('endent');
21+
const CordovaError = require('../../src/CordovaError/CordovaError');
2122

2223
describe('CordovaError class', function () {
2324
it('Test 001 : should be constructable', function () {
@@ -26,6 +27,33 @@ describe('CordovaError class', function () {
2627

2728
it('Test 003 : toString works', function () {
2829
var error003_1 = new CordovaError('error');
29-
expect(error003_1.toString()).toEqual('error');
30+
expect(error003_1.toString()).toEqual('CordovaError: error');
31+
});
32+
33+
describe('given a cause', () => {
34+
let error, cause;
35+
36+
beforeEach(() => {
37+
cause = new Error('cause');
38+
error = new CordovaError('error', cause);
39+
});
40+
41+
it('should save it', () => {
42+
expect(CordovaError.cause(error)).toBe(cause);
43+
});
44+
45+
it('should include the cause in toString result', () => {
46+
expect(error.toString()).toEqual(`CordovaError: error: cause`);
47+
});
48+
49+
it('should include the cause stack in valueOf result', () => {
50+
cause.stack = 'CAUSE_STACK';
51+
error.stack = 'ERROR_STACK';
52+
53+
expect(CordovaError.fullStack(error)).toEqual(endent`
54+
ERROR_STACK
55+
caused by: CAUSE_STACK
56+
`);
57+
});
3058
});
3159
});

src/CordovaError/CordovaError.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,28 @@
1717
under the License.
1818
*/
1919

20+
const VError = require('@netflix/nerror');
21+
2022
/**
21-
* A derived exception class
22-
*
23-
* Based on: https://stackoverflow.com/a/8460753/380229
23+
* A custom exception class derived from VError
2424
*/
25-
class CordovaError extends Error {
25+
class CordovaError extends VError {
2626
/**
2727
* Creates new CordovaError with given error message
2828
*
2929
* @param {String} message Error message
30+
* @param {Error|VErrorOptions} causeOrOpts
31+
* The Error that caused this to be thrown or a VErrorOptions object
3032
*/
31-
constructor (message) {
32-
super(message);
33-
Error.captureStackTrace(this, this.constructor);
34-
this.name = this.constructor.name;
35-
}
33+
constructor (message, causeOrOpts = {}) {
34+
const defaults = { name: 'CordovaError' };
35+
const overrides = { strict: false, skipPrintf: true };
36+
const userOpts = causeOrOpts instanceof Error
37+
? { cause: causeOrOpts }
38+
: causeOrOpts;
39+
const opts = Object.assign(defaults, userOpts, overrides);
3640

37-
/**
38-
* Converts this to its string representation
39-
*
40-
* @return {String} Stringified error representation
41-
*/
42-
toString () {
43-
return this.message;
41+
super(opts, message);
4442
}
4543
}
4644

0 commit comments

Comments
 (0)