Handle response.body if it is not JSON (#3599)

This commit is contained in:
Luke Heath 2022-01-11 13:52:28 -06:00 committed by GitHub
parent c36bf38f46
commit cc73f2d5a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 9 deletions

View file

@ -0,0 +1 @@
* Improve error handling when response body is not JSON

View file

@ -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 = "";

View file

@ -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",

View file

@ -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);
});
}
}