@dynatrace-sdk/http-client 0.4.11 → 1.0.4

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/esm/index.js CHANGED
@@ -221,7 +221,7 @@ var HttpClientAbortError = class extends Error {
221
221
  name = "AbortError";
222
222
  };
223
223
  var throwIfAbortError = (e) => {
224
- if (e instanceof DOMException && e.name === "AbortError") {
224
+ if (e instanceof Error && e.name === "AbortError") {
225
225
  throw new HttpClientAbortError(e);
226
226
  }
227
227
  };
@@ -229,7 +229,9 @@ var throwIfAbortError = (e) => {
229
229
  // packages/http-client/src/lib/platform/http-client-request-error.ts
230
230
  var HttpClientRequestError = class extends BaseError {
231
231
  constructor(cause) {
232
- super("Request failed.");
232
+ super(
233
+ typeof cause?.message === "string" ? `Request failed: ${cause.message}` : "Request failed."
234
+ );
233
235
  this.cause = cause;
234
236
  }
235
237
  name = "RequestError";
@@ -287,6 +289,14 @@ async function decodeBlobResponseBody(response) {
287
289
  function decodeReadableStreamResponseBody(response) {
288
290
  return response.body !== null ? response.body : emptyReadableStream();
289
291
  }
292
+ function throwResponseBodyTypeError(response, responseBodyType, cause) {
293
+ throw new DataTypeError(
294
+ `Response body does not conform to the specified response body type: '${responseBodyType}'. The content type of the response is '${response.headers.get(
295
+ "Content-Type"
296
+ )}'.`,
297
+ cause
298
+ );
299
+ }
290
300
  async function decodeAsyncResponseBody(response, responseBodyType) {
291
301
  try {
292
302
  switch (responseBodyType) {
@@ -305,10 +315,7 @@ async function decodeAsyncResponseBody(response, responseBodyType) {
305
315
  }
306
316
  } catch (e) {
307
317
  throwIfAbortError(e);
308
- throw new DataTypeError(
309
- `Response body does not conform to the specified response body type: '${responseBodyType}'.`,
310
- e
311
- );
318
+ throwResponseBodyTypeError(response, responseBodyType, e);
312
319
  }
313
320
  }
314
321
  function decodeSyncResponseBody(response, responseBodyType) {
@@ -316,10 +323,7 @@ function decodeSyncResponseBody(response, responseBodyType) {
316
323
  return decodeReadableStreamResponseBody(response);
317
324
  } catch (e) {
318
325
  throwIfAbortError(e);
319
- throw new DataTypeError(
320
- `Response body does not conform to the specified response body type: '${responseBodyType}'.`,
321
- e
322
- );
326
+ throwResponseBodyTypeError(response, responseBodyType, e);
323
327
  }
324
328
  }
325
329
  function decodeResponseBodyImpl(response, responseBodyType) {
@@ -378,28 +382,56 @@ import {
378
382
  ErrorType,
379
383
  isGlobalErrorSerializerSupported
380
384
  } from "@dynatrace-sdk/error-handlers";
381
- async function getMessage(response) {
382
- const consumedBody = await response.body("text");
385
+ function getMessageFromObject(obj) {
386
+ if (typeof obj === "object" && obj !== null && typeof obj.message === "string") {
387
+ return obj.message;
388
+ }
389
+ }
390
+ async function parseResponse(response) {
391
+ let consumedBody;
392
+ try {
393
+ consumedBody = await response.body("text");
394
+ } catch (e) {
395
+ const bodyError = `Response body is not available. ${e?.message ?? "Unknown error occurred."}`;
396
+ return {
397
+ message: bodyError.substring(0, 100),
398
+ body: bodyError
399
+ };
400
+ }
383
401
  try {
384
402
  const jsonBody = JSON.parse(consumedBody);
385
- if (jsonBody.message && typeof jsonBody.message === "string") {
386
- return jsonBody.message;
403
+ if (typeof jsonBody === "object" && jsonBody !== null) {
404
+ return {
405
+ message: getMessageFromObject(jsonBody.error) || getMessageFromObject(jsonBody) || consumedBody.substring(0, 100),
406
+ body: jsonBody,
407
+ errorRef: jsonBody.error?.details?.errorRef
408
+ };
409
+ } else {
410
+ return {
411
+ message: consumedBody.substring(0, 100),
412
+ body: consumedBody
413
+ };
387
414
  }
388
415
  } catch (e) {
389
- return consumedBody.substring(0, 100);
416
+ return {
417
+ message: consumedBody.substring(0, 100),
418
+ body: consumedBody
419
+ };
390
420
  }
391
- return consumedBody.substring(0, 100);
392
421
  }
393
422
  var httpClientResponseErrorSerializer = async (error) => {
394
423
  if (error instanceof HttpClientResponseError) {
395
424
  const status = error.response.status;
396
- const message = await getMessage(error.response);
425
+ const { message, body, errorRef } = await parseResponse(error.response);
426
+ const optionalErrorRef = errorRef ? { errorRef } : {};
397
427
  return {
398
428
  name: error.name,
399
429
  status,
400
430
  message,
401
431
  stack: error.stack,
402
- type: ErrorType.HTTP
432
+ type: ErrorType.HTTP,
433
+ body,
434
+ ...optionalErrorRef
403
435
  };
404
436
  }
405
437
  };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@dynatrace-sdk/http-client",
3
- "version": "0.4.11",
3
+ "version": "1.0.4",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@dynatrace-sdk/error-handlers": "^0.2.7"
6
+ "@dynatrace-sdk/error-handlers": "^1.1.0"
7
7
  },
8
8
  "main": "./cjs/index.js",
9
9
  "module": "./esm/index.js",
@@ -1 +1,3 @@
1
+ import { GlobalErrorSerializer } from '@dynatrace-sdk/error-handlers';
2
+ export declare const httpClientResponseErrorSerializer: GlobalErrorSerializer;
1
3
  export declare function addHttpClientResponseErrorSerializer(): void;
@@ -1,5 +0,0 @@
1
- {
2
- "docs": {
3
- "title": "http-client"
4
- }
5
- }