Skip to content

Commit

Permalink
feat(ui): Throw error on non-json api responses (#57634)
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed Oct 10, 2023
1 parent 3f5a647 commit 6c5aa19
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions static/app/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -488,18 +488,18 @@ export class Client {
// GET requests may not have a body
const body = method !== 'GET' ? data : undefined;

const headers = new Headers(this.headers);
const requestHeaders = new Headers(this.headers);

// Do not set the X-CSRFToken header when making a request outside of the
// current domain. Because we use subdomains we loosely compare origins
if (!csrfSafeMethod(method) && isSimilarOrigin(fullUrl, window.location.origin)) {
headers.set('X-CSRFToken', getCsrfToken());
requestHeaders.set('X-CSRFToken', getCsrfToken());
}

const fetchRequest = fetch(fullUrl, {
method,
body,
headers,
headers: requestHeaders,
credentials: this.credentials,
signal: aborter?.signal,
});
Expand Down Expand Up @@ -535,6 +535,8 @@ export class Client {

const responseContentType = response.headers.get('content-type');
const isResponseJSON = responseContentType?.includes('json');
const wasExpectingJson =
requestHeaders.get('Content-Type') === 'application/json';

const isStatus3XX = status >= 300 && status < 400;
if (status !== 204 && !isStatus3XX) {
Expand All @@ -550,6 +552,16 @@ export class Client {
// this should be an error.
ok = false;
errorReason = 'JSON parse error';
} else if (
// Empty responses from POST 201 requests are valid
responseText?.length > 0 &&
wasExpectingJson &&
error instanceof SyntaxError
) {
// Was expecting json but was returned something else. Possibly HTML.
// Ideally this would not be a 200, but we should reject the promise
ok = false;
errorReason = 'JSON parse error. Possibly returned HTML';
}
}
}
Expand Down

0 comments on commit 6c5aa19

Please sign in to comment.