From 213e6c87ffbc627230801fc553f0b280c206548e Mon Sep 17 00:00:00 2001 From: Shigma Date: Sat, 9 Mar 2024 11:11:17 +0800 Subject: [PATCH] feat(undios): support ETIMEDOUT --- packages/core/src/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 11a5eff..36f8958 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -30,6 +30,10 @@ class HTTPError extends Error { static is(error: any): error is HTTPError { return !!error?.[kHTTPError] } + + constructor(message?: string, public code?: HTTP.Error.Code) { + super(message) + } } /** @@ -103,6 +107,10 @@ export namespace HTTP { } export type Error = HTTPError + + export namespace Error { + export type Code = 'ETIMEDOUT' + } } export interface HTTP { @@ -236,11 +244,11 @@ export class HTTP extends Service { let timer: NodeJS.Timeout | number | undefined const dispose = caller.on('dispose', () => { clearTimeout(timer) - controller.abort(new Error('context disposed')) + controller.abort(new HTTPError('context disposed', 'ETIMEDOUT')) }) if (config.timeout) { timer = setTimeout(() => { - controller.abort(new Error('timeout')) + controller.abort(new HTTPError('request timeout', 'ETIMEDOUT')) }, config.timeout) } @@ -263,6 +271,7 @@ export class HTTP extends Service { } caller.emit('http/fetch-init', url, init, config) const raw = await fetch(url, init).catch((cause) => { + if (HTTP.Error.is(cause)) throw cause const error = new HTTP.Error(`fetch ${url} failed`) error.cause = cause throw error