@naturalcycles/nodejs-lib 12.63.1 → 12.64.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.
@@ -6,5 +6,6 @@ import { GetGotOptions } from './got.model';
6
6
  * 1. Error handler hook that prints helpful errors.
7
7
  * 2. Hooks that log start/end of request (optional, false by default).
8
8
  * 3. Reasonable defaults(tm), e.g non-infinite Timeout
9
+ * 4. Preserves error stack traces (!) (experimental!)
9
10
  */
10
11
  export declare function getGot(opt?: GetGotOptions): Got;
@@ -10,6 +10,7 @@ const __1 = require("..");
10
10
  * 1. Error handler hook that prints helpful errors.
11
11
  * 2. Hooks that log start/end of request (optional, false by default).
12
12
  * 3. Reasonable defaults(tm), e.g non-infinite Timeout
13
+ * 4. Preserves error stack traces (!) (experimental!)
13
14
  */
14
15
  function getGot(opt = {}) {
15
16
  opt.logger || (opt.logger = console);
@@ -40,6 +41,18 @@ function getGot(opt = {}) {
40
41
  // Which definitely doesn't fit into default "RequestTimeout"
41
42
  timeout: 60000,
42
43
  ...opt,
44
+ handlers: [
45
+ (options, next) => {
46
+ options.context = {
47
+ ...options.context,
48
+ started: Date.now(),
49
+ // This is to preserve original stack trace
50
+ // https://github.com/sindresorhus/got/blob/main/documentation/async-stack-traces.md
51
+ err: new Error('RequestError'),
52
+ };
53
+ return next(options);
54
+ },
55
+ ],
43
56
  hooks: {
44
57
  ...opt.hooks,
45
58
  beforeError: [
@@ -120,15 +133,15 @@ function gotErrorHook(opt = {}) {
120
133
  err.message = [[statusCode, method, shortUrl].filter(Boolean).join(' '), body]
121
134
  .filter(Boolean)
122
135
  .join('\n');
136
+ const stack = err.options.context?.err?.stack;
137
+ if (stack) {
138
+ err.stack += '\n --' + stack.replace('Error: RequestError', '');
139
+ }
123
140
  return err;
124
141
  };
125
142
  }
126
143
  function gotBeforeRequestHook(opt) {
127
144
  return options => {
128
- options.context = {
129
- ...options.context,
130
- started: Date.now(),
131
- };
132
145
  if (opt.logStart) {
133
146
  const { retryCount } = options.context;
134
147
  const shortUrl = getShortUrl(opt, options.url, options.prefixUrl);
@@ -54,5 +54,6 @@ export interface GotRequestContext extends AnyObject {
54
54
  * Millisecond-timestamp of when the request was started. To be able to count "time spent".
55
55
  */
56
56
  started: number;
57
+ err?: Error;
57
58
  retryCount?: number;
58
59
  }
@@ -43,5 +43,5 @@ export declare const semVerSchema: import("./string.extensions").ExtendedStringS
43
43
  export declare const userAgentSchema: import("./string.extensions").ExtendedStringSchema;
44
44
  export declare const utcOffsetSchema: import("./number.extensions").ExtendedNumberSchema;
45
45
  export declare const ipAddressSchema: import("./string.extensions").ExtendedStringSchema;
46
- export declare const baseDBEntitySchema: ObjectSchemaTyped<Partial<SavedDBEntity>, Partial<SavedDBEntity>>;
47
- export declare const savedDBEntitySchema: ObjectSchemaTyped<SavedDBEntity, SavedDBEntity>;
46
+ export declare const baseDBEntitySchema: ObjectSchemaTyped<Partial<SavedDBEntity<string>>, Partial<SavedDBEntity<string>>>;
47
+ export declare const savedDBEntitySchema: ObjectSchemaTyped<SavedDBEntity<string>, SavedDBEntity<string>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.63.1",
3
+ "version": "12.64.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
package/src/got/getGot.ts CHANGED
@@ -16,6 +16,7 @@ import { GetGotOptions, GotRequestContext } from './got.model'
16
16
  * 1. Error handler hook that prints helpful errors.
17
17
  * 2. Hooks that log start/end of request (optional, false by default).
18
18
  * 3. Reasonable defaults(tm), e.g non-infinite Timeout
19
+ * 4. Preserves error stack traces (!) (experimental!)
19
20
  */
20
21
  export function getGot(opt: GetGotOptions = {}): Got {
21
22
  opt.logger ||= console
@@ -48,6 +49,19 @@ export function getGot(opt: GetGotOptions = {}): Got {
48
49
  // Which definitely doesn't fit into default "RequestTimeout"
49
50
  timeout: 60_000,
50
51
  ...opt,
52
+ handlers: [
53
+ (options, next) => {
54
+ options.context = {
55
+ ...options.context,
56
+ started: Date.now(),
57
+ // This is to preserve original stack trace
58
+ // https://github.com/sindresorhus/got/blob/main/documentation/async-stack-traces.md
59
+ err: new Error('RequestError'),
60
+ } as GotRequestContext
61
+
62
+ return next(options)
63
+ },
64
+ ],
51
65
  hooks: {
52
66
  ...opt.hooks,
53
67
  beforeError: [
@@ -135,17 +149,17 @@ function gotErrorHook(opt: GetGotOptions = {}): BeforeErrorHook {
135
149
  .filter(Boolean)
136
150
  .join('\n')
137
151
 
152
+ const stack = (err.options.context as GotRequestContext)?.err?.stack
153
+ if (stack) {
154
+ err.stack += '\n --' + stack.replace('Error: RequestError', '')
155
+ }
156
+
138
157
  return err
139
158
  }
140
159
  }
141
160
 
142
161
  function gotBeforeRequestHook(opt: GetGotOptions): BeforeRequestHook {
143
162
  return options => {
144
- options.context = {
145
- ...options.context,
146
- started: Date.now(),
147
- } as GotRequestContext
148
-
149
163
  if (opt.logStart) {
150
164
  const { retryCount } = options.context as GotRequestContext
151
165
  const shortUrl = getShortUrl(opt, options.url, options.prefixUrl)
@@ -65,5 +65,7 @@ export interface GotRequestContext extends AnyObject {
65
65
  */
66
66
  started: number
67
67
 
68
+ err?: Error
69
+
68
70
  retryCount?: number
69
71
  }