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

fix: do not cache 200 requests with bad JSON #852

Closed
Closed
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 5.3.0

### Bug Fixes

- [#852](https://github.com/okta/okta-auth-js/pull/852) Skips non-successful requests cacheing

## 5.2.0

### Features
Expand Down
2 changes: 1 addition & 1 deletion lib/fetch/fetchRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function fetchRequest(method: string, url: string, args: FetchOptions) {
return formatResult(status, data);
})
.then(result => {
if (error) {
if (error || result.responseJSON?.error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about throwing different errors for 200 and not 200, so it can provide meaningful debugging information for both devs and our support team.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like error object already includes information about request status

// Throwing result object since error handling is done in http.js
throw result;
}
Expand Down
25 changes: 25 additions & 0 deletions test/spec/fetchRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,31 @@ describe('fetchRequest', function () {
errorSummary: 'Could not parse server response'
};

expect.assertions(1);
return fetchRequest(requestMethod, requestUrl, {})
.catch(err => {
expect(err).toEqual({
status: response.status,
responseText: JSON.stringify(errorJSON),
responseJSON: errorJSON,
responseType: 'json'
});
});
});

it('throws if JSON can not be parsed from successful response', () => {
var error = new Error('A fake error, ignore me');
response.status = 200;
response.ok = true;
response.json = function() {
return Promise.reject(error);
};

var errorJSON = {
error: error,
errorSummary: 'Could not parse server response'
};
expect.assertions(1);
return fetchRequest(requestMethod, requestUrl, {})
.catch(err => {
expect(err).toEqual({
Expand Down