ibm-cloud-sdk-core 4.0.4 → 4.0.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [4.0.6](https://github.com/IBM/node-sdk-core/compare/v4.0.5...v4.0.6) (2023-05-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * use consistent message for JSON.parse() errors ([#243](https://github.com/IBM/node-sdk-core/issues/243)) ([3cc7bab](https://github.com/IBM/node-sdk-core/commit/3cc7babac6a14b1744d64cf3ab93063b2ce63643))
7
+
8
+ ## [4.0.5](https://github.com/IBM/node-sdk-core/compare/v4.0.4...v4.0.5) (2023-03-01)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * correctly handle empty response bodies ([#239](https://github.com/IBM/node-sdk-core/issues/239)) ([d0c9cb3](https://github.com/IBM/node-sdk-core/commit/d0c9cb37b00adf4493fd46a3ae72123d6f1e33e7))
14
+
1
15
  ## [4.0.4](https://github.com/IBM/node-sdk-core/compare/v4.0.3...v4.0.4) (2023-02-27)
2
16
 
3
17
 
@@ -0,0 +1,26 @@
1
+ /**
2
+ * (C) Copyright IBM Corp. 2023.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * Given two Error instances 'error' and 'causedBy', this function will
18
+ * update 'error' by chaining 'causedBy' to it.
19
+ * Specifically, 'causedBy''s message and stack will be appended
20
+ * to 'error''s message and stack, respectively, to simulate chained Errors.
21
+ *
22
+ * @param error the Error object to be updated
23
+ * @param causedBy an Error object that represents the cause of 'error'
24
+ * @returns 'error' after updating its message and stack fields
25
+ */
26
+ export declare function chainError(error: Error, causedBy: Error): Error;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * (C) Copyright IBM Corp. 2023.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * Given two Error instances 'error' and 'causedBy', this function will
18
+ * update 'error' by chaining 'causedBy' to it.
19
+ * Specifically, 'causedBy''s message and stack will be appended
20
+ * to 'error''s message and stack, respectively, to simulate chained Errors.
21
+ *
22
+ * @param error the Error object to be updated
23
+ * @param causedBy an Error object that represents the cause of 'error'
24
+ * @returns 'error' after updating its message and stack fields
25
+ */
26
+ export function chainError(error, causedBy) {
27
+ error.message += ` ${causedBy.toString()}`;
28
+ if (causedBy.stack) {
29
+ error.stack += `\nCaused by: ${causedBy.stack}`;
30
+ }
31
+ return error;
32
+ }
@@ -13,11 +13,11 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { AxiosRequestConfig, AxiosResponse } from 'axios';
16
+ import { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
17
17
  import { CookieJar } from 'tough-cookie';
18
18
  export declare class CookieInterceptor {
19
19
  private readonly cookieJar;
20
20
  constructor(cookieJar: CookieJar | boolean);
21
- requestInterceptor(config: AxiosRequestConfig): Promise<AxiosRequestConfig<any>>;
21
+ requestInterceptor(config: InternalAxiosRequestConfig): Promise<InternalAxiosRequestConfig<any>>;
22
22
  responseInterceptor(response: AxiosResponse): Promise<AxiosResponse<any, any>>;
23
23
  }
@@ -35,6 +35,7 @@ import { buildRequestFileObject, isEmptyObject, isFileData, isFileWithMetadata,
35
35
  import logger from './logger';
36
36
  import { streamToPromise } from './stream-to-promise';
37
37
  import { CookieInterceptor } from './cookie-support';
38
+ import { chainError } from './chain-error';
38
39
  export class RequestWrapper {
39
40
  constructor(axiosOptions) {
40
41
  axiosOptions = axiosOptions || {};
@@ -462,7 +463,11 @@ function parseServiceErrorMessage(response) {
462
463
  * @throws error - if the content is meant as JSON but is malformed
463
464
  */
464
465
  function ensureJSONResponseBodyIsObject(response) {
465
- if (typeof response.data !== 'string' || !isJsonMimeType(response.headers['content-type'])) {
466
+ // If axios gave us an empty string, it is because the response had an empty body
467
+ // which can happen for a HEAD request, etc. Return the empty string in that case
468
+ if (typeof response.data !== 'string' ||
469
+ response.data === '' ||
470
+ !isJsonMimeType(response.headers['content-type'])) {
466
471
  return response.data;
467
472
  }
468
473
  // If the content is supposed to be JSON but axios gave us a string, it is most
@@ -475,9 +480,9 @@ function ensureJSONResponseBodyIsObject(response) {
475
480
  dataAsObject = JSON.parse(response.data);
476
481
  }
477
482
  catch (e) {
478
- logger.error('Response body was supposed to have JSON content but JSON parsing failed.');
479
- logger.error(`Malformed JSON string: ${response.data}`);
480
- throw e;
483
+ logger.verbose('Response body was supposed to have JSON content but JSON parsing failed.');
484
+ logger.verbose(`Malformed JSON string: ${response.data}`);
485
+ throw chainError(new Error('Error processing HTTP response:'), e);
481
486
  }
482
487
  return dataAsObject;
483
488
  }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * (C) Copyright IBM Corp. 2023.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * Given two Error instances 'error' and 'causedBy', this function will
18
+ * update 'error' by chaining 'causedBy' to it.
19
+ * Specifically, 'causedBy''s message and stack will be appended
20
+ * to 'error''s message and stack, respectively, to simulate chained Errors.
21
+ *
22
+ * @param error the Error object to be updated
23
+ * @param causedBy an Error object that represents the cause of 'error'
24
+ * @returns 'error' after updating its message and stack fields
25
+ */
26
+ export declare function chainError(error: Error, causedBy: Error): Error;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /**
3
+ * (C) Copyright IBM Corp. 2023.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.chainError = void 0;
19
+ /**
20
+ * Given two Error instances 'error' and 'causedBy', this function will
21
+ * update 'error' by chaining 'causedBy' to it.
22
+ * Specifically, 'causedBy''s message and stack will be appended
23
+ * to 'error''s message and stack, respectively, to simulate chained Errors.
24
+ *
25
+ * @param error the Error object to be updated
26
+ * @param causedBy an Error object that represents the cause of 'error'
27
+ * @returns 'error' after updating its message and stack fields
28
+ */
29
+ function chainError(error, causedBy) {
30
+ error.message += " ".concat(causedBy.toString());
31
+ if (causedBy.stack) {
32
+ error.stack += "\nCaused by: ".concat(causedBy.stack);
33
+ }
34
+ return error;
35
+ }
36
+ exports.chainError = chainError;
@@ -13,11 +13,11 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { AxiosRequestConfig, AxiosResponse } from 'axios';
16
+ import { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
17
17
  import { CookieJar } from 'tough-cookie';
18
18
  export declare class CookieInterceptor {
19
19
  private readonly cookieJar;
20
20
  constructor(cookieJar: CookieJar | boolean);
21
- requestInterceptor(config: AxiosRequestConfig): Promise<AxiosRequestConfig<any>>;
21
+ requestInterceptor(config: InternalAxiosRequestConfig): Promise<InternalAxiosRequestConfig<any>>;
22
22
  responseInterceptor(response: AxiosResponse): Promise<AxiosResponse<any, any>>;
23
23
  }
@@ -102,6 +102,7 @@ var helper_1 = require("./helper");
102
102
  var logger_1 = __importDefault(require("./logger"));
103
103
  var stream_to_promise_1 = require("./stream-to-promise");
104
104
  var cookie_support_1 = require("./cookie-support");
105
+ var chain_error_1 = require("./chain-error");
105
106
  var RequestWrapper = /** @class */ (function () {
106
107
  function RequestWrapper(axiosOptions) {
107
108
  var _this = this;
@@ -565,7 +566,11 @@ function parseServiceErrorMessage(response) {
565
566
  * @throws error - if the content is meant as JSON but is malformed
566
567
  */
567
568
  function ensureJSONResponseBodyIsObject(response) {
568
- if (typeof response.data !== 'string' || !(0, helper_1.isJsonMimeType)(response.headers['content-type'])) {
569
+ // If axios gave us an empty string, it is because the response had an empty body
570
+ // which can happen for a HEAD request, etc. Return the empty string in that case
571
+ if (typeof response.data !== 'string' ||
572
+ response.data === '' ||
573
+ !(0, helper_1.isJsonMimeType)(response.headers['content-type'])) {
569
574
  return response.data;
570
575
  }
571
576
  // If the content is supposed to be JSON but axios gave us a string, it is most
@@ -578,9 +583,9 @@ function ensureJSONResponseBodyIsObject(response) {
578
583
  dataAsObject = JSON.parse(response.data);
579
584
  }
580
585
  catch (e) {
581
- logger_1.default.error('Response body was supposed to have JSON content but JSON parsing failed.');
582
- logger_1.default.error("Malformed JSON string: ".concat(response.data));
583
- throw e;
586
+ logger_1.default.verbose('Response body was supposed to have JSON content but JSON parsing failed.');
587
+ logger_1.default.verbose("Malformed JSON string: ".concat(response.data));
588
+ throw (0, chain_error_1.chainError)(new Error('Error processing HTTP response:'), e);
584
589
  }
585
590
  return dataAsObject;
586
591
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ibm-cloud-sdk-core",
3
- "version": "4.0.4",
3
+ "version": "4.0.6",
4
4
  "description": "Core functionality to support SDKs generated with IBM's OpenAPI SDK Generator.",
5
5
  "main": "index.js",
6
6
  "typings": "./es/index.d.ts",
@@ -42,7 +42,7 @@
42
42
  "@types/isstream": "^0.1.0",
43
43
  "@types/node": "~10.14.19",
44
44
  "@types/tough-cookie": "^4.0.0",
45
- "axios": "1.2.1",
45
+ "axios": "1.4.0",
46
46
  "camelcase": "^5.3.1",
47
47
  "debug": "^4.1.1",
48
48
  "dotenv": "^6.2.0",