2 * tst.werror.js: tests basic functionality of the WError class.
5 var mod_assert = require('assert');
6 var mod_verror = require('../lib/verror');
8 var VError = mod_verror.VError;
9 var WError = mod_verror.WError;
11 var err, suberr, stack, substack;
14 * Remove full paths and relative line numbers from stack traces so that we can
15 * compare against "known-good" output.
17 function cleanStack(stacktxt)
19 var re = new RegExp(__filename + ':\\d+:\\d+', 'gm');
20 stacktxt = stacktxt.replace(re, 'tst.werror.js');
25 * Save the generic parts of all stack traces so we can avoid hardcoding
26 * Node-specific implementation details in our testing of stack traces.
28 var nodestack = new Error().stack.split('\n').slice(2).join('\n');
32 mod_assert.equal(err.name, 'WError');
33 mod_assert.ok(err instanceof Error);
34 mod_assert.ok(err instanceof WError);
35 mod_assert.equal(err.message, '');
36 mod_assert.equal(err.toString(), 'WError');
37 mod_assert.ok(err.cause() === undefined);
38 stack = cleanStack(err.stack);
39 mod_assert.equal(stack, [
41 ' at Object.<anonymous> (tst.werror.js)'
42 ].join('\n') + '\n' + nodestack);
44 /* options-argument form */
46 mod_assert.equal(err.message, '');
47 mod_assert.equal(err.toString(), 'WError');
48 mod_assert.ok(err.cause() === undefined);
51 err = new WError('my error');
52 mod_assert.equal(err.message, 'my error');
53 mod_assert.equal(err.toString(), 'WError: my error');
54 mod_assert.ok(err.cause() === undefined);
55 stack = cleanStack(err.stack);
56 mod_assert.equal(stack, [
58 ' at Object.<anonymous> (tst.werror.js)'
59 ].join('\n') + '\n' + nodestack);
61 err = new WError({}, 'my error');
62 mod_assert.equal(err.message, 'my error');
63 mod_assert.equal(err.toString(), 'WError: my error');
64 mod_assert.ok(err.cause() === undefined);
66 /* printf-style message */
67 err = new WError('%s error: %3d problems', 'very bad', 15);
68 mod_assert.equal(err.message, 'very bad error: 15 problems');
69 mod_assert.equal(err.toString(), 'WError: very bad error: 15 problems');
70 mod_assert.ok(err.cause() === undefined);
72 err = new WError({}, '%s error: %3d problems', 'very bad', 15);
73 mod_assert.equal(err.message, 'very bad error: 15 problems');
74 mod_assert.equal(err.toString(), 'WError: very bad error: 15 problems');
75 mod_assert.ok(err.cause() === undefined);
77 /* caused by another error, with no additional message */
78 suberr = new Error('root cause');
79 err = new WError(suberr);
80 mod_assert.equal(err.message, '');
81 mod_assert.equal(err.toString(), 'WError; caused by Error: root cause');
82 mod_assert.ok(err.cause() === suberr);
84 err = new WError({ 'cause': suberr });
85 mod_assert.equal(err.message, '');
86 mod_assert.equal(err.toString(), 'WError; caused by Error: root cause');
87 mod_assert.ok(err.cause() === suberr);
89 /* caused by another error, with annotation */
90 err = new WError(suberr, 'proximate cause: %d issues', 3);
91 mod_assert.equal(err.message, 'proximate cause: 3 issues');
92 mod_assert.equal(err.toString(), 'WError: proximate cause: 3 issues; ' +
93 'caused by Error: root cause');
94 mod_assert.ok(err.cause() === suberr);
95 stack = cleanStack(err.stack);
96 mod_assert.equal(stack, [
97 'WError: proximate cause: 3 issues; caused by Error: root cause',
98 ' at Object.<anonymous> (tst.werror.js)'
99 ].join('\n') + '\n' + nodestack);
101 err = new WError({ 'cause': suberr }, 'proximate cause: %d issues', 3);
102 mod_assert.equal(err.message, 'proximate cause: 3 issues');
103 mod_assert.equal(err.toString(), 'WError: proximate cause: 3 issues; ' +
104 'caused by Error: root cause');
105 mod_assert.ok(err.cause() === suberr);
106 stack = cleanStack(err.stack);
107 mod_assert.equal(stack, [
108 'WError: proximate cause: 3 issues; caused by Error: root cause',
109 ' at Object.<anonymous> (tst.werror.js)'
110 ].join('\n') + '\n' + nodestack);
112 /* caused by another WError, with annotation. */
114 err = new WError(suberr, 'top');
115 mod_assert.equal(err.message, 'top');
116 mod_assert.equal(err.toString(), 'WError: top; caused by WError: ' +
117 'proximate cause: 3 issues; caused by Error: root cause');
118 mod_assert.ok(err.cause() === suberr);
120 err = new WError({ 'cause': suberr }, 'top');
121 mod_assert.equal(err.message, 'top');
122 mod_assert.equal(err.toString(), 'WError: top; caused by WError: ' +
123 'proximate cause: 3 issues; caused by Error: root cause');
124 mod_assert.ok(err.cause() === suberr);
126 /* caused by a VError */
127 suberr = new VError(new Error('root cause'), 'mid');
128 err = new WError(suberr, 'top');
129 mod_assert.equal(err.message, 'top');
130 mod_assert.equal(err.toString(),
131 'WError: top; caused by VError: mid: root cause');
132 mod_assert.ok(err.cause() === suberr);
134 /* null cause (for backwards compatibility with older versions) */
135 err = new WError(null, 'my error');
136 mod_assert.equal(err.message, 'my error');
137 mod_assert.equal(err.toString(), 'WError: my error');
138 mod_assert.ok(err.cause() === undefined);
139 stack = cleanStack(err.stack);
140 mod_assert.equal(stack, [
142 ' at Object.<anonymous> (tst.werror.js)'
143 ].join('\n') + '\n' + nodestack);
145 err = new WError({ 'cause': null }, 'my error');
146 mod_assert.equal(err.message, 'my error');
147 mod_assert.equal(err.toString(), 'WError: my error');
148 mod_assert.ok(err.cause() === undefined);
150 err = new WError(null);
151 mod_assert.equal(err.message, '');
152 mod_assert.equal(err.toString(), 'WError');
153 mod_assert.ok(err.cause() === undefined);
154 stack = cleanStack(err.stack);
155 mod_assert.equal(stack, [
157 ' at Object.<anonymous> (tst.werror.js)'
158 ].join('\n') + '\n' + nodestack);
161 function makeErr(options) {
162 return (new WError(options, 'test error'));
165 mod_assert.equal(err.toString(), 'WError: test error');
166 stack = cleanStack(err.stack);
167 mod_assert.equal(stack, [
168 'WError: test error',
169 ' at makeErr (tst.werror.js)',
170 ' at Object.<anonymous> (tst.werror.js)'
171 ].join('\n') + '\n' + nodestack);
173 err = makeErr({ 'constructorOpt': makeErr });
174 mod_assert.equal(err.toString(), 'WError: test error');
175 stack = cleanStack(err.stack);
176 mod_assert.equal(stack, [
177 'WError: test error',
178 ' at Object.<anonymous> (tst.werror.js)'
179 ].join('\n') + '\n' + nodestack);