mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
refactor(nbstore): improve HTTP connection timeout handling (#11985)
Closes: [BS-3344](https://linear.app/affine-design/issue/BS-3344/改进请求超时提示) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Improved internal request timeout handling for cloud operations, resulting in more robust and reliable network requests. No changes to user-facing features or functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -3,26 +3,24 @@ import { gqlFetcherFactory } from '@affine/graphql';
|
||||
|
||||
import { DummyConnection } from '../../connection';
|
||||
|
||||
const TIMEOUT = 15000;
|
||||
|
||||
export class HttpConnection extends DummyConnection {
|
||||
readonly fetch = async (input: string, init?: RequestInit) => {
|
||||
const externalSignal = init?.signal;
|
||||
if (externalSignal?.aborted) {
|
||||
throw externalSignal.reason;
|
||||
}
|
||||
const abortController = new AbortController();
|
||||
externalSignal?.addEventListener('abort', reason => {
|
||||
abortController.abort(reason);
|
||||
});
|
||||
|
||||
const timeout = 15000;
|
||||
const timeoutId = setTimeout(() => {
|
||||
abortController.abort(new Error('request timeout'));
|
||||
}, timeout);
|
||||
const signals = [AbortSignal.timeout(TIMEOUT)];
|
||||
if (externalSignal) signals.push(externalSignal);
|
||||
|
||||
const combinedSignal = AbortSignal.any(signals);
|
||||
|
||||
const res = await globalThis
|
||||
.fetch(new URL(input, this.serverBaseUrl), {
|
||||
...init,
|
||||
signal: abortController.signal,
|
||||
signal: combinedSignal,
|
||||
headers: {
|
||||
...this.requestHeaders,
|
||||
...init?.headers,
|
||||
@@ -30,16 +28,18 @@ export class HttpConnection extends DummyConnection {
|
||||
},
|
||||
})
|
||||
.catch(err => {
|
||||
const message =
|
||||
err.name === 'TimeoutError' ? 'request timeout' : err.message;
|
||||
|
||||
throw new UserFriendlyError({
|
||||
status: 504,
|
||||
code: 'NETWORK_ERROR',
|
||||
type: 'NETWORK_ERROR',
|
||||
name: 'NETWORK_ERROR',
|
||||
message: `Network error: ${err.message}`,
|
||||
message: `Network error: ${message}`,
|
||||
stacktrace: err.stack,
|
||||
});
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
if (!res.ok && res.status !== 404) {
|
||||
if (res.status === 413) {
|
||||
throw new UserFriendlyError({
|
||||
|
||||
Reference in New Issue
Block a user