-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
fetch exception should be rethrowed in kfetch's promise #21982
Comments
@elastic/kibana-platform And @spalger in particular since this seems to be impacting code search |
@sqren any idea how this might impact the work to add interceptors to kfetch? |
@spalger not 100%. First need to be sure I understand the question completely. const abortController = new AbortController()
kfetch({
pathname: .....,
signal: abortController.signal
}).catch(...)
abortController.abort()
How is that different from now? If new Promise((resolve, reject) => {
throw new Error('throwing')
})
new Promise((resolve, reject) => {
reject(new Error('rejecting'))
}) Again, sorry if I'm missing something. Please let me know! :) Btw: there is also |
Ping @spacedragon |
Here are some codes from codesearch, I added 2 public async sendRequest(
method: string,
params: any,
signal?: AbortSignal
): Promise<ResponseMessage> {
try {
console.log('invoking kfetch');
const response = await kfetch({
pathname: `${this.baseUri}/${method}`,
method: 'POST',
body: JSON.stringify(params),
signal,
});
return response as ResponseMessage;
} catch (e) {
console.log('error from kfetch', e);
const error = e.body;
throw new ResponseError<any>(error.code, error.message, error.data);
}
} I thought "error from kfetch" would be printed out, but it didn't. Our code couldn't catch the |
Here are some codes from kfetch, I'm adding some comments : const fetching = new Promise<any>(async (resolve, reject) => {
// I think problem happens here, when an AbortController cancel a fetch request
// a AbortError throws from the following line
const res = await fetch(fullUrl, combinedFetchOptions);
// thus no response is returned, code won't reach here.
if (res.ok) {
return resolve(await res.json());
}
...
// this promise won't catch any AbortError because the Exception throws before here.
return fetching; I think this might fix the problem: const fetching = new Promise<any>(async (resolve, reject) => {
let res;
try {
res = await fetch(fullUrl, combinedFetchOptions);
} catch(error) {
reject(error)
}
if (res && res.ok) {
return resolve(await res.json());
}
... |
I'm not sure whether my previous comment is true. I read the doc from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/promise#Syntax.
If Promise works this way, why can't I catch the error? |
@spacedragon
|
Describe the bug:
It is possible that fetch throw an exception:
I think this line should be wrapped in a try-catch block and re-throw using the
reject
method.kibana/src/ui/public/kfetch/kfetch.ts
Line 70 in b434652
Steps to reproduce:
Expected behavior:
Errors in browser console (if relevant):
Any additional context:
In CodeSearch project, we are trying to attach a AbortController signal to let user be able to cancel a fetch. When a fetch is aborted, it throws a AbortError.
See here:
https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
The text was updated successfully, but these errors were encountered: