check-error 2.1.0 → 2.1.3
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/README.md +23 -83
- package/index.js +6 -9
- package/package.json +9 -43
package/README.md
CHANGED
|
@@ -1,75 +1,15 @@
|
|
|
1
1
|
<h1 align=center>
|
|
2
2
|
<a href="http://chaijs.com" title="Chai Documentation">
|
|
3
|
-
<img alt="ChaiJS" src="http://chaijs.com/img/chai-logo.png"
|
|
3
|
+
<img alt="ChaiJS" src="http://chaijs.com/img/chai-logo.png">
|
|
4
4
|
</a>
|
|
5
|
+
<br>
|
|
6
|
+
check-error
|
|
5
7
|
</h1>
|
|
6
8
|
|
|
7
9
|
<p align=center>
|
|
8
10
|
Error comparison and information related utility for <a href="http://nodejs.org">node</a> and the browser.
|
|
9
11
|
</p>
|
|
10
12
|
|
|
11
|
-
<p align=center>
|
|
12
|
-
<a href="./LICENSE">
|
|
13
|
-
<img
|
|
14
|
-
alt="license:mit"
|
|
15
|
-
src="https://img.shields.io/badge/license-mit-green.svg?style=flat-square"
|
|
16
|
-
/>
|
|
17
|
-
</a>
|
|
18
|
-
<a href="https://github.com/chaijs/check-error/releases">
|
|
19
|
-
<img
|
|
20
|
-
alt="tag:?"
|
|
21
|
-
src="https://img.shields.io/github/tag/chaijs/check-error.svg?style=flat-square"
|
|
22
|
-
/>
|
|
23
|
-
</a>
|
|
24
|
-
|
|
25
|
-

|
|
26
|
-
|
|
27
|
-
<a href="https://coveralls.io/r/chaijs/check-error">
|
|
28
|
-
<img
|
|
29
|
-
alt="coverage:?"
|
|
30
|
-
src="https://img.shields.io/coveralls/chaijs/check-error/master.svg?style=flat-square"
|
|
31
|
-
/>
|
|
32
|
-
</a>
|
|
33
|
-
<a href="https://www.npmjs.com/packages/check-error">
|
|
34
|
-
<img
|
|
35
|
-
alt="npm:?"
|
|
36
|
-
src="https://img.shields.io/npm/v/check-error.svg?style=flat-square"
|
|
37
|
-
/>
|
|
38
|
-
</a>
|
|
39
|
-
<a href="https://www.npmjs.com/packages/check-error">
|
|
40
|
-
<img
|
|
41
|
-
alt="dependencies:?"
|
|
42
|
-
src="https://img.shields.io/npm/dm/check-error.svg?style=flat-square"
|
|
43
|
-
/>
|
|
44
|
-
</a>
|
|
45
|
-
<a href="">
|
|
46
|
-
<img
|
|
47
|
-
alt="devDependencies:?"
|
|
48
|
-
src="https://img.shields.io/david/chaijs/check-error.svg?style=flat-square"
|
|
49
|
-
/>
|
|
50
|
-
</a>
|
|
51
|
-
<br/>
|
|
52
|
-
<a href="https://saucelabs.com/u/chaijs-check-error">
|
|
53
|
-
<img
|
|
54
|
-
alt="Selenium Test Status"
|
|
55
|
-
src="https://saucelabs.com/browser-matrix/chaijs-check-error.svg"
|
|
56
|
-
/>
|
|
57
|
-
</a>
|
|
58
|
-
<br>
|
|
59
|
-
<a href="https://chai-slack.herokuapp.com/">
|
|
60
|
-
<img
|
|
61
|
-
alt="Join the Slack chat"
|
|
62
|
-
src="https://img.shields.io/badge/slack-join%20chat-E2206F.svg?style=flat-square"
|
|
63
|
-
/>
|
|
64
|
-
</a>
|
|
65
|
-
<a href="https://gitter.im/chaijs/chai">
|
|
66
|
-
<img
|
|
67
|
-
alt="Join the Gitter chat"
|
|
68
|
-
src="https://img.shields.io/badge/gitter-join%20chat-D0104D.svg?style=flat-square"
|
|
69
|
-
/>
|
|
70
|
-
</a>
|
|
71
|
-
</p>
|
|
72
|
-
|
|
73
13
|
## What is Check-Error?
|
|
74
14
|
|
|
75
15
|
Check-Error is a module which you can use to retrieve an Error's information such as its `message` or `constructor` name and also to check whether two Errors are compatible based on their messages, constructors or even instances.
|
|
@@ -101,16 +41,16 @@ The primary export of `check-error` is an object which has the following methods
|
|
|
101
41
|
* `getMessage(err)` - Retrieves the message of an error or `err` itself if it's a String. If `err` or `err.message` is undefined we return an empty String.
|
|
102
42
|
|
|
103
43
|
```js
|
|
104
|
-
|
|
44
|
+
import * as checkError 'check-error';
|
|
105
45
|
```
|
|
106
46
|
|
|
107
47
|
#### .compatibleInstance(err, errorLike)
|
|
108
48
|
|
|
109
49
|
```js
|
|
110
|
-
|
|
50
|
+
import * as checkError 'check-error';
|
|
111
51
|
|
|
112
|
-
|
|
113
|
-
|
|
52
|
+
const funcThatThrows = function() { throw new TypeError('I am a TypeError') };
|
|
53
|
+
let caughtErr;
|
|
114
54
|
|
|
115
55
|
try {
|
|
116
56
|
funcThatThrows();
|
|
@@ -118,7 +58,7 @@ try {
|
|
|
118
58
|
caughtErr = e;
|
|
119
59
|
}
|
|
120
60
|
|
|
121
|
-
|
|
61
|
+
const sameInstance = caughtErr;
|
|
122
62
|
|
|
123
63
|
checkError.compatibleInstance(caughtErr, sameInstance); // true
|
|
124
64
|
checkError.compatibleInstance(caughtErr, new TypeError('Another error')); // false
|
|
@@ -127,10 +67,10 @@ checkError.compatibleInstance(caughtErr, new TypeError('Another error')); // fal
|
|
|
127
67
|
#### .compatibleConstructor(err, errorLike)
|
|
128
68
|
|
|
129
69
|
```js
|
|
130
|
-
|
|
70
|
+
import * as checkError 'check-error';
|
|
131
71
|
|
|
132
|
-
|
|
133
|
-
|
|
72
|
+
const funcThatThrows = function() { throw new TypeError('I am a TypeError') };
|
|
73
|
+
let caughtErr;
|
|
134
74
|
|
|
135
75
|
try {
|
|
136
76
|
funcThatThrows();
|
|
@@ -146,10 +86,10 @@ checkError.compatibleConstructor(caughtErr, RangeError); // false
|
|
|
146
86
|
#### .compatibleMessage(err, errMatcher)
|
|
147
87
|
|
|
148
88
|
```js
|
|
149
|
-
|
|
89
|
+
import * as checkError 'check-error';
|
|
150
90
|
|
|
151
|
-
|
|
152
|
-
|
|
91
|
+
const funcThatThrows = function() { throw new TypeError('I am a TypeError') };
|
|
92
|
+
let caughtErr;
|
|
153
93
|
|
|
154
94
|
try {
|
|
155
95
|
funcThatThrows();
|
|
@@ -157,7 +97,7 @@ try {
|
|
|
157
97
|
caughtErr = e;
|
|
158
98
|
}
|
|
159
99
|
|
|
160
|
-
|
|
100
|
+
const sameInstance = caughtErr;
|
|
161
101
|
|
|
162
102
|
checkError.compatibleMessage(caughtErr, /TypeError$/); // true
|
|
163
103
|
checkError.compatibleMessage(caughtErr, 'I am a'); // true
|
|
@@ -168,10 +108,10 @@ checkError.compatibleMessage(caughtErr, 'I do not exist'); // false
|
|
|
168
108
|
#### .getConstructorName(errorLike)
|
|
169
109
|
|
|
170
110
|
```js
|
|
171
|
-
|
|
111
|
+
import * as checkError 'check-error';
|
|
172
112
|
|
|
173
|
-
|
|
174
|
-
|
|
113
|
+
const funcThatThrows = function() { throw new TypeError('I am a TypeError') };
|
|
114
|
+
let caughtErr;
|
|
175
115
|
|
|
176
116
|
try {
|
|
177
117
|
funcThatThrows();
|
|
@@ -179,7 +119,7 @@ try {
|
|
|
179
119
|
caughtErr = e;
|
|
180
120
|
}
|
|
181
121
|
|
|
182
|
-
|
|
122
|
+
const sameInstance = caughtErr;
|
|
183
123
|
|
|
184
124
|
checkError.getConstructorName(caughtErr) // 'TypeError'
|
|
185
125
|
```
|
|
@@ -187,10 +127,10 @@ checkError.getConstructorName(caughtErr) // 'TypeError'
|
|
|
187
127
|
#### .getMessage(err)
|
|
188
128
|
|
|
189
129
|
```js
|
|
190
|
-
|
|
130
|
+
import * as checkError 'check-error';
|
|
191
131
|
|
|
192
|
-
|
|
193
|
-
|
|
132
|
+
const funcThatThrows = function() { throw new TypeError('I am a TypeError') };
|
|
133
|
+
let caughtErr;
|
|
194
134
|
|
|
195
135
|
try {
|
|
196
136
|
funcThatThrows();
|
|
@@ -198,7 +138,7 @@ try {
|
|
|
198
138
|
caughtErr = e;
|
|
199
139
|
}
|
|
200
140
|
|
|
201
|
-
|
|
141
|
+
const sameInstance = caughtErr;
|
|
202
142
|
|
|
203
143
|
checkError.getMessage(caughtErr) // 'I am a TypeError'
|
|
204
144
|
```
|
package/index.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
function isErrorInstance(obj) {
|
|
2
|
-
// eslint-disable-next-line prefer-reflect
|
|
3
2
|
return obj instanceof Error || Object.prototype.toString.call(obj) === '[object Error]';
|
|
4
3
|
}
|
|
5
4
|
|
|
6
|
-
function isErrorClass(obj) {
|
|
7
|
-
return obj === Error || (typeof obj === 'function' && obj.name === 'Error');
|
|
8
|
-
}
|
|
9
|
-
|
|
10
5
|
function isRegExp(obj) {
|
|
11
|
-
// eslint-disable-next-line prefer-reflect
|
|
12
6
|
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
|
13
7
|
}
|
|
14
8
|
|
|
@@ -50,7 +44,7 @@ function compatibleConstructor(thrown, errorLike) {
|
|
|
50
44
|
if (isErrorInstance(errorLike)) {
|
|
51
45
|
// If `errorLike` is an instance of any error we compare their constructors
|
|
52
46
|
return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor;
|
|
53
|
-
} else if (
|
|
47
|
+
} else if ((typeof errorLike === 'object' || typeof errorLike === 'function') && errorLike.prototype) {
|
|
54
48
|
// If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly
|
|
55
49
|
return thrown.constructor === errorLike || thrown instanceof errorLike;
|
|
56
50
|
}
|
|
@@ -74,10 +68,13 @@ function compatibleConstructor(thrown, errorLike) {
|
|
|
74
68
|
|
|
75
69
|
function compatibleMessage(thrown, errMatcher) {
|
|
76
70
|
const comparisonString = typeof thrown === 'string' ? thrown : thrown.message;
|
|
71
|
+
if (comparisonString === undefined) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
77
74
|
if (isRegExp(errMatcher)) {
|
|
78
75
|
return errMatcher.test(comparisonString);
|
|
79
76
|
} else if (typeof errMatcher === 'string') {
|
|
80
|
-
return comparisonString.indexOf(errMatcher) !== -1;
|
|
77
|
+
return comparisonString.indexOf(errMatcher) !== -1;
|
|
81
78
|
}
|
|
82
79
|
|
|
83
80
|
return false;
|
|
@@ -104,7 +101,7 @@ function getConstructorName(errorLike) {
|
|
|
104
101
|
// of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more.
|
|
105
102
|
constructorName = errorLike.name;
|
|
106
103
|
if (constructorName === '') {
|
|
107
|
-
const newConstructorName = (new errorLike().name);
|
|
104
|
+
const newConstructorName = (new errorLike().name);
|
|
108
105
|
constructorName = newConstructorName || constructorName;
|
|
109
106
|
}
|
|
110
107
|
}
|
package/package.json
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.1.0",
|
|
3
2
|
"name": "check-error",
|
|
4
3
|
"description": "Error comparison and information related utility for node and the browser",
|
|
5
4
|
"keywords": [
|
|
@@ -27,54 +26,21 @@
|
|
|
27
26
|
"url": "git+ssh://git@github.com/chaijs/check-error.git"
|
|
28
27
|
},
|
|
29
28
|
"scripts": {
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"prepublish": "npm run build",
|
|
33
|
-
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
|
|
34
|
-
"pretest": "npm run lint && npm run build",
|
|
29
|
+
"lint": "eslint index.js test/",
|
|
30
|
+
"pretest": "npm run lint",
|
|
35
31
|
"test": "npm run test:node && npm run test:browser",
|
|
36
32
|
"test:browser": "web-test-runner",
|
|
37
33
|
"test:node": "mocha"
|
|
38
34
|
},
|
|
39
|
-
"config": {
|
|
40
|
-
"ghooks": {
|
|
41
|
-
"commit-msg": "validate-commit-msg"
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
"eslintConfig": {
|
|
45
|
-
"extends": [
|
|
46
|
-
"strict/es6"
|
|
47
|
-
],
|
|
48
|
-
"env": {
|
|
49
|
-
"es6": true
|
|
50
|
-
},
|
|
51
|
-
"globals": {
|
|
52
|
-
"HTMLElement": false
|
|
53
|
-
},
|
|
54
|
-
"rules": {
|
|
55
|
-
"complexity": "off",
|
|
56
|
-
"max-statements": "off",
|
|
57
|
-
"prefer-arrow-callback": "off",
|
|
58
|
-
"prefer-reflect": "off"
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
35
|
"devDependencies": {
|
|
62
|
-
"@
|
|
63
|
-
"@
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"eslint": "^2.4.0",
|
|
68
|
-
"eslint-config-strict": "^8.5.0",
|
|
69
|
-
"eslint-plugin-filenames": "^0.2.0",
|
|
70
|
-
"ghooks": "^1.0.1",
|
|
71
|
-
"mocha": "^9.1.2",
|
|
72
|
-
"rollup": "^2.58.0",
|
|
73
|
-
"semantic-release": "^4.3.5",
|
|
74
|
-
"simple-assert": "^2.0.0",
|
|
75
|
-
"validate-commit-msg": "^2.3.1"
|
|
36
|
+
"@eslint/js": "^9.31.0",
|
|
37
|
+
"@web/test-runner": "^0.20.2",
|
|
38
|
+
"eslint": "^9.31.0",
|
|
39
|
+
"mocha": "^11.7.1",
|
|
40
|
+
"simple-assert": "^2.0.0"
|
|
76
41
|
},
|
|
77
42
|
"engines": {
|
|
78
43
|
"node": ">= 16"
|
|
79
|
-
}
|
|
44
|
+
},
|
|
45
|
+
"version": "2.1.3"
|
|
80
46
|
}
|