@util.js/errors 0.41.5 → 0.41.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -1
- package/doc/README-examples.js +0 -71
- package/lib/AsyncError.js +0 -38
- package/lib/Errors.js +0 -148
- package/lib/FalsyError.js +0 -27
- package/lib/MissingError.js +0 -27
- package/lib/RethrownError.js +0 -30
- package/test/ErrorsTest.js +0 -191
- package/test/Errors_FalsyError_Test.js +0 -84
- package/test/Errors_FalsyError_check_Test.js +0 -74
- package/test/Errors_MissingError_Test.js +0 -158
- package/test/Errors_MissingError_check_Test.js +0 -102
package/package.json
CHANGED
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
"@babel/runtime-corejs3": "7.29.7"
|
|
5
5
|
},
|
|
6
6
|
"description": "JavaScript utility methods for errors",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"index.js",
|
|
10
|
+
"RethrownError.js"
|
|
11
|
+
],
|
|
7
12
|
"keywords": [
|
|
8
13
|
"error",
|
|
9
14
|
"errors",
|
|
@@ -25,5 +30,5 @@
|
|
|
25
30
|
"type": "git",
|
|
26
31
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-errors"
|
|
27
32
|
},
|
|
28
|
-
"version": "0.41.
|
|
33
|
+
"version": "0.41.7"
|
|
29
34
|
}
|
package/doc/README-examples.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
if (process.argv.length < 3) {
|
|
4
|
-
for (let i = 0; i < 1; i++) runExample(i);
|
|
5
|
-
} else {
|
|
6
|
-
runExample(parseInt(process.argv[2]));
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function runExample(i) {
|
|
10
|
-
switch (i) {
|
|
11
|
-
case 0:
|
|
12
|
-
runExample0();
|
|
13
|
-
break;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function runExample0() {
|
|
18
|
-
const errors = require("..");
|
|
19
|
-
function rejectAPromise() {
|
|
20
|
-
return Promise.reject(new TypeError("Fail!"));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
errors
|
|
24
|
-
.catch(rejectAPromise())
|
|
25
|
-
.catch((error) =>
|
|
26
|
-
console.log("\n\nWith Caller Stack Trace\n" + error.stack),
|
|
27
|
-
);
|
|
28
|
-
// With Caller Stack Trace
|
|
29
|
-
// AsyncError: Fail!
|
|
30
|
-
// at promise.catch.error (/root/utiljs/packages/utiljs-errors/lib/Errors.js:28:13)
|
|
31
|
-
// at process._tickCallback (internal/process/next_tick.js:68:7)
|
|
32
|
-
// at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
|
|
33
|
-
// at startup (internal/bootstrap/node.js:240:19)
|
|
34
|
-
// at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)
|
|
35
|
-
// at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:23:11)
|
|
36
|
-
// at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
|
|
37
|
-
// at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
|
|
38
|
-
// at Module._compile (internal/modules/cjs/loader.js:702:30)
|
|
39
|
-
// at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
|
|
40
|
-
// at Module.load (internal/modules/cjs/loader.js:612:32)
|
|
41
|
-
// at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
|
|
42
|
-
// at Function.Module._load (internal/modules/cjs/loader.js:543:3)
|
|
43
|
-
// at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
|
|
44
|
-
// TypeError: Fail!
|
|
45
|
-
// at rejectAPromise (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:20:27)
|
|
46
|
-
// at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:23:12)
|
|
47
|
-
// at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
|
|
48
|
-
// at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
|
|
49
|
-
// at Module._compile (internal/modules/cjs/loader.js:702:30)
|
|
50
|
-
// at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
|
|
51
|
-
// at Module.load (internal/modules/cjs/loader.js:612:32)
|
|
52
|
-
// at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
|
|
53
|
-
// at Function.Module._load (internal/modules/cjs/loader.js:543:3)
|
|
54
|
-
// at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
|
|
55
|
-
|
|
56
|
-
rejectAPromise().catch((error) =>
|
|
57
|
-
console.log("\n\nWithout Caller Stack Trace\n" + error.stack),
|
|
58
|
-
);
|
|
59
|
-
// Without Caller Stack Trace
|
|
60
|
-
// TypeError: Fail!
|
|
61
|
-
// at rejectAPromise (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:20:27)
|
|
62
|
-
// at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:53:3)
|
|
63
|
-
// at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
|
|
64
|
-
// at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
|
|
65
|
-
// at Module._compile (internal/modules/cjs/loader.js:702:30)
|
|
66
|
-
// at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
|
|
67
|
-
// at Module.load (internal/modules/cjs/loader.js:612:32)
|
|
68
|
-
// at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
|
|
69
|
-
// at Function.Module._load (internal/modules/cjs/loader.js:543:3)
|
|
70
|
-
// at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
|
|
71
|
-
}
|
package/lib/AsyncError.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* See {@link Errors#AsyncError}.
|
|
5
|
-
*/
|
|
6
|
-
module.exports = class AsyncError extends Error {
|
|
7
|
-
constructor(callerError, asyncError, message) {
|
|
8
|
-
super(message != null ? message : getErrorProperty(asyncError, "message"));
|
|
9
|
-
if (!callerError)
|
|
10
|
-
throw new TypeError(
|
|
11
|
-
`${this.constructor.name}'s constructor expects a defined callerError, but it was ${callerError}.`,
|
|
12
|
-
);
|
|
13
|
-
if (!asyncError)
|
|
14
|
-
throw new TypeError(
|
|
15
|
-
`${this.constructor.name}'s constructor expects a defined asyncError, but it was ${asyncError}.`,
|
|
16
|
-
);
|
|
17
|
-
this.name = this.constructor.name;
|
|
18
|
-
this.original = asyncError;
|
|
19
|
-
this.newStack = this.stack;
|
|
20
|
-
const callerStack =
|
|
21
|
-
" at " + callerError.stack.split(" at ").splice(2).join(" at ");
|
|
22
|
-
// If asyncError.stack is falsy, then let us just try asyncError;
|
|
23
|
-
// asyncError may be a string.
|
|
24
|
-
const asyncErrorStack = getErrorProperty(asyncError, "stack");
|
|
25
|
-
this.stack = `${this.stack}\n${callerStack}\n${asyncErrorStack}`;
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
function getErrorProperty(error, property) {
|
|
30
|
-
if (error[property]) return error[property];
|
|
31
|
-
try {
|
|
32
|
-
return JSON.stringify(error);
|
|
33
|
-
} catch (e) {
|
|
34
|
-
// JSON.stringify might throw the following, "TypeError: Converting circular
|
|
35
|
-
// structure to JSON."
|
|
36
|
-
return error;
|
|
37
|
-
}
|
|
38
|
-
}
|
package/lib/Errors.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* JavaScript utility methods for [errors]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error}
|
|
5
|
-
*
|
|
6
|
-
* @public
|
|
7
|
-
* @class Errors
|
|
8
|
-
*/
|
|
9
|
-
module.exports = class Errors {
|
|
10
|
-
/**
|
|
11
|
-
* Wraps the given promise so that errors caught preserve the stack trace of the calling thread.
|
|
12
|
-
*
|
|
13
|
-
* The example compares the stack trace of a rejected promise using this method and the stack trace of a rejected promise that does not use this method.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* const errors = require("@util.js/errors");
|
|
17
|
-
* function rejectAPromise() {
|
|
18
|
-
* return Promise.reject(new TypeError("Fail!"));
|
|
19
|
-
* }
|
|
20
|
-
*
|
|
21
|
-
* errors
|
|
22
|
-
* .catch(rejectAPromise())
|
|
23
|
-
* .catch(error => console.log("\n\nWith Caller Stack Trace\n" + error.stack));
|
|
24
|
-
* // With Caller Stack Trace
|
|
25
|
-
* // AsyncError: Fail!
|
|
26
|
-
* // at promise.catch.error (/root/utiljs/packages/utiljs-errors/lib/Errors.js:28:13)
|
|
27
|
-
* // at process._tickCallback (internal/process/next_tick.js:68:7)
|
|
28
|
-
* // at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
|
|
29
|
-
* // at startup (internal/bootstrap/node.js:240:19)
|
|
30
|
-
* // at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)
|
|
31
|
-
* // at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:23:11)
|
|
32
|
-
* // at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
|
|
33
|
-
* // at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
|
|
34
|
-
* // at Module._compile (internal/modules/cjs/loader.js:702:30)
|
|
35
|
-
* // at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
|
|
36
|
-
* // at Module.load (internal/modules/cjs/loader.js:612:32)
|
|
37
|
-
* // at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
|
|
38
|
-
* // at Function.Module._load (internal/modules/cjs/loader.js:543:3)
|
|
39
|
-
* // at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
|
|
40
|
-
* // TypeError: Fail!
|
|
41
|
-
* // at rejectAPromise (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:20:27)
|
|
42
|
-
* // at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:23:12)
|
|
43
|
-
* // at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
|
|
44
|
-
* // at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
|
|
45
|
-
* // at Module._compile (internal/modules/cjs/loader.js:702:30)
|
|
46
|
-
* // at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
|
|
47
|
-
* // at Module.load (internal/modules/cjs/loader.js:612:32)
|
|
48
|
-
* // at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
|
|
49
|
-
* // at Function.Module._load (internal/modules/cjs/loader.js:543:3)
|
|
50
|
-
* // at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
|
|
51
|
-
*
|
|
52
|
-
* rejectAPromise().catch(error =>
|
|
53
|
-
* console.log("\n\nWithout Caller Stack Trace\n" + error.stack)
|
|
54
|
-
* );
|
|
55
|
-
* // Without Caller Stack Trace
|
|
56
|
-
* // TypeError: Fail!
|
|
57
|
-
* // at rejectAPromise (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:20:27)
|
|
58
|
-
* // at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:53:3)
|
|
59
|
-
* // at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
|
|
60
|
-
* // at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
|
|
61
|
-
* // at Module._compile (internal/modules/cjs/loader.js:702:30)
|
|
62
|
-
* // at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
|
|
63
|
-
* // at Module.load (internal/modules/cjs/loader.js:612:32)
|
|
64
|
-
* // at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
|
|
65
|
-
* // at Function.Module._load (internal/modules/cjs/loader.js:543:3)
|
|
66
|
-
* // at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
|
|
67
|
-
*
|
|
68
|
-
* @param {Promise} promise The promise to wrap
|
|
69
|
-
* @param {String} [message] A human-readable description of the error
|
|
70
|
-
* @return {Promise} A promise that upon rejection wraps the error in an AsyncError and rethrows
|
|
71
|
-
* @throws {TypeError} If the given promise is not an instance of Promise
|
|
72
|
-
*
|
|
73
|
-
* @public
|
|
74
|
-
* @function Errors#catch
|
|
75
|
-
*/
|
|
76
|
-
catch(promise, message) {
|
|
77
|
-
const callerError = new Error();
|
|
78
|
-
return promise.catch((error) => {
|
|
79
|
-
throw new this.AsyncError(callerError, error, message);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* An error that preserves the stack trace of the thread that calls an asynchronous function.
|
|
85
|
-
*
|
|
86
|
-
* Use {@link Errors#catch} to use this class. Use this class directly for instanceof checks.
|
|
87
|
-
*
|
|
88
|
-
* @public
|
|
89
|
-
* @class Errors#AsyncError
|
|
90
|
-
*/
|
|
91
|
-
get AsyncError() {
|
|
92
|
-
return require("./AsyncError");
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
get FalsyError() {
|
|
96
|
-
return require("./FalsyError");
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
get MissingError() {
|
|
100
|
-
return require("./MissingError");
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* A wrapper of another error used for rethrowing.
|
|
105
|
-
*
|
|
106
|
-
* This error preserves information about the original error and its stack trace.
|
|
107
|
-
*
|
|
108
|
-
* A [Stack Overflow]{@link https://stackoverflow.com/questions/42754270/re-throwing-exception-in-nodejs-and-not-losing-stack-trace} article inspired this class.
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* function throwATypeError() {
|
|
112
|
-
* throw new TypeError("Invalid Argument");
|
|
113
|
-
* }
|
|
114
|
-
*
|
|
115
|
-
* function rethrowTheTypeError() {
|
|
116
|
-
* try {
|
|
117
|
-
* throwATypeError();
|
|
118
|
-
* } catch (error) {
|
|
119
|
-
* throw new RethrownError(error, "Lorem Ipsum");
|
|
120
|
-
* }
|
|
121
|
-
* }
|
|
122
|
-
*
|
|
123
|
-
* try {
|
|
124
|
-
* rethrowTheTypeError();
|
|
125
|
-
* } catch (error) {
|
|
126
|
-
* console.log(error.stack);
|
|
127
|
-
* // This outputs a stack trace like the following:
|
|
128
|
-
* //
|
|
129
|
-
* // RethrownError: Lorem Ipsum
|
|
130
|
-
* // at rethrowTheTypeError (/root/utiljs/packages/utiljs-errors/test/ErrorsTest.js:16:15)
|
|
131
|
-
* // TypeError: Invalid Argument
|
|
132
|
-
* // at throwATypeError (/root/utiljs/packages/utiljs-errors/test/ErrorsTest.js:10:13)
|
|
133
|
-
* // at rethrowTheTypeError (/root/utiljs/packages/utiljs-errors/test/ErrorsTest.js:14:9)
|
|
134
|
-
* // at Context.it (/root/utiljs/packages/utiljs-errors/test/ErrorsTest.js:20:7)
|
|
135
|
-
* // ...
|
|
136
|
-
* }
|
|
137
|
-
*
|
|
138
|
-
* @param {Error} error The error to rethrow
|
|
139
|
-
* @param {String} [message] A human-readable description of the error
|
|
140
|
-
* @throws {TypeError} if the given error is not defined
|
|
141
|
-
*
|
|
142
|
-
* @public
|
|
143
|
-
* @class Errors#RethrownError
|
|
144
|
-
*/
|
|
145
|
-
get RethrownError() {
|
|
146
|
-
return require("./RethrownError");
|
|
147
|
-
}
|
|
148
|
-
};
|
package/lib/FalsyError.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
module.exports = class FalsyError extends TypeError {
|
|
4
|
-
static check(argumentValue, argumentName, functionName) {
|
|
5
|
-
if (argumentValue) return;
|
|
6
|
-
throw new FalsyError(argumentValue, argumentName, functionName);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
constructor(argumentValue, argumentName, functionName) {
|
|
10
|
-
super(
|
|
11
|
-
argumentValue
|
|
12
|
-
? "Why are you creating a FalsyError with a truthy `argumentValue`: '" +
|
|
13
|
-
argumentValue +
|
|
14
|
-
"'?"
|
|
15
|
-
: (functionName || "We") +
|
|
16
|
-
" expected " +
|
|
17
|
-
(argumentName ? "`" + argumentName + "`" : "an argument") +
|
|
18
|
-
" to be truthy. " +
|
|
19
|
-
(argumentName ? "`" + argumentName + "`" : "It") +
|
|
20
|
-
" is the falsy value " +
|
|
21
|
-
(argumentValue === "" ? "''" : argumentValue) +
|
|
22
|
-
".",
|
|
23
|
-
);
|
|
24
|
-
this.name = this.constructor.name;
|
|
25
|
-
this.argumentName = argumentName;
|
|
26
|
-
}
|
|
27
|
-
};
|
package/lib/MissingError.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
module.exports = class MissingError extends TypeError {
|
|
4
|
-
static check(argumentValue, argumentName, functionName) {
|
|
5
|
-
if (argumentValue != null) return;
|
|
6
|
-
throw new MissingError(argumentValue, argumentName, functionName);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
constructor(argumentValue, argumentName, functionName) {
|
|
10
|
-
super(
|
|
11
|
-
argumentValue != null
|
|
12
|
-
? "Why are you creating a MissingError with a defined `argumentValue`: '" +
|
|
13
|
-
argumentValue +
|
|
14
|
-
"'?"
|
|
15
|
-
: (functionName || "We") +
|
|
16
|
-
" expected " +
|
|
17
|
-
(argumentName ? "`" + argumentName + "`" : "an argument") +
|
|
18
|
-
" to be defined. " +
|
|
19
|
-
(argumentName ? "`" + argumentName + "`" : "It") +
|
|
20
|
-
" is '" +
|
|
21
|
-
argumentValue +
|
|
22
|
-
"'.",
|
|
23
|
-
);
|
|
24
|
-
this.name = this.constructor.name;
|
|
25
|
-
this.argumentName = argumentName;
|
|
26
|
-
}
|
|
27
|
-
};
|
package/lib/RethrownError.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* See {@link Errors#RethrownError}.
|
|
5
|
-
*/
|
|
6
|
-
module.exports = class RethrownError extends Error {
|
|
7
|
-
constructor(error, message) {
|
|
8
|
-
super(message != null ? message : getErrorProperty(error, "message"));
|
|
9
|
-
if (!error)
|
|
10
|
-
throw new TypeError(
|
|
11
|
-
`RethrownError's constructor expects a defined error, but it was ${error}.`,
|
|
12
|
-
);
|
|
13
|
-
this.name = this.constructor.name;
|
|
14
|
-
this.original = error;
|
|
15
|
-
this.newStack = this.stack;
|
|
16
|
-
const errorStack = getErrorProperty(error, "stack");
|
|
17
|
-
this.stack = `${this.stack}\n${errorStack}`;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
function getErrorProperty(error, property) {
|
|
22
|
-
if (error[property]) return error[property];
|
|
23
|
-
try {
|
|
24
|
-
return JSON.stringify(error);
|
|
25
|
-
} catch (e) {
|
|
26
|
-
// JSON.stringify might throw the following, "TypeError: Converting circular
|
|
27
|
-
// structure to JSON."
|
|
28
|
-
return error;
|
|
29
|
-
}
|
|
30
|
-
}
|
package/test/ErrorsTest.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { expect } = require("chai");
|
|
4
|
-
const errors = require("..");
|
|
5
|
-
const { AsyncError, RethrownError } = errors;
|
|
6
|
-
|
|
7
|
-
describe("Errors#catch(promise)", () => {
|
|
8
|
-
it("should return a Promise that throws an AsyncError upon rejection", () => {
|
|
9
|
-
function rejectAPromise() {
|
|
10
|
-
return Promise.reject(new TypeError("Fail!"));
|
|
11
|
-
}
|
|
12
|
-
return errors.catch(rejectAPromise()).catch((error) => {
|
|
13
|
-
expect(error).to.be.an.instanceof(AsyncError);
|
|
14
|
-
expect(error.message).to.eql("Fail!");
|
|
15
|
-
expect(error.original).to.be.an.instanceof(TypeError);
|
|
16
|
-
expect(error.stack).to.include("AsyncError: Fail!");
|
|
17
|
-
expect(error.stack).to.include("at rejectAPromise");
|
|
18
|
-
expect(error.stack).to.include("TypeError: Fail!");
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
it("should take the optional message argument", () => {
|
|
22
|
-
function rejectAPromise() {
|
|
23
|
-
return Promise.reject(new TypeError("Fail!"));
|
|
24
|
-
}
|
|
25
|
-
return errors.catch(rejectAPromise(), "optional message").catch((error) => {
|
|
26
|
-
expect(error).to.be.an.instanceof(AsyncError);
|
|
27
|
-
expect(error.message).to.eql("optional message");
|
|
28
|
-
expect(error.original).to.be.an.instanceof(TypeError);
|
|
29
|
-
expect(error.stack).to.include("AsyncError: optional message");
|
|
30
|
-
expect(error.stack).to.include("at rejectAPromise");
|
|
31
|
-
expect(error.stack).to.include("TypeError: Fail!");
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
it("should return a Promise that throws an AsyncError upon rejecting with a string", () => {
|
|
35
|
-
function rejectAPromise() {
|
|
36
|
-
// eslint-disable-next-line prefer-promise-reject-errors
|
|
37
|
-
return Promise.reject("ENTITY_NOT_FOUND");
|
|
38
|
-
}
|
|
39
|
-
return errors.catch(rejectAPromise()).catch((error) => {
|
|
40
|
-
expect(error).to.be.an.instanceof(AsyncError);
|
|
41
|
-
expect(error.message).to.eql('"ENTITY_NOT_FOUND"');
|
|
42
|
-
expect(error.original).to.eql("ENTITY_NOT_FOUND");
|
|
43
|
-
expect(error.stack).to.include("AsyncError");
|
|
44
|
-
expect(error.stack).to.include("ENTITY_NOT_FOUND");
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
it("should return a Promise that throws an AsyncError upon rejecting with an array", () => {
|
|
48
|
-
function rejectAPromise() {
|
|
49
|
-
// eslint-disable-next-line prefer-promise-reject-errors
|
|
50
|
-
return Promise.reject([{ code: "1234" }]);
|
|
51
|
-
}
|
|
52
|
-
return errors.catch(rejectAPromise()).catch((error) => {
|
|
53
|
-
expect(error).to.be.an.instanceof(AsyncError);
|
|
54
|
-
expect(error.message).to.eql('[{"code":"1234"}]');
|
|
55
|
-
expect(error.original).to.eql([{ code: "1234" }]);
|
|
56
|
-
expect(error.stack).to.include("AsyncError");
|
|
57
|
-
expect(error.stack).to.include('[{"code":"1234"}]');
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
it("should return a Promise that throws an AsyncError upon rejecting with a circularly referenced object", () => {
|
|
61
|
-
const obj = {};
|
|
62
|
-
obj.circular = obj;
|
|
63
|
-
return errors.catch(Promise.reject(obj)).catch((error) => {
|
|
64
|
-
expect(error).to.be.an.instanceof(AsyncError);
|
|
65
|
-
expect(error.message).to.eql("[object Object]");
|
|
66
|
-
expect(error.original).to.eql(obj);
|
|
67
|
-
expect(error.stack).to.include("AsyncError");
|
|
68
|
-
expect(error.stack).to.include("[object Object]");
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
it("should throw a TypeError if the given promise is not defined", () => {
|
|
72
|
-
expect(() => errors.catch()).to.throw(TypeError);
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
describe("new Errors#RethrownError(message, error)", () => {
|
|
77
|
-
it("should create a rethrown error with an appropriate stack trace", () => {
|
|
78
|
-
function throwATypeError() {
|
|
79
|
-
throw new TypeError("Invalid Argument");
|
|
80
|
-
}
|
|
81
|
-
function rethrowTheTypeError() {
|
|
82
|
-
try {
|
|
83
|
-
throwATypeError();
|
|
84
|
-
} catch (error) {
|
|
85
|
-
expect(new RethrownError(error).stack).to.include(
|
|
86
|
-
"RethrownError: Invalid Argument\n",
|
|
87
|
-
);
|
|
88
|
-
expect(new RethrownError(error, "").stack).to.include(
|
|
89
|
-
"RethrownError\n",
|
|
90
|
-
);
|
|
91
|
-
throw new RethrownError(error, "Lorem Ipsum");
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
rethrowTheTypeError();
|
|
96
|
-
expect.fail("We expected the runtime to throw a RethrownError.");
|
|
97
|
-
} catch (error) {
|
|
98
|
-
if (!(error instanceof RethrownError)) throw error;
|
|
99
|
-
expect(error.message).to.eql("Lorem Ipsum");
|
|
100
|
-
expect(error.original).to.be.an.instanceof(TypeError);
|
|
101
|
-
expect(error.stack).to.include("RethrownError: Lorem Ipsum");
|
|
102
|
-
expect(error.stack).to.include("at rethrowTheTypeError");
|
|
103
|
-
expect(error.stack).to.include("TypeError: Invalid Argument");
|
|
104
|
-
expect(error.stack).to.include("at throwATypeError");
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
it("should create a rethrown error from a thrown string", () => {
|
|
108
|
-
function throwAString() {
|
|
109
|
-
// eslint-disable-next-line no-throw-literal
|
|
110
|
-
throw "ENTITY_NOT_FOUND";
|
|
111
|
-
}
|
|
112
|
-
function rethrowTheString() {
|
|
113
|
-
try {
|
|
114
|
-
throwAString();
|
|
115
|
-
} catch (error) {
|
|
116
|
-
expect(new RethrownError(error).stack).to.include(
|
|
117
|
-
'RethrownError: "ENTITY_NOT_FOUND"\n',
|
|
118
|
-
);
|
|
119
|
-
expect(new RethrownError(error).stack.endsWith('"ENTITY_NOT_FOUND"')).to
|
|
120
|
-
.be.true;
|
|
121
|
-
expect(new RethrownError(error, "").stack).to.include(
|
|
122
|
-
"RethrownError\n",
|
|
123
|
-
);
|
|
124
|
-
expect(new RethrownError(error, 0).stack).to.include(
|
|
125
|
-
"RethrownError: 0\n",
|
|
126
|
-
);
|
|
127
|
-
expect(new RethrownError(error, "").stack).to.include(
|
|
128
|
-
"ENTITY_NOT_FOUND",
|
|
129
|
-
);
|
|
130
|
-
throw new RethrownError(error, "Lorem Ipsum");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
try {
|
|
134
|
-
rethrowTheString();
|
|
135
|
-
expect.fail("We expected the runtime to throw a RethrownError.");
|
|
136
|
-
} catch (error) {
|
|
137
|
-
if (!(error instanceof RethrownError)) throw error;
|
|
138
|
-
expect(error.message).to.eql("Lorem Ipsum");
|
|
139
|
-
expect(error.original).to.eql("ENTITY_NOT_FOUND");
|
|
140
|
-
expect(error.stack).to.include("RethrownError");
|
|
141
|
-
expect(error.stack).to.include("at rethrowTheString");
|
|
142
|
-
expect(error.stack).to.include("ENTITY_NOT_FOUND");
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
it("should create a rethrown error from a thrown array", () => {
|
|
146
|
-
function throwAnArray() {
|
|
147
|
-
// eslint-disable-next-line no-throw-literal
|
|
148
|
-
throw [{ code: "1234" }];
|
|
149
|
-
}
|
|
150
|
-
function rethrowTheArray() {
|
|
151
|
-
try {
|
|
152
|
-
throwAnArray();
|
|
153
|
-
} catch (error) {
|
|
154
|
-
expect(new RethrownError(error).stack).to.include(
|
|
155
|
-
'RethrownError: [{"code":"1234"}]\n',
|
|
156
|
-
);
|
|
157
|
-
expect(new RethrownError(error).stack.endsWith('[{"code":"1234"}]')).to
|
|
158
|
-
.be.true;
|
|
159
|
-
expect(new RethrownError(error, "").stack).to.include(
|
|
160
|
-
"RethrownError\n",
|
|
161
|
-
);
|
|
162
|
-
expect(new RethrownError(error, "").stack).to.include(
|
|
163
|
-
'[{"code":"1234"}]',
|
|
164
|
-
);
|
|
165
|
-
throw new RethrownError(error, "Lorem Ipsum");
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
try {
|
|
169
|
-
rethrowTheArray();
|
|
170
|
-
expect.fail("We expected the runtime to throw a RethrownError.");
|
|
171
|
-
} catch (error) {
|
|
172
|
-
if (!(error instanceof RethrownError)) throw error;
|
|
173
|
-
expect(error.message).to.eql("Lorem Ipsum");
|
|
174
|
-
expect(error.original).to.eql([{ code: "1234" }]);
|
|
175
|
-
expect(error.stack).to.include("RethrownError");
|
|
176
|
-
expect(error.stack).to.include("at rethrowTheArray");
|
|
177
|
-
expect(error.stack).to.include('[{"code":"1234"}]');
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
it("should create a rethrown error from a circularly referenced object", () => {
|
|
181
|
-
const obj = {};
|
|
182
|
-
obj.circular = obj;
|
|
183
|
-
const error = new RethrownError(obj);
|
|
184
|
-
expect(error.stack).to.include("RethrownError: [object Object]\n");
|
|
185
|
-
expect(error.stack).to.include("[object Object]");
|
|
186
|
-
});
|
|
187
|
-
it("should throw an error when given an invalid error", () => {
|
|
188
|
-
expect(() => new RethrownError()).to.throw(TypeError);
|
|
189
|
-
expect(() => new RethrownError(null, "Lorem Ipsum")).to.throw(TypeError);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { expect } = require("chai");
|
|
4
|
-
const { FalsyError } = require("..");
|
|
5
|
-
|
|
6
|
-
describe("FalsyError#constructor", () => {
|
|
7
|
-
it('should create a new FalsyError instance given a falsy argument value: (null, "argument", "Lawyer#argue").', () => {
|
|
8
|
-
const actual = new FalsyError(null, "argument", "Lawyer#argue");
|
|
9
|
-
|
|
10
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
11
|
-
|
|
12
|
-
const expectedName = "FalsyError";
|
|
13
|
-
const actualName = actual.name;
|
|
14
|
-
expect(actualName).to.equal(expectedName);
|
|
15
|
-
|
|
16
|
-
const expectedMessage =
|
|
17
|
-
"Lawyer#argue expected `argument` to be truthy. `argument` is the falsy value null.";
|
|
18
|
-
const actualMessage = actual.message;
|
|
19
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
20
|
-
|
|
21
|
-
const expectedArgumentName = "argument";
|
|
22
|
-
const actualArgumentName = actual.argumentName;
|
|
23
|
-
expect(actualArgumentName).to.equal(expectedArgumentName);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should create a new FalsyError instance given a falsy argument value: (0, "argument").', () => {
|
|
27
|
-
const actual = new FalsyError(0, "argument");
|
|
28
|
-
|
|
29
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
30
|
-
|
|
31
|
-
const expectedName = "FalsyError";
|
|
32
|
-
const actualName = actual.name;
|
|
33
|
-
expect(actualName).to.equal(expectedName);
|
|
34
|
-
|
|
35
|
-
const expectedMessage =
|
|
36
|
-
"We expected `argument` to be truthy. `argument` is the falsy value 0.";
|
|
37
|
-
const actualMessage = actual.message;
|
|
38
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
39
|
-
|
|
40
|
-
const expectedArgumentName = "argument";
|
|
41
|
-
const actualArgumentName = actual.argumentName;
|
|
42
|
-
expect(actualArgumentName).to.equal(expectedArgumentName);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('should create a new FalsyError instance given a falsy argument value: ("", null, "Lawyer#argue").', () => {
|
|
46
|
-
const actual = new FalsyError("", null, "Lawyer#argue");
|
|
47
|
-
|
|
48
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
49
|
-
|
|
50
|
-
const expectedName = "FalsyError";
|
|
51
|
-
const actualName = actual.name;
|
|
52
|
-
expect(actualName).to.equal(expectedName);
|
|
53
|
-
|
|
54
|
-
const expectedMessage =
|
|
55
|
-
"Lawyer#argue expected an argument to be truthy. It is the falsy value ''.";
|
|
56
|
-
const actualMessage = actual.message;
|
|
57
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
58
|
-
|
|
59
|
-
const actualArgumentName = actual.argumentName;
|
|
60
|
-
expect(actualArgumentName).to.be.null;
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should create a new FalsyError instance even when given a truthy argument value: ("whyTruthy", "badUsage", "Coder#useWrong").', () => {
|
|
64
|
-
// If a coder constructs this class with a truthy argument, they are using
|
|
65
|
-
// this constructor wrong. The coder is already throwing an error, so let us
|
|
66
|
-
// just display a weird message instead of throwing a different error.
|
|
67
|
-
|
|
68
|
-
const actual = new FalsyError("whyTruthy", "badUsage", "Coder#useWrong");
|
|
69
|
-
|
|
70
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
71
|
-
|
|
72
|
-
const expectedName = "FalsyError";
|
|
73
|
-
const actualName = actual.name;
|
|
74
|
-
expect(actualName).to.equal(expectedName);
|
|
75
|
-
|
|
76
|
-
const expectedMessage =
|
|
77
|
-
"Why are you creating a FalsyError with a truthy `argumentValue`: 'whyTruthy'?";
|
|
78
|
-
const actualMessage = actual.message;
|
|
79
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
80
|
-
|
|
81
|
-
const actualArgumentName = actual.argumentName;
|
|
82
|
-
expect(actualArgumentName).to.equal("badUsage");
|
|
83
|
-
});
|
|
84
|
-
});
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { expect } = require("chai");
|
|
4
|
-
const { FalsyError } = require("..");
|
|
5
|
-
|
|
6
|
-
describe("FalsyError#check", () => {
|
|
7
|
-
it('should throw a FalsyError given a falsy argument value: (NaN, "argument", "Lawyer#argue").', () => {
|
|
8
|
-
try {
|
|
9
|
-
FalsyError.check(NaN, "argument", "Lawyer#argue");
|
|
10
|
-
throw new Error("The method did not throw an error.");
|
|
11
|
-
} catch (e) {
|
|
12
|
-
expect(e).to.be.an.instanceof(FalsyError);
|
|
13
|
-
expect(e.message).to.equal(
|
|
14
|
-
"Lawyer#argue expected `argument` to be truthy. `argument` is the falsy value NaN.",
|
|
15
|
-
);
|
|
16
|
-
expect(e.argumentName).to.equal("argument");
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should throw a FalsyError given a falsy argument value: (false, "argument").', () => {
|
|
21
|
-
try {
|
|
22
|
-
FalsyError.check(false, "argument");
|
|
23
|
-
throw new Error("The method did not throw an error.");
|
|
24
|
-
} catch (e) {
|
|
25
|
-
expect(e).to.be.an.instanceof(FalsyError);
|
|
26
|
-
expect(e.message).to.equal(
|
|
27
|
-
"We expected `argument` to be truthy. `argument` is the falsy value false.",
|
|
28
|
-
);
|
|
29
|
-
expect(e.argumentName).to.equal("argument");
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should throw a FalsyError given a falsy argument value: (undefined, null, "Lawyer#argue").', () => {
|
|
34
|
-
try {
|
|
35
|
-
FalsyError.check(undefined, null, "Lawyer#argue");
|
|
36
|
-
throw new Error("The method did not throw an error.");
|
|
37
|
-
} catch (e) {
|
|
38
|
-
expect(e).to.be.an.instanceof(FalsyError);
|
|
39
|
-
expect(e.message).to.equal(
|
|
40
|
-
"Lawyer#argue expected an argument to be truthy. It is the falsy value undefined.",
|
|
41
|
-
);
|
|
42
|
-
expect(e.argumentName).to.be.null;
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("should throw a FalsyError given a falsy argument value: (null).", () => {
|
|
47
|
-
try {
|
|
48
|
-
FalsyError.check(null);
|
|
49
|
-
throw new Error("The method did not throw an error.");
|
|
50
|
-
} catch (e) {
|
|
51
|
-
expect(e).to.be.an.instanceof(FalsyError);
|
|
52
|
-
expect(e.message).to.equal(
|
|
53
|
-
"We expected an argument to be truthy. It is the falsy value null.",
|
|
54
|
-
);
|
|
55
|
-
expect(e.argumentName).to.be.undefined;
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('should do nothing given a truthy argument value: ("truthyValue", "truthy", "Fountain#spitTruth").', () => {
|
|
60
|
-
FalsyError.check("truthyValue", "truthy", "Fountain#spitTruth");
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should do nothing given a truthy argument value: ("truthyValue", "truthy").', () => {
|
|
64
|
-
FalsyError.check("truthyValue", "truthy");
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should do nothing given a truthy argument value: ("truthyValue", null, "Fountain#spitTruth").', () => {
|
|
68
|
-
FalsyError.check("truthyValue", null, "Fountain#spitTruth");
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should do nothing given a truthy argument value: ("truthyValue").', () => {
|
|
72
|
-
FalsyError.check("truthyValue");
|
|
73
|
-
});
|
|
74
|
-
});
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { expect } = require("chai");
|
|
4
|
-
const { MissingError } = require("..");
|
|
5
|
-
|
|
6
|
-
describe("MissingError#constructor", () => {
|
|
7
|
-
it('should create a new MissingError instance given a \'null\' argument value: (null, "argument", "Lawyer#argue").', () => {
|
|
8
|
-
const actual = new MissingError(null, "argument", "Lawyer#argue");
|
|
9
|
-
|
|
10
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
11
|
-
|
|
12
|
-
const expectedName = "MissingError";
|
|
13
|
-
const actualName = actual.name;
|
|
14
|
-
expect(actualName).to.equal(expectedName);
|
|
15
|
-
|
|
16
|
-
const expectedMessage =
|
|
17
|
-
"Lawyer#argue expected `argument` to be defined. `argument` is 'null'.";
|
|
18
|
-
const actualMessage = actual.message;
|
|
19
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
20
|
-
|
|
21
|
-
const expectedArgumentName = "argument";
|
|
22
|
-
const actualArgumentName = actual.argumentName;
|
|
23
|
-
expect(actualArgumentName).to.equal(expectedArgumentName);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("should create a new MissingError instance given a 'null' argument value: (null, \"argument\").", () => {
|
|
27
|
-
const actual = new MissingError(null, "argument");
|
|
28
|
-
|
|
29
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
30
|
-
|
|
31
|
-
const expectedName = "MissingError";
|
|
32
|
-
const actualName = actual.name;
|
|
33
|
-
expect(actualName).to.equal(expectedName);
|
|
34
|
-
|
|
35
|
-
const expectedMessage =
|
|
36
|
-
"We expected `argument` to be defined. `argument` is 'null'.";
|
|
37
|
-
const actualMessage = actual.message;
|
|
38
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
39
|
-
|
|
40
|
-
const expectedArgumentName = "argument";
|
|
41
|
-
const actualArgumentName = actual.argumentName;
|
|
42
|
-
expect(actualArgumentName).to.equal(expectedArgumentName);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("should create a new MissingError instance given a 'null' argument value: (null, null, \"Lawyer#argue\").", () => {
|
|
46
|
-
const actual = new MissingError(null, null, "Lawyer#argue");
|
|
47
|
-
|
|
48
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
49
|
-
|
|
50
|
-
const expectedName = "MissingError";
|
|
51
|
-
const actualName = actual.name;
|
|
52
|
-
expect(actualName).to.equal(expectedName);
|
|
53
|
-
|
|
54
|
-
const expectedMessage =
|
|
55
|
-
"Lawyer#argue expected an argument to be defined. It is 'null'.";
|
|
56
|
-
const actualMessage = actual.message;
|
|
57
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
58
|
-
|
|
59
|
-
const actualArgumentName = actual.argumentName;
|
|
60
|
-
expect(actualArgumentName).to.be.null;
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should create a new MissingError instance given an \'undefined\' argument value: (undefined, "argument", "Lawyer#argue").', () => {
|
|
64
|
-
const actual = new MissingError(undefined, "argument", "Lawyer#argue");
|
|
65
|
-
|
|
66
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
67
|
-
|
|
68
|
-
const expectedName = "MissingError";
|
|
69
|
-
const actualName = actual.name;
|
|
70
|
-
expect(actualName).to.equal(expectedName);
|
|
71
|
-
|
|
72
|
-
const expectedMessage =
|
|
73
|
-
"Lawyer#argue expected `argument` to be defined. `argument` is 'undefined'.";
|
|
74
|
-
const actualMessage = actual.message;
|
|
75
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
76
|
-
|
|
77
|
-
const expectedArgumentName = "argument";
|
|
78
|
-
const actualArgumentName = actual.argumentName;
|
|
79
|
-
expect(actualArgumentName).to.equal(expectedArgumentName);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("should create a new MissingError instance given an 'undefined' argument value: (undefined, \"argument\").", () => {
|
|
83
|
-
const actual = new MissingError(undefined, "argument");
|
|
84
|
-
|
|
85
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
86
|
-
|
|
87
|
-
const expectedName = "MissingError";
|
|
88
|
-
const actualName = actual.name;
|
|
89
|
-
expect(actualName).to.equal(expectedName);
|
|
90
|
-
|
|
91
|
-
const expectedMessage =
|
|
92
|
-
"We expected `argument` to be defined. `argument` is 'undefined'.";
|
|
93
|
-
const actualMessage = actual.message;
|
|
94
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
95
|
-
|
|
96
|
-
const expectedArgumentName = "argument";
|
|
97
|
-
const actualArgumentName = actual.argumentName;
|
|
98
|
-
expect(actualArgumentName).to.equal(expectedArgumentName);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("should create a new MissingError instance given an 'undefined' argument value: (undefined, null, \"Lawyer#argue\").", () => {
|
|
102
|
-
const actual = new MissingError(undefined, null, "Lawyer#argue");
|
|
103
|
-
|
|
104
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
105
|
-
|
|
106
|
-
const expectedName = "MissingError";
|
|
107
|
-
const actualName = actual.name;
|
|
108
|
-
expect(actualName).to.equal(expectedName);
|
|
109
|
-
|
|
110
|
-
const expectedMessage =
|
|
111
|
-
"Lawyer#argue expected an argument to be defined. It is 'undefined'.";
|
|
112
|
-
const actualMessage = actual.message;
|
|
113
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
114
|
-
|
|
115
|
-
const actualArgumentName = actual.argumentName;
|
|
116
|
-
expect(actualArgumentName).to.be.null;
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("should create a new MissingError instance given an 'undefined' argument value: no arguments.", () => {
|
|
120
|
-
const actual = new MissingError();
|
|
121
|
-
|
|
122
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
123
|
-
|
|
124
|
-
const expectedName = "MissingError";
|
|
125
|
-
const actualName = actual.name;
|
|
126
|
-
expect(actualName).to.equal(expectedName);
|
|
127
|
-
|
|
128
|
-
const expectedMessage =
|
|
129
|
-
"We expected an argument to be defined. It is 'undefined'.";
|
|
130
|
-
const actualMessage = actual.message;
|
|
131
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
132
|
-
|
|
133
|
-
const actualArgumentName = actual.argumentName;
|
|
134
|
-
expect(actualArgumentName).to.be.undefined;
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('should create a new MissingError instance even when given a defined argument value: ("whyDefined", "badUsage", "Coder#useWrong").', () => {
|
|
138
|
-
// If a coder constructs this class with a defined argument, they are using
|
|
139
|
-
// this constructor wrong. The coder is already throwing an error, so let us
|
|
140
|
-
// just display a weird message instead of throwing a different error.
|
|
141
|
-
|
|
142
|
-
const actual = new MissingError("whyDefined", "badUsage", "Coder#useWrong");
|
|
143
|
-
|
|
144
|
-
expect(actual).to.be.an.instanceof(TypeError);
|
|
145
|
-
|
|
146
|
-
const expectedName = "MissingError";
|
|
147
|
-
const actualName = actual.name;
|
|
148
|
-
expect(actualName).to.equal(expectedName);
|
|
149
|
-
|
|
150
|
-
const expectedMessage =
|
|
151
|
-
"Why are you creating a MissingError with a defined `argumentValue`: 'whyDefined'?";
|
|
152
|
-
const actualMessage = actual.message;
|
|
153
|
-
expect(actualMessage).to.equal(expectedMessage);
|
|
154
|
-
|
|
155
|
-
const actualArgumentName = actual.argumentName;
|
|
156
|
-
expect(actualArgumentName).to.equal("badUsage");
|
|
157
|
-
});
|
|
158
|
-
});
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { expect } = require("chai");
|
|
4
|
-
const { MissingError } = require("..");
|
|
5
|
-
|
|
6
|
-
describe("MissingError#check", () => {
|
|
7
|
-
it('should throw a MissingError given a \'null\' argument value: (null, "argument", "Lawyer#argue").', () => {
|
|
8
|
-
try {
|
|
9
|
-
MissingError.check(null, "argument", "Lawyer#argue");
|
|
10
|
-
throw new Error("The method did not throw an error.");
|
|
11
|
-
} catch (e) {
|
|
12
|
-
expect(e).to.be.an.instanceof(MissingError);
|
|
13
|
-
expect(e.argumentName).to.equal("argument");
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it("should throw a MissingError given a 'null' argument value: (null, \"argument\").", () => {
|
|
18
|
-
try {
|
|
19
|
-
MissingError.check(null, "argument");
|
|
20
|
-
throw new Error("The method did not throw an error.");
|
|
21
|
-
} catch (e) {
|
|
22
|
-
expect(e).to.be.an.instanceof(MissingError);
|
|
23
|
-
expect(e.argumentName).to.equal("argument");
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("should throw a MissingError given a 'null' argument value: (null, null, \"Lawyer#argue\").", () => {
|
|
28
|
-
try {
|
|
29
|
-
MissingError.check(null, null, "Lawyer#argue");
|
|
30
|
-
throw new Error("The method did not throw an error.");
|
|
31
|
-
} catch (e) {
|
|
32
|
-
expect(e).to.be.an.instanceof(MissingError);
|
|
33
|
-
expect(e.argumentName).to.be.null;
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("should throw a MissingError given a 'null' argument value: (null).", () => {
|
|
38
|
-
try {
|
|
39
|
-
MissingError.check(null);
|
|
40
|
-
throw new Error("The method did not throw an error.");
|
|
41
|
-
} catch (e) {
|
|
42
|
-
expect(e).to.be.an.instanceof(MissingError);
|
|
43
|
-
expect(e.argumentName).to.be.undefined;
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('should throw a MissingError given an \'undefined\' argument value: (undefined, "argument", "Lawyer#argue").', () => {
|
|
48
|
-
try {
|
|
49
|
-
MissingError.check(undefined, "argument", "Lawyer#argue");
|
|
50
|
-
throw new Error("The method did not throw an error.");
|
|
51
|
-
} catch (e) {
|
|
52
|
-
expect(e).to.be.an.instanceof(MissingError);
|
|
53
|
-
expect(e.argumentName).to.equal("argument");
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("should throw a MissingError given an 'undefined' argument value: (undefined, \"argument\").", () => {
|
|
58
|
-
try {
|
|
59
|
-
MissingError.check(undefined, "argument");
|
|
60
|
-
throw new Error("The method did not throw an error.");
|
|
61
|
-
} catch (e) {
|
|
62
|
-
expect(e).to.be.an.instanceof(MissingError);
|
|
63
|
-
expect(e.argumentName).to.equal("argument");
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("should throw a MissingError given an 'undefined' argument value: (undefined, null, \"Lawyer#argue\").", () => {
|
|
68
|
-
try {
|
|
69
|
-
MissingError.check(undefined, null, "Lawyer#argue");
|
|
70
|
-
throw new Error("The method did not throw an error.");
|
|
71
|
-
} catch (e) {
|
|
72
|
-
expect(e).to.be.an.instanceof(MissingError);
|
|
73
|
-
expect(e.argumentName).to.be.null;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("should throw a MissingError given an 'undefined' argument value: no arguments.", () => {
|
|
78
|
-
try {
|
|
79
|
-
MissingError.check(undefined);
|
|
80
|
-
throw new Error("The method did not throw an error.");
|
|
81
|
-
} catch (e) {
|
|
82
|
-
expect(e).to.be.an.instanceof(MissingError);
|
|
83
|
-
expect(e.argumentName).to.be.undefined;
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('should do nothing given a defined argument value: (0, "zero", "Zero#returnZero").', () => {
|
|
88
|
-
MissingError.check(0, "zero", "Zero#returnZero");
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should do nothing given a defined argument value: (0, "zero").', () => {
|
|
92
|
-
MissingError.check(0, "zero");
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should do nothing given a defined argument value: (0, null, "Zero#returnZero").', () => {
|
|
96
|
-
MissingError.check(0, null, "Zero#returnZero");
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it("should do nothing given a defined argument value: (0).", () => {
|
|
100
|
-
MissingError.check(0);
|
|
101
|
-
});
|
|
102
|
-
});
|