@naturalcycles/js-lib 15.86.1 → 15.86.2

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.
@@ -174,24 +174,49 @@ export function _errorSnippet(err, opt = {}) {
174
174
  .join(': ');
175
175
  }
176
176
  }
177
+ // These duck-typing checks read properties of an arbitrary object, which may invoke its
178
+ // property getters, which may throw (e.g a stateful getter, a Proxy trap).
179
+ // A type guard should never throw - an object whose properties cannot even be read safely
180
+ // is, by definition, not a well-behaved Error-like - hence try/catch returning false.
181
+ // try/catch is free in V8 unless it actually catches, so there's no performance penalty.
177
182
  export function _isBackendErrorResponseObject(o) {
178
- return _isErrorObject(o?.error);
183
+ try {
184
+ return _isErrorObject(o?.error);
185
+ }
186
+ catch {
187
+ return false;
188
+ }
179
189
  }
180
190
  export function _isHttpRequestErrorObject(o) {
181
- return !!o && o.name === 'HttpRequestError' && typeof o.data?.requestUrl === 'string';
191
+ try {
192
+ return !!o && o.name === 'HttpRequestError' && typeof o.data?.requestUrl === 'string';
193
+ }
194
+ catch {
195
+ return false;
196
+ }
182
197
  }
183
198
  /**
184
199
  * Note: any instance of AppError is also automatically an ErrorObject
185
200
  */
186
201
  export function _isErrorObject(o) {
187
- return (!!o &&
188
- typeof o === 'object' &&
189
- typeof o.name === 'string' &&
190
- typeof o.message === 'string' &&
191
- typeof o.data === 'object');
202
+ try {
203
+ return (!!o &&
204
+ typeof o === 'object' &&
205
+ typeof o.name === 'string' &&
206
+ typeof o.message === 'string' &&
207
+ typeof o.data === 'object');
208
+ }
209
+ catch {
210
+ return false;
211
+ }
192
212
  }
193
213
  export function _isErrorLike(o) {
194
- return !!o && typeof o === 'object' && typeof o.name === 'string' && typeof o.message === 'string';
214
+ try {
215
+ return (!!o && typeof o === 'object' && typeof o.name === 'string' && typeof o.message === 'string');
216
+ }
217
+ catch {
218
+ return false;
219
+ }
195
220
  }
196
221
  /**
197
222
  * Convenience function to safely add properties to Error's `data` object
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.86.1",
4
+ "version": "15.86.2",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -14,11 +14,11 @@
14
14
  }
15
15
  },
16
16
  "devDependencies": {
17
+ "@naturalcycles/dev-lib": "0.0.0",
17
18
  "@types/node": "^26",
18
19
  "@types/semver": "^7",
19
20
  "typescript": "^7",
20
- "dayjs": "^1",
21
- "@naturalcycles/dev-lib": "0.0.0"
21
+ "dayjs": "^1"
22
22
  },
23
23
  "exports": {
24
24
  ".": "./dist/index.js",
@@ -226,12 +226,26 @@ export function _errorSnippet(err: any, opt: ErrorSnippetOptions = {}): string {
226
226
  }
227
227
  }
228
228
 
229
+ // These duck-typing checks read properties of an arbitrary object, which may invoke its
230
+ // property getters, which may throw (e.g a stateful getter, a Proxy trap).
231
+ // A type guard should never throw - an object whose properties cannot even be read safely
232
+ // is, by definition, not a well-behaved Error-like - hence try/catch returning false.
233
+ // try/catch is free in V8 unless it actually catches, so there's no performance penalty.
234
+
229
235
  export function _isBackendErrorResponseObject(o: any): o is BackendErrorResponseObject {
230
- return _isErrorObject(o?.error)
236
+ try {
237
+ return _isErrorObject(o?.error)
238
+ } catch {
239
+ return false
240
+ }
231
241
  }
232
242
 
233
243
  export function _isHttpRequestErrorObject(o: any): o is ErrorObject<HttpRequestErrorData> {
234
- return !!o && o.name === 'HttpRequestError' && typeof o.data?.requestUrl === 'string'
244
+ try {
245
+ return !!o && o.name === 'HttpRequestError' && typeof o.data?.requestUrl === 'string'
246
+ } catch {
247
+ return false
248
+ }
235
249
  }
236
250
 
237
251
  /**
@@ -240,17 +254,27 @@ export function _isHttpRequestErrorObject(o: any): o is ErrorObject<HttpRequestE
240
254
  export function _isErrorObject<DATA_TYPE extends ErrorData = ErrorData>(
241
255
  o: any,
242
256
  ): o is ErrorObject<DATA_TYPE> {
243
- return (
244
- !!o &&
245
- typeof o === 'object' &&
246
- typeof o.name === 'string' &&
247
- typeof o.message === 'string' &&
248
- typeof o.data === 'object'
249
- )
257
+ try {
258
+ return (
259
+ !!o &&
260
+ typeof o === 'object' &&
261
+ typeof o.name === 'string' &&
262
+ typeof o.message === 'string' &&
263
+ typeof o.data === 'object'
264
+ )
265
+ } catch {
266
+ return false
267
+ }
250
268
  }
251
269
 
252
270
  export function _isErrorLike(o: any): o is ErrorLike {
253
- return !!o && typeof o === 'object' && typeof o.name === 'string' && typeof o.message === 'string'
271
+ try {
272
+ return (
273
+ !!o && typeof o === 'object' && typeof o.name === 'string' && typeof o.message === 'string'
274
+ )
275
+ } catch {
276
+ return false
277
+ }
254
278
  }
255
279
 
256
280
  /**