@naturalcycles/js-lib 14.83.1 → 14.84.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.
@@ -1,4 +1,4 @@
1
- import { AnyFunction, CommonLogger } from '..';
1
+ import { AnyFunction, AnyObject, CommonLogger } from '..';
2
2
  export interface PRetryOptions {
3
3
  /**
4
4
  * If set - will be included in the error message.
@@ -76,6 +76,10 @@ export interface PRetryOptions {
76
76
  * @experimental
77
77
  */
78
78
  keepStackTrace?: boolean;
79
+ /**
80
+ * Will be merged with `err.data` object.
81
+ */
82
+ errorData?: AnyObject;
79
83
  }
80
84
  /**
81
85
  * Returns a Function (!), enhanced with retry capabilities.
@@ -31,7 +31,7 @@ async function pRetry(fn, opt = {}) {
31
31
  return await new Promise((resolve, reject) => {
32
32
  const rejectWithTimeout = () => {
33
33
  timedOut = true; // to prevent more tries
34
- const err = new pTimeout_1.TimeoutError(`"${fname}" timed out after ${timeout} ms`);
34
+ const err = new pTimeout_1.TimeoutError(`"${fname}" timed out after ${timeout} ms`, opt.errorData);
35
35
  if (fakeError) {
36
36
  // keep original stack
37
37
  err.stack = fakeError.stack.replace('Error: RetryError', 'TimeoutError');
@@ -75,6 +75,11 @@ async function pRetry(fn, opt = {}) {
75
75
  fakeError.stack.replace('Error: RetryError', ''),
76
76
  });
77
77
  }
78
+ ;
79
+ err.data = {
80
+ ...err.data,
81
+ ...opt.errorData,
82
+ };
78
83
  reject(err);
79
84
  }
80
85
  else {
@@ -1,5 +1,5 @@
1
1
  import { AppError } from '../error/app.error';
2
- import { AnyFunction } from '../types';
2
+ import { AnyFunction, AnyObject } from '../types';
3
3
  export declare class TimeoutError extends AppError {
4
4
  }
5
5
  export interface PTimeoutOptions {
@@ -25,6 +25,10 @@ export interface PTimeoutOptions {
25
25
  * @experimental
26
26
  */
27
27
  keepStackTrace?: boolean;
28
+ /**
29
+ * Will be merged with `err.data` object.
30
+ */
31
+ errorData?: AnyObject;
28
32
  }
29
33
  /**
30
34
  * Decorates a Function with a timeout.
@@ -37,11 +37,15 @@ async function pTimeout(promise, opt) {
37
37
  catch (err) {
38
38
  if (fakeError)
39
39
  err.stack = fakeError.stack; // keep original stack
40
+ err.data = {
41
+ ...err.data,
42
+ ...opt.errorData,
43
+ };
40
44
  reject(err);
41
45
  }
42
46
  return;
43
47
  }
44
- const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`);
48
+ const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`, opt.errorData);
45
49
  if (fakeError)
46
50
  err.stack = fakeError.stack; // keep original stack
47
51
  reject(err);
@@ -27,7 +27,7 @@ export async function pRetry(fn, opt = {}) {
27
27
  return await new Promise((resolve, reject) => {
28
28
  const rejectWithTimeout = () => {
29
29
  timedOut = true; // to prevent more tries
30
- const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`);
30
+ const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`, opt.errorData);
31
31
  if (fakeError) {
32
32
  // keep original stack
33
33
  err.stack = fakeError.stack.replace('Error: RetryError', 'TimeoutError');
@@ -71,6 +71,8 @@ export async function pRetry(fn, opt = {}) {
71
71
  fakeError.stack.replace('Error: RetryError', ''),
72
72
  });
73
73
  }
74
+ ;
75
+ err.data = Object.assign(Object.assign({}, err.data), opt.errorData);
74
76
  reject(err);
75
77
  }
76
78
  else {
@@ -32,11 +32,12 @@ export async function pTimeout(promise, opt) {
32
32
  catch (err) {
33
33
  if (fakeError)
34
34
  err.stack = fakeError.stack; // keep original stack
35
+ err.data = Object.assign(Object.assign({}, err.data), opt.errorData);
35
36
  reject(err);
36
37
  }
37
38
  return;
38
39
  }
39
- const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`);
40
+ const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`, opt.errorData);
40
41
  if (fakeError)
41
42
  err.stack = fakeError.stack; // keep original stack
42
43
  reject(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.83.1",
3
+ "version": "14.84.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,4 +1,4 @@
1
- import { _since, _stringifyAny, AnyFunction, CommonLogger } from '..'
1
+ import { _since, _stringifyAny, AnyFunction, AnyObject, AppError, CommonLogger } from '..'
2
2
  import { TimeoutError } from './pTimeout'
3
3
 
4
4
  export interface PRetryOptions {
@@ -91,6 +91,11 @@ export interface PRetryOptions {
91
91
  * @experimental
92
92
  */
93
93
  keepStackTrace?: boolean
94
+
95
+ /**
96
+ * Will be merged with `err.data` object.
97
+ */
98
+ errorData?: AnyObject
94
99
  }
95
100
 
96
101
  /**
@@ -139,7 +144,7 @@ export async function pRetry<T>(
139
144
  return await new Promise((resolve, reject) => {
140
145
  const rejectWithTimeout = () => {
141
146
  timedOut = true // to prevent more tries
142
- const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`)
147
+ const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`, opt.errorData)
143
148
  if (fakeError) {
144
149
  // keep original stack
145
150
  err.stack = fakeError.stack!.replace('Error: RetryError', 'TimeoutError')
@@ -199,6 +204,11 @@ export async function pRetry<T>(
199
204
  })
200
205
  }
201
206
 
207
+ ;(err as AppError).data = {
208
+ ...(err as AppError).data,
209
+ ...opt.errorData,
210
+ }
211
+
202
212
  reject(err)
203
213
  } else {
204
214
  // Retry after delay
@@ -1,5 +1,5 @@
1
1
  import { AppError } from '../error/app.error'
2
- import { AnyFunction } from '../types'
2
+ import { AnyFunction, AnyObject } from '../types'
3
3
 
4
4
  export class TimeoutError extends AppError {}
5
5
 
@@ -29,6 +29,11 @@ export interface PTimeoutOptions {
29
29
  * @experimental
30
30
  */
31
31
  keepStackTrace?: boolean
32
+
33
+ /**
34
+ * Will be merged with `err.data` object.
35
+ */
36
+ errorData?: AnyObject
32
37
  }
33
38
 
34
39
  /**
@@ -63,12 +68,19 @@ export async function pTimeout<T>(promise: Promise<T>, opt: PTimeoutOptions): Pr
63
68
  resolve(onTimeout())
64
69
  } catch (err: any) {
65
70
  if (fakeError) err.stack = fakeError.stack // keep original stack
71
+ err.data = {
72
+ ...err.data,
73
+ ...opt.errorData,
74
+ }
66
75
  reject(err)
67
76
  }
68
77
  return
69
78
  }
70
79
 
71
- const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`)
80
+ const err = new TimeoutError(
81
+ `"${name || 'pTimeout function'}" timed out after ${timeout} ms`,
82
+ opt.errorData,
83
+ )
72
84
  if (fakeError) err.stack = fakeError.stack // keep original stack
73
85
  reject(err)
74
86
  }, timeout)