Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include error response body in thrown fetch errors #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class FetchRequestor extends Requestor {
requestInit.headers['Accept'] = 'application/json, text/javascript, */*; q=0.01';
}

return fetch(url.toString(), requestInit).then(response => {
return fetch(url.toString(), requestInit).then(async response => {
if (response.status >= 200 && response.status < 300) {
const contentType = response.headers.get('content-type');
if (isJsonDataType || (contentType && contentType.indexOf('application/json') !== -1)) {
Expand All @@ -94,7 +94,13 @@ export class FetchRequestor extends Requestor {
return response.text();
}
} else {
return Promise.reject(new AppAuthError(response.status.toString(), response.statusText));
// Extract the response's body if possible as usually contains useful error info
let body = '';
try {
body = await response.text();
body = JSON.parse(body);
} catch {}
return Promise.reject(new AppAuthError(`${response.status} ${response.statusText}`, body));
}
});
}
Expand Down