@sasjs/adapter 6.1.4 → 6.1.6

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.
@@ -18,6 +18,7 @@ const SAS9AuthError_1 = require("../types/errors/SAS9AuthError");
18
18
  const utils_1 = require("../utils");
19
19
  const InvalidSASjsCsrfError_1 = require("../types/errors/InvalidSASjsCsrfError");
20
20
  const host_1 = require("../utils/host");
21
+ const responseReceived_1 = require("./responseReceived");
21
22
  const isNode_1 = require("../utils/isNode");
22
23
  const mockReadiness_1 = require("./mockReadiness");
23
24
  const MockReadinessError_1 = require("../types/errors/MockReadinessError");
@@ -312,19 +313,19 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
312
313
  throw (0, error_1.prefixMessage)(err, 'Error while executing callback in handleError. ');
313
314
  });
314
315
  }
315
- throw e;
316
+ throw (0, responseReceived_1.markResponseReceived)(e, true);
316
317
  }
317
318
  else if ((response === null || response === void 0 ? void 0 : response.status) === 404) {
318
- throw new errors_1.NotFoundError(response.config.url);
319
+ throw (0, responseReceived_1.markResponseReceived)(new errors_1.NotFoundError(response.config.url), true);
319
320
  }
320
321
  else if ((response === null || response === void 0 ? void 0 : response.status) === 502) {
321
322
  if (debug)
322
- throw new errors_1.InternalServerError();
323
+ throw (0, responseReceived_1.markResponseReceived)(new errors_1.InternalServerError(), true);
323
324
  else
324
325
  return;
325
326
  }
326
327
  if (e.isAxiosError && e.code === 'UNABLE_TO_VERIFY_LEAF_SIGNATURE') {
327
- throw new errors_1.CertificateError(e.message);
328
+ throw (0, responseReceived_1.markResponseReceived)(new errors_1.CertificateError(e.message), false);
328
329
  }
329
330
  if (e.isAxiosError &&
330
331
  !response &&
@@ -349,18 +350,20 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
349
350
  }
350
351
  return yield callback();
351
352
  }
352
- catch (_b) {
353
- // Session could not be recovered — surface LoginRequiredError
354
- throw new errors_1.LoginRequiredError();
353
+ catch (retryError) {
354
+ // Session could not be recovered — surface LoginRequiredError. The
355
+ // retry itself may have reached the server and been refused, which is
356
+ // not the same as never arriving: the marker follows the retry's own
357
+ // answer, not the opaque network error that started this.
358
+ throw (0, responseReceived_1.markResponseReceived)(new errors_1.LoginRequiredError(), (0, responseReceived_1.responseWasReceived)(retryError));
355
359
  }
356
360
  finally {
357
361
  this.isRecoveringFromNetworkError = false;
358
362
  }
359
363
  }
360
364
  if (e.message)
361
- throw e;
362
- else
363
- throw (0, error_1.prefixMessage)(e, 'Error while handling error. ');
365
+ throw (0, responseReceived_1.markResponseReceived)(e, Boolean(response));
366
+ throw (0, responseReceived_1.markResponseReceived)((0, error_1.prefixMessage)(e, 'Error while handling error. '), Boolean(response));
364
367
  });
365
368
  this.createHttpClient(baseUrl, httpsAgentOptions);
366
369
  if (requestsLimit)
@@ -629,7 +632,27 @@ exports.RequestClient = RequestClient;
629
632
  * included here.
630
633
  */
631
634
  const isTokenRefreshUrl = (url) => !!url && /(^|\/)auth\/refresh(\?|$)/.test(url);
635
+ /**
636
+ * Raises the error a response status implies, marked as one built from a
637
+ * response.
638
+ *
639
+ * The marking has to happen here: every error below is a new object carrying
640
+ * the body, so the axios error - the thing that knew a response had arrived -
641
+ * is gone by the time a caller catches it (sasjs/adapter#895). A 401 from a
642
+ * refresh route is named by `throwForStatus` as a rejected refresh token
643
+ * (sasjs/adapter#975); this marks it as an answer that arrived, so a transport
644
+ * failure and a refusal are not reported with the same title.
645
+ */
632
646
  const throwIfError = (response) => {
647
+ try {
648
+ throwForStatus(response);
649
+ }
650
+ catch (error) {
651
+ throw (0, responseReceived_1.markResponseReceived)(error, true);
652
+ }
653
+ };
654
+ exports.throwIfError = throwIfError;
655
+ const throwForStatus = (response) => {
633
656
  var _a, _b, _c, _d, _e, _f, _g, _h;
634
657
  switch (response.status) {
635
658
  case 400:
@@ -681,7 +704,6 @@ const throwIfError = (response) => {
681
704
  throw error;
682
705
  }
683
706
  };
684
- exports.throwIfError = throwIfError;
685
707
  const parseError = (data) => {
686
708
  if (!data)
687
709
  return null;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Whether a failed request came back with an HTTP response.
3
+ *
4
+ * The request client turns a response into an error of its own - a 401 becomes a
5
+ * `LoginRequiredError` carrying the body, a 404 a `NotFoundError` - so the axios
6
+ * error, and with it the `response` property, is gone by the time a caller sees
7
+ * the failure. That is why this cannot be inferred at the call site from the
8
+ * error's shape: `throwIfError` has already replaced the shape.
9
+ *
10
+ * So the boundary stamps what it knows (`markResponseReceived`) and callers ask
11
+ * this module. The `response` property is still the fallback, for errors that
12
+ * never passed through the request client - but the marker wins when both are
13
+ * present, because the boundary saw the response itself rather than a rebuilt
14
+ * error that may or may not carry it.
15
+ */
16
+ /**
17
+ * Records on an error whether the request it reports on came back with a
18
+ * response. Returns the same error, so a throw site reads `throw mark(...)`.
19
+ * Marks are kept but hidden: the error travels to the caller, where a new
20
+ * enumerable field would show up in a log, a `JSON.stringify`, or a
21
+ * deep-equality assertion against an unmarked copy - a difference nothing to do
22
+ * with the request, reported as though it were.
23
+ *
24
+ * A frozen error cannot take the mark; it is returned unchanged and
25
+ * `responseWasReceived` falls back to whatever `response` the error carries.
26
+ */
27
+ export declare const markResponseReceived: <T>(error: T, received: boolean) => T;
28
+ /**
29
+ * Whether the failure behind this error came with an HTTP response.
30
+ *
31
+ * `false` is what a transport failure looks like: a wrong URL, a closed port, a
32
+ * VPN that is down, a certificate that does not verify. The message that
33
+ * accompanies such a failure is the transport's own (`connect ETIMEDOUT
34
+ * 192.0.2.1:443`), not something a server said.
35
+ */
36
+ export declare const responseWasReceived: (error: unknown) => boolean;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ /**
3
+ * Whether a failed request came back with an HTTP response.
4
+ *
5
+ * The request client turns a response into an error of its own - a 401 becomes a
6
+ * `LoginRequiredError` carrying the body, a 404 a `NotFoundError` - so the axios
7
+ * error, and with it the `response` property, is gone by the time a caller sees
8
+ * the failure. That is why this cannot be inferred at the call site from the
9
+ * error's shape: `throwIfError` has already replaced the shape.
10
+ *
11
+ * So the boundary stamps what it knows (`markResponseReceived`) and callers ask
12
+ * this module. The `response` property is still the fallback, for errors that
13
+ * never passed through the request client - but the marker wins when both are
14
+ * present, because the boundary saw the response itself rather than a rebuilt
15
+ * error that may or may not carry it.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.responseWasReceived = exports.markResponseReceived = void 0;
19
+ /** The property the request client leaves on an error it built. */
20
+ const MARKER = 'responseReceived';
21
+ /**
22
+ * Records on an error whether the request it reports on came back with a
23
+ * response. Returns the same error, so a throw site reads `throw mark(...)`.
24
+ * Marks are kept but hidden: the error travels to the caller, where a new
25
+ * enumerable field would show up in a log, a `JSON.stringify`, or a
26
+ * deep-equality assertion against an unmarked copy - a difference nothing to do
27
+ * with the request, reported as though it were.
28
+ *
29
+ * A frozen error cannot take the mark; it is returned unchanged and
30
+ * `responseWasReceived` falls back to whatever `response` the error carries.
31
+ */
32
+ const markResponseReceived = (error, received) => {
33
+ if (!error || (typeof error !== 'object' && typeof error !== 'function'))
34
+ return error;
35
+ try {
36
+ Object.defineProperty(error, MARKER, {
37
+ value: received,
38
+ enumerable: false,
39
+ configurable: true,
40
+ writable: true
41
+ });
42
+ }
43
+ catch (_a) {
44
+ // not extensible: leave it unmarked rather than fail the request twice
45
+ }
46
+ return error;
47
+ };
48
+ exports.markResponseReceived = markResponseReceived;
49
+ /**
50
+ * Whether the failure behind this error came with an HTTP response.
51
+ *
52
+ * `false` is what a transport failure looks like: a wrong URL, a closed port, a
53
+ * VPN that is down, a certificate that does not verify. The message that
54
+ * accompanies such a failure is the transport's own (`connect ETIMEDOUT
55
+ * 192.0.2.1:443`), not something a server said.
56
+ */
57
+ const responseWasReceived = (error) => {
58
+ const marked = error === null || error === void 0 ? void 0 : error.responseReceived;
59
+ if (typeof marked === 'boolean')
60
+ return marked;
61
+ return Boolean(error === null || error === void 0 ? void 0 : error.response);
62
+ };
63
+ exports.responseWasReceived = responseWasReceived;