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

Core HTTP fetch returns incorrect error when a request is aborted #56244

Closed
lukasolson opened this issue Jan 28, 2020 · 7 comments · Fixed by #57550
Closed

Core HTTP fetch returns incorrect error when a request is aborted #56244

lukasolson opened this issue Jan 28, 2020 · 7 comments · Fixed by #57550
Assignees
Labels
bug Fixes for quality problems that affect the customer experience Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more!

Comments

@lukasolson
Copy link
Member

lukasolson commented Jan 28, 2020

Steps to reproduce:

const abortController = new AbortController();
const { signal } = abortController;
const promise = core.http.fetch({
  path: '/path/to/some/route',
  method: 'GET',
  signal
});
setTimeout(() => abortController.abort(), 5);
await promise;

You'd expect the error to be thrown here to be a DOM AbortError, but instead you get TypeError: Cannot read property 'credentials' of undefined.

It looks like we're not properly checking if request exists in the unauthorized response HTTP interceptor. This probably happened here, since we were no longer throwing our own error, but forwarding along the error from the DOM.

@lukasolson lukasolson added bug Fixes for quality problems that affect the customer experience Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more! labels Jan 28, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform (Team:Platform)

@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-security (Team:Security)

@kobelb
Copy link
Contributor

kobelb commented Jan 28, 2020

While it is possible to add a check to

if (httpErrorResponse.request.credentials === 'omit') {
to ensure the request exists before checking request.credentials === 'omit', I'm hesitant to do so. If there is a "response error", a "request" conceptually has to exist and the types for the HttpInterceptorResponseError have the error always being available.

@joshdover
Copy link
Contributor

I agree with @kobelb, this appears to be a bug in the Core HttpService. The types are not matching the runtime results. We'll prioritize fixing this on the Platform team.

@pgayvallet
Copy link
Contributor

try {
response = await window.fetch(request);
} catch (err) {
if (err.name === 'AbortError') {
throw err;
} else {
throw new HttpFetchError(err.message, request);
}
}

If I remember correctly, this was done to avoid loosing the AbortError type when a request is canceled, as it allowed to check this way if the request was aborted from the calling code. Not sure how to keep this behavior if we don't return the original error in that case.

  • Maybe constructing an error and setting it's name to AbortError is enough?
  • Or maybe we consolidate the original AbortError by injecting the request to it?

@lukasolson
Copy link
Member Author

If I remember correctly, this was done to avoid loosing the AbortError type when a request is canceled, as it allowed to check this way if the request was aborted from the calling code. Not sure how to keep this behavior if we don't return the original error in that case.

Yes, that's correct.

Maybe constructing an error and setting it's name to AbortError is enough?
Or maybe we consolidate the original AbortError by injecting the request to it?

I think either of these approaches would work for our purposes in handling aborted requests.

@joshdover joshdover self-assigned this Feb 11, 2020
@pgayvallet pgayvallet assigned pgayvallet and unassigned joshdover Feb 12, 2020
@pgayvallet
Copy link
Contributor

From a quick inspection, every check agains AbortError in the codebase are done with

if (error.name === 'AbortError') {...}

For consistency, adding a name attribute to our HttpFetchError and IHttpFetchError types is probably the best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants