@naturalcycles/js-lib 14.159.0 → 14.160.0

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.
@@ -69,6 +69,13 @@ export interface ErrorData {
69
69
  [k: string]: any;
70
70
  }
71
71
  export interface HttpRequestErrorData extends ErrorData {
72
+ /**
73
+ * Actual `fetch` Response as it was returned.
74
+ * Note that not every Request has a Response, hence it's optional.
75
+ *
76
+ * Non-enumerable.
77
+ */
78
+ response?: Response;
72
79
  requestUrl: string;
73
80
  requestBaseUrl?: string;
74
81
  requestMethod: HttpMethod;
@@ -87,37 +87,43 @@ exports._errorLikeToErrorObject = _errorLikeToErrorObject;
87
87
  function _errorObjectToError(o, errorClass = Error) {
88
88
  if (o instanceof errorClass)
89
89
  return o;
90
- const err = new errorClass(o.message);
90
+ // Here we pass constructor values assuming it's AppError or sub-class of it
91
+ // If not - will be checked at the next step
92
+ // We cannot check `if (errorClass instanceof AppError)`, only `err instanceof AppError`
93
+ const err = new errorClass(o.message, o.data, o.cause, o.name);
91
94
  // name: err.name, // cannot be assigned to a readonly property like this
92
95
  // stack: o.stack, // also readonly e.g in Firefox
93
- Object.defineProperty(err, 'name', {
94
- value: o.name,
95
- configurable: true,
96
- writable: true,
97
- });
98
- Object.defineProperty(err.constructor, 'name', {
99
- value: o.name,
100
- configurable: true,
101
- writable: true,
102
- });
103
- Object.defineProperty(err, 'data', {
104
- value: o.data,
105
- writable: true,
106
- configurable: true,
107
- enumerable: false,
108
- });
109
96
  if (o.stack) {
110
97
  Object.defineProperty(err, 'stack', {
111
98
  value: o.stack,
112
99
  });
113
100
  }
114
- if (o.cause) {
115
- Object.defineProperty(err, 'cause', {
116
- value: o.cause,
101
+ if (!(err instanceof __1.AppError)) {
102
+ // Following actions are only needed for non-AppError-like errors
103
+ Object.defineProperty(err, 'name', {
104
+ value: o.name,
105
+ configurable: true,
106
+ writable: true,
107
+ });
108
+ Object.defineProperty(err.constructor, 'name', {
109
+ value: o.name,
110
+ configurable: true,
111
+ writable: true,
112
+ });
113
+ Object.defineProperty(err, 'data', {
114
+ value: o.data,
117
115
  writable: true,
118
116
  configurable: true,
119
117
  enumerable: false,
120
118
  });
119
+ if (o.cause) {
120
+ Object.defineProperty(err, 'cause', {
121
+ value: o.cause,
122
+ writable: true,
123
+ configurable: true,
124
+ enumerable: false,
125
+ });
126
+ }
121
127
  }
122
128
  return err;
123
129
  }
@@ -21,6 +21,14 @@ const app_error_1 = require("./app.error");
21
21
  */
22
22
  class HttpRequestError extends app_error_1.AppError {
23
23
  constructor(message, data, cause) {
24
+ if (data.response) {
25
+ Object.defineProperty(data, 'response', {
26
+ value: data.response,
27
+ writable: true,
28
+ configurable: true,
29
+ enumerable: false,
30
+ });
31
+ }
24
32
  super(message, data, cause, 'HttpRequestError');
25
33
  }
26
34
  }
@@ -309,6 +309,7 @@ class Fetcher {
309
309
  };
310
310
  const message = [res.fetchResponse?.status, res.signature].filter(Boolean).join(' ');
311
311
  res.err = new httpRequestError_1.HttpRequestError(message, (0, object_util_1._filterNullishValues)({
312
+ response: res.fetchResponse,
312
313
  responseStatusCode: res.fetchResponse?.status || 0,
313
314
  // These properties are provided to be used in e.g custom Sentry error grouping
314
315
  // Actually, disabled now, to avoid unnecessary error printing when both msg and data are printed
@@ -316,7 +317,7 @@ class Fetcher {
316
317
  // method: req.method,
317
318
  // tryCount: req.tryCount,
318
319
  requestUrl: res.req.fullUrl,
319
- requestBaseUrl: this.cfg.baseUrl || null,
320
+ requestBaseUrl: this.cfg.baseUrl || undefined,
320
321
  requestMethod: res.req.init.method,
321
322
  requestSignature: res.signature,
322
323
  requestDuration: Date.now() - res.req.started,
@@ -543,7 +544,7 @@ class Fetcher {
543
544
  });
544
545
  if (Object.keys(searchParams).length) {
545
546
  const qs = new URLSearchParams(searchParams).toString();
546
- req.fullUrl += req.fullUrl.includes('?') ? '&' : '?' + qs;
547
+ req.fullUrl += (req.fullUrl.includes('?') ? '&' : '?') + qs;
547
548
  }
548
549
  // setup request body
549
550
  if (opt.json !== undefined) {
@@ -1,3 +1,4 @@
1
+ /// <reference lib="dom" />
1
2
  import type { CommonLogger } from '../log/commonLogger';
2
3
  import type { Promisable } from '../typeFest';
3
4
  import type { AnyObject, NumberOfMilliseconds, Reviver, UnixTimestampMillisNumber } from '../types';
@@ -1,2 +1,3 @@
1
1
  "use strict";
2
+ /// <reference lib="dom"/>
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,4 @@
1
- import { _jsonParseIfPossible, _stringifyAny } from '..';
1
+ import { AppError, _jsonParseIfPossible, _stringifyAny } from '..';
2
2
  /**
3
3
  * Useful to ensure that error in `catch (err) { ... }`
4
4
  * is indeed an Error (and not e.g `string` or `undefined`).
@@ -78,37 +78,43 @@ export function _errorLikeToErrorObject(e) {
78
78
  export function _errorObjectToError(o, errorClass = Error) {
79
79
  if (o instanceof errorClass)
80
80
  return o;
81
- const err = new errorClass(o.message);
81
+ // Here we pass constructor values assuming it's AppError or sub-class of it
82
+ // If not - will be checked at the next step
83
+ // We cannot check `if (errorClass instanceof AppError)`, only `err instanceof AppError`
84
+ const err = new errorClass(o.message, o.data, o.cause, o.name);
82
85
  // name: err.name, // cannot be assigned to a readonly property like this
83
86
  // stack: o.stack, // also readonly e.g in Firefox
84
- Object.defineProperty(err, 'name', {
85
- value: o.name,
86
- configurable: true,
87
- writable: true,
88
- });
89
- Object.defineProperty(err.constructor, 'name', {
90
- value: o.name,
91
- configurable: true,
92
- writable: true,
93
- });
94
- Object.defineProperty(err, 'data', {
95
- value: o.data,
96
- writable: true,
97
- configurable: true,
98
- enumerable: false,
99
- });
100
87
  if (o.stack) {
101
88
  Object.defineProperty(err, 'stack', {
102
89
  value: o.stack,
103
90
  });
104
91
  }
105
- if (o.cause) {
106
- Object.defineProperty(err, 'cause', {
107
- value: o.cause,
92
+ if (!(err instanceof AppError)) {
93
+ // Following actions are only needed for non-AppError-like errors
94
+ Object.defineProperty(err, 'name', {
95
+ value: o.name,
96
+ configurable: true,
97
+ writable: true,
98
+ });
99
+ Object.defineProperty(err.constructor, 'name', {
100
+ value: o.name,
101
+ configurable: true,
102
+ writable: true,
103
+ });
104
+ Object.defineProperty(err, 'data', {
105
+ value: o.data,
108
106
  writable: true,
109
107
  configurable: true,
110
108
  enumerable: false,
111
109
  });
110
+ if (o.cause) {
111
+ Object.defineProperty(err, 'cause', {
112
+ value: o.cause,
113
+ writable: true,
114
+ configurable: true,
115
+ enumerable: false,
116
+ });
117
+ }
112
118
  }
113
119
  return err;
114
120
  }
@@ -18,6 +18,14 @@ import { AppError } from './app.error';
18
18
  */
19
19
  export class HttpRequestError extends AppError {
20
20
  constructor(message, data, cause) {
21
+ if (data.response) {
22
+ Object.defineProperty(data, 'response', {
23
+ value: data.response,
24
+ writable: true,
25
+ configurable: true,
26
+ enumerable: false,
27
+ });
28
+ }
21
29
  super(message, data, cause, 'HttpRequestError');
22
30
  }
23
31
  }
@@ -295,6 +295,7 @@ export class Fetcher {
295
295
  });
296
296
  const message = [(_a = res.fetchResponse) === null || _a === void 0 ? void 0 : _a.status, res.signature].filter(Boolean).join(' ');
297
297
  res.err = new HttpRequestError(message, _filterNullishValues({
298
+ response: res.fetchResponse,
298
299
  responseStatusCode: ((_b = res.fetchResponse) === null || _b === void 0 ? void 0 : _b.status) || 0,
299
300
  // These properties are provided to be used in e.g custom Sentry error grouping
300
301
  // Actually, disabled now, to avoid unnecessary error printing when both msg and data are printed
@@ -302,7 +303,7 @@ export class Fetcher {
302
303
  // method: req.method,
303
304
  // tryCount: req.tryCount,
304
305
  requestUrl: res.req.fullUrl,
305
- requestBaseUrl: this.cfg.baseUrl || null,
306
+ requestBaseUrl: this.cfg.baseUrl || undefined,
306
307
  requestMethod: res.req.init.method,
307
308
  requestSignature: res.signature,
308
309
  requestDuration: Date.now() - res.req.started,
@@ -512,7 +513,7 @@ export class Fetcher {
512
513
  const searchParams = _filterUndefinedValues(Object.assign(Object.assign({}, this.cfg.searchParams), opt.searchParams));
513
514
  if (Object.keys(searchParams).length) {
514
515
  const qs = new URLSearchParams(searchParams).toString();
515
- req.fullUrl += req.fullUrl.includes('?') ? '&' : '?' + qs;
516
+ req.fullUrl += (req.fullUrl.includes('?') ? '&' : '?') + qs;
516
517
  }
517
518
  // setup request body
518
519
  if (opt.json !== undefined) {
@@ -1 +1,2 @@
1
+ /// <reference lib="dom"/>
1
2
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.159.0",
3
+ "version": "14.160.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -85,6 +85,14 @@ export interface ErrorData {
85
85
  }
86
86
 
87
87
  export interface HttpRequestErrorData extends ErrorData {
88
+ /**
89
+ * Actual `fetch` Response as it was returned.
90
+ * Note that not every Request has a Response, hence it's optional.
91
+ *
92
+ * Non-enumerable.
93
+ */
94
+ response?: Response
95
+
88
96
  requestUrl: string
89
97
  requestBaseUrl?: string
90
98
  requestMethod: HttpMethod
@@ -109,42 +109,49 @@ export function _errorObjectToError<DATA_TYPE extends ErrorData, ERROR_TYPE exte
109
109
  ): ERROR_TYPE {
110
110
  if (o instanceof errorClass) return o
111
111
 
112
- const err = new errorClass(o.message)
112
+ // Here we pass constructor values assuming it's AppError or sub-class of it
113
+ // If not - will be checked at the next step
114
+ // We cannot check `if (errorClass instanceof AppError)`, only `err instanceof AppError`
115
+ const err = new errorClass(o.message, o.data, o.cause, o.name)
113
116
  // name: err.name, // cannot be assigned to a readonly property like this
114
117
  // stack: o.stack, // also readonly e.g in Firefox
115
118
 
116
- Object.defineProperty(err, 'name', {
117
- value: o.name,
118
- configurable: true,
119
- writable: true,
120
- })
121
-
122
- Object.defineProperty(err.constructor, 'name', {
123
- value: o.name,
124
- configurable: true,
125
- writable: true,
126
- })
127
-
128
- Object.defineProperty(err, 'data', {
129
- value: o.data,
130
- writable: true,
131
- configurable: true,
132
- enumerable: false,
133
- })
134
-
135
119
  if (o.stack) {
136
120
  Object.defineProperty(err, 'stack', {
137
121
  value: o.stack,
138
122
  })
139
123
  }
140
124
 
141
- if (o.cause) {
142
- Object.defineProperty(err, 'cause', {
143
- value: o.cause,
125
+ if (!(err instanceof AppError)) {
126
+ // Following actions are only needed for non-AppError-like errors
127
+
128
+ Object.defineProperty(err, 'name', {
129
+ value: o.name,
130
+ configurable: true,
131
+ writable: true,
132
+ })
133
+
134
+ Object.defineProperty(err.constructor, 'name', {
135
+ value: o.name,
136
+ configurable: true,
137
+ writable: true,
138
+ })
139
+
140
+ Object.defineProperty(err, 'data', {
141
+ value: o.data,
144
142
  writable: true,
145
143
  configurable: true,
146
144
  enumerable: false,
147
145
  })
146
+
147
+ if (o.cause) {
148
+ Object.defineProperty(err, 'cause', {
149
+ value: o.cause,
150
+ writable: true,
151
+ configurable: true,
152
+ enumerable: false,
153
+ })
154
+ }
148
155
  }
149
156
 
150
157
  return err
@@ -20,6 +20,15 @@ import type { ErrorObject, HttpRequestErrorData } from './error.model'
20
20
  */
21
21
  export class HttpRequestError extends AppError<HttpRequestErrorData> {
22
22
  constructor(message: string, data: HttpRequestErrorData, cause: ErrorObject) {
23
+ if (data.response) {
24
+ Object.defineProperty(data, 'response', {
25
+ value: data.response,
26
+ writable: true,
27
+ configurable: true,
28
+ enumerable: false,
29
+ })
30
+ }
31
+
23
32
  super(message, data, cause, 'HttpRequestError')
24
33
  }
25
34
 
@@ -1,3 +1,5 @@
1
+ /// <reference lib="dom"/>
2
+
1
3
  import type { CommonLogger } from '../log/commonLogger'
2
4
  import type { Promisable } from '../typeFest'
3
5
  import type { AnyObject, NumberOfMilliseconds, Reviver, UnixTimestampMillisNumber } from '../types'
@@ -395,6 +395,7 @@ export class Fetcher {
395
395
  res.err = new HttpRequestError(
396
396
  message,
397
397
  _filterNullishValues({
398
+ response: res.fetchResponse,
398
399
  responseStatusCode: res.fetchResponse?.status || 0,
399
400
  // These properties are provided to be used in e.g custom Sentry error grouping
400
401
  // Actually, disabled now, to avoid unnecessary error printing when both msg and data are printed
@@ -402,7 +403,7 @@ export class Fetcher {
402
403
  // method: req.method,
403
404
  // tryCount: req.tryCount,
404
405
  requestUrl: res.req.fullUrl,
405
- requestBaseUrl: this.cfg.baseUrl || (null as any),
406
+ requestBaseUrl: this.cfg.baseUrl || undefined,
406
407
  requestMethod: res.req.init.method,
407
408
  requestSignature: res.signature,
408
409
  requestDuration: Date.now() - res.req.started,
@@ -660,7 +661,7 @@ export class Fetcher {
660
661
 
661
662
  if (Object.keys(searchParams).length) {
662
663
  const qs = new URLSearchParams(searchParams).toString()
663
- req.fullUrl += req.fullUrl.includes('?') ? '&' : '?' + qs
664
+ req.fullUrl += (req.fullUrl.includes('?') ? '&' : '?') + qs
664
665
  }
665
666
 
666
667
  // setup request body