Skip to content

Commit

Permalink
feat(undios): support ETIMEDOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 9, 2024
1 parent 0087bf1 commit 213e6c8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

/**
Expand Down Expand Up @@ -103,6 +107,10 @@ export namespace HTTP {
}

export type Error = HTTPError

export namespace Error {
export type Code = 'ETIMEDOUT'
}
}

export interface HTTP {
Expand Down Expand Up @@ -236,11 +244,11 @@ export class HTTP extends Service<HTTP.Config> {
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)
}

Expand All @@ -263,6 +271,7 @@ export class HTTP extends Service<HTTP.Config> {
}
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
Expand Down

0 comments on commit 213e6c8

Please sign in to comment.