-
Notifications
You must be signed in to change notification settings - Fork 70
/
error.js
211 lines (196 loc) · 6.29 KB
/
error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/// <reference types="ses"/>
import { X, q } from '@endo/errors';
import { assertChecker } from './passStyle-helpers.js';
/** @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper */
/** @typedef {import('./types.js').Checker} Checker */
const { getPrototypeOf, getOwnPropertyDescriptors, hasOwn, entries } = Object;
// TODO: Maintenance hazard: Coordinate with the list of errors in the SES
// whilelist.
const errorConstructors = new Map(
// Cast because otherwise TS is confused by AggregateError
// See https://github.com/endojs/endo/pull/2042#discussion_r1484933028
/** @type {Array<[string, import('ses').GenericErrorConstructor]>} */
([
['Error', Error],
['EvalError', EvalError],
['RangeError', RangeError],
['ReferenceError', ReferenceError],
['SyntaxError', SyntaxError],
['TypeError', TypeError],
['URIError', URIError],
// https://github.com/endojs/endo/issues/550
// To accommodate platforms prior to AggregateError, we comment out the
// following line and instead conditionally add it to the map below.
// ['AggregateError', AggregateError],
]),
);
if (typeof AggregateError !== 'undefined') {
// Conditional, to accommodate platforms prior to AggregateError
errorConstructors.set('AggregateError', AggregateError);
}
/**
* Because the error constructor returned by this function might be
* `AggregateError`, which has different construction parameters
* from the other error constructors, do not use it directly to try
* to make an error instance. Rather, use `makeError` which encapsulates
* this non-uniformity.
*
* @param {string} name
* @returns {import('ses').GenericErrorConstructor | undefined}
*/
export const getErrorConstructor = name => errorConstructors.get(name);
harden(getErrorConstructor);
/**
* @param {unknown} candidate
* @param {Checker} [check]
* @returns {boolean}
*/
const checkErrorLike = (candidate, check = undefined) => {
const reject = !!check && ((T, ...subs) => check(false, X(T, ...subs)));
// TODO: Need a better test than instanceof
return (
candidate instanceof Error ||
(reject && reject`Error expected: ${candidate}`)
);
};
harden(checkErrorLike);
/// <reference types="ses"/>
/**
* Validating error objects are passable raises a tension between security
* vs preserving diagnostic information. For errors, we need to remember
* the error itself exists to help us diagnose a bug that's likely more
* pressing than a validity bug in the error itself. Thus, whenever it is safe
* to do so, we prefer to let the error-like test succeed and to couch these
* complaints as notes on the error.
*
* To resolve this, such a malformed error object will still pass
* `isErrorLike` so marshal can use this for top level error to report from,
* even if it would not actually validate.
* Instead, the diagnostics that `assertError` would have reported are
* attached as notes to the malformed error. Thus, a malformed
* error is passable by itself, but not as part of a passable structure.
*
* @param {unknown} candidate
* @returns {boolean}
*/
export const isErrorLike = candidate => checkErrorLike(candidate);
harden(isErrorLike);
/**
* @param {string} propName
* @param {PropertyDescriptor} desc
* @param {import('./internal-types.js').PassStyleOf} passStyleOfRecur
* @param {Checker} [check]
* @returns {boolean}
*/
export const checkRecursivelyPassableErrorPropertyDesc = (
propName,
desc,
passStyleOfRecur,
check = undefined,
) => {
const reject = !!check && ((T, ...subs) => check(false, X(T, ...subs)));
if (desc.enumerable) {
return (
reject &&
reject`Passable Error ${q(
propName,
)} own property must not be enumerable: ${desc}`
);
}
if (!hasOwn(desc, 'value')) {
return (
reject &&
reject`Passable Error ${q(
propName,
)} own property must be a data property: ${desc}`
);
}
const { value } = desc;
switch (propName) {
case 'message':
case 'stack': {
return (
typeof value === 'string' ||
(reject &&
reject`Passable Error ${q(
propName,
)} own property must be a string: ${value}`)
);
}
case 'cause': {
// eslint-disable-next-line no-use-before-define
return checkRecursivelyPassableError(value, passStyleOfRecur, check);
}
case 'errors': {
if (!Array.isArray(value) || passStyleOfRecur(value) !== 'copyArray') {
return (
reject &&
reject`Passable Error ${q(
propName,
)} own property must be a copyArray: ${value}`
);
}
return value.every(err =>
// eslint-disable-next-line no-use-before-define
checkRecursivelyPassableError(err, passStyleOfRecur, check),
);
}
default: {
break;
}
}
return (
reject && reject`Passable Error has extra unpassed property ${q(propName)}`
);
};
harden(checkRecursivelyPassableErrorPropertyDesc);
/**
* @param {unknown} candidate
* @param {import('./internal-types.js').PassStyleOf} passStyleOfRecur
* @param {Checker} [check]
* @returns {boolean}
*/
export const checkRecursivelyPassableError = (
candidate,
passStyleOfRecur,
check = undefined,
) => {
const reject = !!check && ((T, ...subs) => check(false, X(T, ...subs)));
if (!checkErrorLike(candidate, check)) {
return false;
}
const proto = getPrototypeOf(candidate);
const { name } = proto;
const errConstructor = getErrorConstructor(name);
if (errConstructor === undefined || errConstructor.prototype !== proto) {
return (
reject &&
reject`Passable Error must inherit from an error class .prototype: ${candidate}`
);
}
const descs = getOwnPropertyDescriptors(candidate);
if (!('message' in descs)) {
return (
reject &&
reject`Passable Error must have an own "message" string property: ${candidate}`
);
}
return entries(descs).every(([propName, desc]) =>
checkRecursivelyPassableErrorPropertyDesc(
propName,
desc,
passStyleOfRecur,
check,
),
);
};
harden(checkRecursivelyPassableError);
/**
* @type {PassStyleHelper}
*/
export const ErrorHelper = harden({
styleName: 'error',
canBeValid: checkErrorLike,
assertValid: (candidate, passStyleOfRecur) =>
checkRecursivelyPassableError(candidate, passStyleOfRecur, assertChecker),
});