From cc73f2d5a41f50748ce83cafd4782a4d471517e2 Mon Sep 17 00:00:00 2001 From: Luke Heath Date: Tue, 11 Jan 2022 13:52:28 -0600 Subject: [PATCH] Handle response.body if it is not JSON (#3599) --- changes/1469-improve-error-handling | 1 + cypress/integration/all/app/hosts.spec.ts | 2 +- frontend/fleet/endpoints.ts | 4 ---- frontend/fleet/request.js | 13 +++++++++---- 4 files changed, 11 insertions(+), 9 deletions(-) create mode 100644 changes/1469-improve-error-handling diff --git a/changes/1469-improve-error-handling b/changes/1469-improve-error-handling new file mode 100644 index 0000000000..7b01006a7d --- /dev/null +++ b/changes/1469-improve-error-handling @@ -0,0 +1 @@ +* Improve error handling when response body is not JSON diff --git a/cypress/integration/all/app/hosts.spec.ts b/cypress/integration/all/app/hosts.spec.ts index a1f00db4ed..f34c781168 100644 --- a/cypress/integration/all/app/hosts.spec.ts +++ b/cypress/integration/all/app/hosts.spec.ts @@ -69,7 +69,7 @@ describe( // Go to host details page cy.location("pathname").should("match", /hosts\/[0-9]/i); - cy.getAttached("span.status").should("contain", /online/i); + cy.getAttached(".status--online").should("exist"); // Run policy on host let policyname = ""; diff --git a/frontend/fleet/endpoints.ts b/frontend/fleet/endpoints.ts index 17fbdde605..70b85cb46f 100644 --- a/frontend/fleet/endpoints.ts +++ b/frontend/fleet/endpoints.ts @@ -1,7 +1,3 @@ -import { IPack } from "interfaces/pack"; -import { IScheduledQuery } from "interfaces/scheduled_query"; -import { IGlobalScheduledQuery } from "interfaces/global_scheduled_query"; - export default { ACTIVITIES: "/v1/fleet/activities", CHANGE_PASSWORD: "/v1/fleet/change_password", diff --git a/frontend/fleet/request.js b/frontend/fleet/request.js index d42278ee35..11a72397af 100644 --- a/frontend/fleet/request.js +++ b/frontend/fleet/request.js @@ -44,10 +44,15 @@ export default class Request { send() { const { endpoint, requestAttributes } = this; - return fetch(endpoint, requestAttributes).then((response) => { - return response.json().then((jsonResponse) => { - return Request.handleResponse(response, jsonResponse); - }); + return fetch(endpoint, requestAttributes).then(async (response) => { + let jsonResponse; + try { + jsonResponse = await response.json(); + } catch (error) { + console.log("Failed to parse response body as JSON: ", error); + throw response.statusText || error; + } + return Request.handleResponse(response, jsonResponse); }); } }