@naturalcycles/js-lib 14.133.0 → 14.134.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.
@@ -14,5 +14,5 @@ export declare class AppError<DATA_TYPE extends ErrorData = ErrorData> extends E
14
14
  * cause here is normalized to be instance of Error
15
15
  */
16
16
  cause?: Error;
17
- constructor(message: string, data?: DATA_TYPE, opt?: ErrorOptions);
17
+ constructor(message: string, data?: DATA_TYPE, opt?: ErrorOptions, name?: string);
18
18
  }
@@ -11,11 +11,18 @@ exports.AppError = void 0;
11
11
  * Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
12
12
  */
13
13
  class AppError extends Error {
14
- constructor(message, data = {}, opt) {
14
+ constructor(message, data = {}, opt, name) {
15
15
  super(message);
16
16
  Object.defineProperty(this, 'name', {
17
- value: this.constructor.name,
17
+ value: name || this.constructor.name,
18
18
  configurable: true,
19
+ writable: true,
20
+ });
21
+ // this is to allow changing this.constuctor.name to a non-minified version
22
+ Object.defineProperty(this.constructor, 'name', {
23
+ value: name || this.constructor.name,
24
+ configurable: true,
25
+ writable: true,
19
26
  });
20
27
  Object.defineProperty(this, 'data', {
21
28
  value: data,
@@ -36,4 +36,5 @@ export declare function _assertIsString(v: any, message?: string): asserts v is
36
36
  export declare function _assertIsNumber(v: any, message?: string): asserts v is number;
37
37
  export declare function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T;
38
38
  export declare class AssertionError extends AppError {
39
+ constructor(message: string, data?: {}, opt?: ErrorOptions);
39
40
  }
@@ -103,5 +103,8 @@ function _assertTypeOf(v, expectedType, message) {
103
103
  }
104
104
  exports._assertTypeOf = _assertTypeOf;
105
105
  class AssertionError extends app_error_1.AppError {
106
+ constructor(message, data = {}, opt) {
107
+ super(message, data, opt, 'AssertionError');
108
+ }
106
109
  }
107
110
  exports.AssertionError = AssertionError;
@@ -88,6 +88,12 @@ function _errorObjectToError(o, errorClass = Error) {
88
88
  Object.defineProperty(err, 'name', {
89
89
  value: o.name,
90
90
  configurable: true,
91
+ writable: true,
92
+ });
93
+ Object.defineProperty(err.constructor, 'name', {
94
+ value: o.name,
95
+ configurable: true,
96
+ writable: true,
91
97
  });
92
98
  Object.defineProperty(err, 'data', {
93
99
  value: o.data,
@@ -7,7 +7,7 @@ const app_error_1 = require("./app.error");
7
7
  */
8
8
  class HttpError extends app_error_1.AppError {
9
9
  constructor(message, data, opt) {
10
- super(message, data, opt);
10
+ super(message, data, opt, 'HttpError');
11
11
  }
12
12
  }
13
13
  exports.HttpError = HttpError;
@@ -42,6 +42,8 @@ export declare class Fetcher {
42
42
  /**
43
43
  * Default is yes,
44
44
  * unless there's reason not to (e.g method is POST).
45
+ *
46
+ * statusCode of 0 (or absense of it) will BE retried.
45
47
  */
46
48
  private shouldRetry;
47
49
  private getStatusFamily;
@@ -12,7 +12,7 @@ const time_util_1 = require("../time/time.util");
12
12
  const http_model_1 = require("./http.model");
13
13
  const defRetryOptions = {
14
14
  count: 2,
15
- timeout: 500,
15
+ timeout: 1000,
16
16
  timeoutMax: 30000,
17
17
  timeoutMultiplier: 2,
18
18
  };
@@ -253,11 +253,14 @@ class Fetcher {
253
253
  return;
254
254
  retryStatus.retryAttempt++;
255
255
  retryStatus.retryTimeout = (0, number_util_1._clamp)(retryStatus.retryTimeout * timeoutMultiplier, 0, timeoutMax);
256
- await (0, pDelay_1.pDelay)(retryStatus.retryTimeout);
256
+ const noise = Math.random() * 500;
257
+ await (0, pDelay_1.pDelay)(retryStatus.retryTimeout + noise);
257
258
  }
258
259
  /**
259
260
  * Default is yes,
260
261
  * unless there's reason not to (e.g method is POST).
262
+ *
263
+ * statusCode of 0 (or absense of it) will BE retried.
261
264
  */
262
265
  shouldRetry(res) {
263
266
  const { retryPost, retry4xx, retry5xx } = res.req;
@@ -2,6 +2,7 @@ import { AppError } from '../error/app.error';
2
2
  import type { ErrorData } from '../error/error.model';
3
3
  import type { AnyAsyncFunction } from '../types';
4
4
  export declare class TimeoutError extends AppError {
5
+ constructor(message: string, data?: {}, opt?: ErrorOptions);
5
6
  }
6
7
  export interface PTimeoutOptions {
7
8
  /**
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pTimeout = exports.pTimeoutFn = exports.TimeoutError = void 0;
4
4
  const app_error_1 = require("../error/app.error");
5
5
  class TimeoutError extends app_error_1.AppError {
6
+ constructor(message, data = {}, opt) {
7
+ super(message, data, opt, 'TimeoutError');
8
+ }
6
9
  }
7
10
  exports.TimeoutError = TimeoutError;
8
11
  /**
@@ -81,6 +81,8 @@ function _stringifyAny(obj, opt = {}) {
81
81
  }
82
82
  if ((0, error_util_1._isErrorObject)(obj)) {
83
83
  if ((0, error_util_1._isHttpErrorObject)(obj)) {
84
+ // Only include (statusCode) if it's non-zero
85
+ // No: print (0), as it removes ambiguity
84
86
  // `replace` here works ONCE, exactly as we need it
85
87
  s = s.replace('HttpError', `HttpError(${obj.data.httpStatusCode})`);
86
88
  }
@@ -8,11 +8,18 @@
8
8
  * Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
9
9
  */
10
10
  export class AppError extends Error {
11
- constructor(message, data = {}, opt) {
11
+ constructor(message, data = {}, opt, name) {
12
12
  super(message);
13
13
  Object.defineProperty(this, 'name', {
14
- value: this.constructor.name,
14
+ value: name || this.constructor.name,
15
15
  configurable: true,
16
+ writable: true,
17
+ });
18
+ // this is to allow changing this.constuctor.name to a non-minified version
19
+ Object.defineProperty(this.constructor, 'name', {
20
+ value: name || this.constructor.name,
21
+ configurable: true,
22
+ writable: true,
16
23
  });
17
24
  Object.defineProperty(this, 'data', {
18
25
  value: data,
@@ -84,4 +84,7 @@ export function _assertTypeOf(v, expectedType, message) {
84
84
  }
85
85
  }
86
86
  export class AssertionError extends AppError {
87
+ constructor(message, data = {}, opt) {
88
+ super(message, data, opt, 'AssertionError');
89
+ }
87
90
  }
@@ -79,6 +79,12 @@ export function _errorObjectToError(o, errorClass = Error) {
79
79
  Object.defineProperty(err, 'name', {
80
80
  value: o.name,
81
81
  configurable: true,
82
+ writable: true,
83
+ });
84
+ Object.defineProperty(err.constructor, 'name', {
85
+ value: o.name,
86
+ configurable: true,
87
+ writable: true,
82
88
  });
83
89
  Object.defineProperty(err, 'data', {
84
90
  value: o.data,
@@ -4,6 +4,6 @@ import { AppError } from './app.error';
4
4
  */
5
5
  export class HttpError extends AppError {
6
6
  constructor(message, data, opt) {
7
- super(message, data, opt);
7
+ super(message, data, opt, 'HttpError');
8
8
  }
9
9
  }
@@ -10,7 +10,7 @@ import { _since } from '../time/time.util';
10
10
  import { HTTP_METHODS } from './http.model';
11
11
  const defRetryOptions = {
12
12
  count: 2,
13
- timeout: 500,
13
+ timeout: 1000,
14
14
  timeoutMax: 30000,
15
15
  timeoutMultiplier: 2,
16
16
  };
@@ -291,11 +291,14 @@ export class Fetcher {
291
291
  return;
292
292
  retryStatus.retryAttempt++;
293
293
  retryStatus.retryTimeout = _clamp(retryStatus.retryTimeout * timeoutMultiplier, 0, timeoutMax);
294
- await pDelay(retryStatus.retryTimeout);
294
+ const noise = Math.random() * 500;
295
+ await pDelay(retryStatus.retryTimeout + noise);
295
296
  }
296
297
  /**
297
298
  * Default is yes,
298
299
  * unless there's reason not to (e.g method is POST).
300
+ *
301
+ * statusCode of 0 (or absense of it) will BE retried.
299
302
  */
300
303
  shouldRetry(res) {
301
304
  var _a;
@@ -1,5 +1,8 @@
1
1
  import { AppError } from '../error/app.error';
2
2
  export class TimeoutError extends AppError {
3
+ constructor(message, data = {}, opt) {
4
+ super(message, data, opt, 'TimeoutError');
5
+ }
3
6
  }
4
7
  /**
5
8
  * Decorates a Function with a timeout.
@@ -77,6 +77,8 @@ export function _stringifyAny(obj, opt = {}) {
77
77
  }
78
78
  if (_isErrorObject(obj)) {
79
79
  if (_isHttpErrorObject(obj)) {
80
+ // Only include (statusCode) if it's non-zero
81
+ // No: print (0), as it removes ambiguity
80
82
  // `replace` here works ONCE, exactly as we need it
81
83
  s = s.replace('HttpError', `HttpError(${obj.data.httpStatusCode})`);
82
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.133.0",
3
+ "version": "14.134.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -17,12 +17,20 @@ export class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
17
17
  */
18
18
  override cause?: Error
19
19
 
20
- constructor(message: string, data = {} as DATA_TYPE, opt?: ErrorOptions) {
20
+ constructor(message: string, data = {} as DATA_TYPE, opt?: ErrorOptions, name?: string) {
21
21
  super(message)
22
22
 
23
23
  Object.defineProperty(this, 'name', {
24
- value: this.constructor.name,
24
+ value: name || this.constructor.name,
25
25
  configurable: true,
26
+ writable: true,
27
+ })
28
+
29
+ // this is to allow changing this.constuctor.name to a non-minified version
30
+ Object.defineProperty(this.constructor, 'name', {
31
+ value: name || this.constructor.name,
32
+ configurable: true,
33
+ writable: true,
26
34
  })
27
35
 
28
36
  Object.defineProperty(this, 'data', {
@@ -123,4 +123,8 @@ export function _assertTypeOf<T>(v: any, expectedType: string, message?: string)
123
123
  }
124
124
  }
125
125
 
126
- export class AssertionError extends AppError {}
126
+ export class AssertionError extends AppError {
127
+ constructor(message: string, data = {}, opt?: ErrorOptions) {
128
+ super(message, data, opt, 'AssertionError')
129
+ }
130
+ }
@@ -105,6 +105,13 @@ export function _errorObjectToError<DATA_TYPE extends ErrorData, ERROR_TYPE exte
105
105
  Object.defineProperty(err, 'name', {
106
106
  value: o.name,
107
107
  configurable: true,
108
+ writable: true,
109
+ })
110
+
111
+ Object.defineProperty(err.constructor, 'name', {
112
+ value: o.name,
113
+ configurable: true,
114
+ writable: true,
108
115
  })
109
116
 
110
117
  Object.defineProperty(err, 'data', {
@@ -8,6 +8,6 @@ export class HttpError<
8
8
  DATA_TYPE extends HttpErrorData = HttpErrorData,
9
9
  > extends AppError<DATA_TYPE> {
10
10
  constructor(message: string, data: DATA_TYPE, opt?: ErrorOptions) {
11
- super(message, data, opt)
11
+ super(message, data, opt, 'HttpError')
12
12
  }
13
13
  }
@@ -30,7 +30,7 @@ import type { HttpStatusFamily } from './http.model'
30
30
 
31
31
  const defRetryOptions: FetcherRetryOptions = {
32
32
  count: 2,
33
- timeout: 500,
33
+ timeout: 1000,
34
34
  timeoutMax: 30_000,
35
35
  timeoutMultiplier: 2,
36
36
  }
@@ -336,12 +336,15 @@ export class Fetcher {
336
336
  retryStatus.retryAttempt++
337
337
  retryStatus.retryTimeout = _clamp(retryStatus.retryTimeout * timeoutMultiplier, 0, timeoutMax)
338
338
 
339
- await pDelay(retryStatus.retryTimeout)
339
+ const noise = Math.random() * 500
340
+ await pDelay(retryStatus.retryTimeout + noise)
340
341
  }
341
342
 
342
343
  /**
343
344
  * Default is yes,
344
345
  * unless there's reason not to (e.g method is POST).
346
+ *
347
+ * statusCode of 0 (or absense of it) will BE retried.
345
348
  */
346
349
  private shouldRetry(res: FetcherResponse): boolean {
347
350
  const { retryPost, retry4xx, retry5xx } = res.req
@@ -2,7 +2,11 @@ import { AppError } from '../error/app.error'
2
2
  import type { ErrorData } from '../error/error.model'
3
3
  import type { AnyAsyncFunction } from '../types'
4
4
 
5
- export class TimeoutError extends AppError {}
5
+ export class TimeoutError extends AppError {
6
+ constructor(message: string, data = {}, opt?: ErrorOptions) {
7
+ super(message, data, opt, 'TimeoutError')
8
+ }
9
+ }
6
10
 
7
11
  export interface PTimeoutOptions {
8
12
  /**
@@ -125,6 +125,8 @@ export function _stringifyAny(obj: any, opt: StringifyAnyOptions = {}): string {
125
125
 
126
126
  if (_isErrorObject(obj)) {
127
127
  if (_isHttpErrorObject(obj)) {
128
+ // Only include (statusCode) if it's non-zero
129
+ // No: print (0), as it removes ambiguity
128
130
  // `replace` here works ONCE, exactly as we need it
129
131
  s = s.replace('HttpError', `HttpError(${obj.data.httpStatusCode})`)
130
132
  }