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

如果接口:Failed to fetch错误,如何关闭连接?使用curRequestController.abort();未生效,请指教 #46

Closed
wpfBruce opened this issue Jun 1, 2023 · 8 comments

Comments

@wpfBruce
Copy link

wpfBruce commented Jun 1, 2023

const abortController = new AbortController();
const signal = abortController.signal;
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('x-client-token', token);
await fetchEventSource(Url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-client-token': ${token},
},
body: JSON.stringify({ chat_detail_id: chat_detail_id }),
signal: signal,
retry: false,
async onopen(response) {
console.log('Open:', response);
if (
response.ok &&
response.headers.get('content-type') === 'text/event-stream'
) {
console.log("everything's good");
} else if (
response.status >= 400 &&
response.status < 500 &&
response.status !== 429
) {
console.log('请求错误');
} else {
console.log('其他错误');
}
},
async onerror(error) {
console.log('Error:', error);
abortController.abort();
},
});

@wpfBruce wpfBruce changed the title 如果是Failed to fetch请求如何关闭连接,使用curRequestController.abort();未生效 如果接口:Failed to fetch错误,如何关闭连接?使用curRequestController.abort();未生效,请指教 Jun 1, 2023
@terryokay
Copy link

错误的情况,是不是自己就关闭了?

@wpfBruce
Copy link
Author

wpfBruce commented Jun 6, 2023

错误的情况,是不是自己就关了?

那如果我想不让他一直尝试连接,设置 retry: false, 我看源码里是每隔一秒就尝试连接。这个能取消吗?

@pakarsunny
Copy link

/**
* Called when there is any error making the request / processing messages /
* handling callbacks etc. Use this to control the retry strategy: if the
* error is fatal, rethrow the error inside the callback to stop the entire
* operation. Otherwise, you can return an interval (in milliseconds) after
* which the request will automatically retry (with the last-event-id).
* If this callback is not specified, or it returns undefined, fetchEventSource
* will treat every error as retriable and will try again after 1 second.
*/
源码里面说如果想要在错误的情况下停止重试,就在onerror里面再抛一个错误,告诉是错误的不再重试

@wpfBruce
Copy link
Author

wpfBruce commented Jun 8, 2023

/** * Called when there is any error making the request / processing messages / * handling callbacks etc. Use this to control the retry strategy: if the * error is fatal, rethrow the error inside the callback to stop the entire * operation. Otherwise, you can return an interval (in milliseconds) after * which the request will automatically retry (with the last-event-id). * If this callback is not specified, or it returns undefined, fetchEventSource * will treat every error as retriable and will try again after 1 second. */ 源码里面说如果想要在错误的情况下停止重试,就在onerror里面再抛一个错误,告诉是错误的不再重试

谢谢!

@Xuan-Yu-San
Copy link

Xuan-Yu-San commented Jun 19, 2023

When I leave my website, an error is thrown: "The user aborted a request."
However, this error cannot be caught by the "onerror" method, resulting in the request being fetched repeatedly.
I don't understand why a request should be aborted in the "onVisibilityChange" method.

@ZhangHanwen96
Copy link

ZhangHanwen96 commented Jun 26, 2023

onerror(err) {
        // https://github1s.com/Azure/fetch-event-source/blob/HEAD/src/fetch.ts#L132-L133
        // stop retrying
        throw err
      },

不确定是不是你想要的,需要throw err 才不会重试

@wpfBruce
Copy link
Author

onerror(err) {
        // https://github1s.com/Azure/fetch-event-source/blob/HEAD/src/fetch.ts#L132-L133
        // stop retrying
        throw err
      },

不确定不是你想要的,需要抛出错误才不会重试

谢谢,已使用抛错解决过了

@ichernev
Copy link

Yeah, this is a serious issue, bit me too: #54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants