Skip to content

Commit

Permalink
feat: added new undiciError
Browse files Browse the repository at this point in the history
  • Loading branch information
Mert Can Altin committed Jun 19, 2024
1 parent 6782734 commit eb29a2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
13 changes: 13 additions & 0 deletions lib/core/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ class RequestRetryError extends UndiciError {
}
}

class ResponseError extends UndiciError {
constructor (message, code, { headers, data }) {
super(message)
this.name = 'ResponseError'
this.message = message || 'Response error'
this.code = 'UND_ERR_RESPONSE'
this.statusCode = code
this.data = data
this.headers = headers
}
}

class SecureProxyConnectionError extends UndiciError {
constructor (cause, message, options) {
super(message, { cause, ...(options ?? {}) })
Expand Down Expand Up @@ -227,5 +239,6 @@ module.exports = {
BalancedPoolMissingUpstreamError,
ResponseExceededMaxSizeError,
RequestRetryError,
ResponseError,
SecureProxyConnectionError
}
19 changes: 2 additions & 17 deletions lib/interceptor/response-error.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { parseHeaders } = require('../core/util')
const http = require('node:http')
const { DecoratorHandler } = require('undici')
const { ResponseError } = require('../core/errors')

class Handler extends DecoratorHandler {
#handler
Expand Down Expand Up @@ -66,22 +66,7 @@ class Handler extends DecoratorHandler {

this.#errored = true

let err

const stackTraceLimit = Error.stackTraceLimit
Error.stackTraceLimit = 0
try {
err = Object.assign(new Error(http.STATUS_CODES[this.#statusCode]), {
statusCode: this.#statusCode,
status: this.#statusCode,
reason: this.#body?.reason,
error: this.#body?.error,
headers: this.#headers,
body: this.#body
})
} finally {
Error.stackTraceLimit = stackTraceLimit
}
const err = new ResponseError('Response Error', this.#statusCode, this.#headers, this.#body)

if (this.opts.throwOnError !== false && this.opts.error !== false) {
this.#handler.onError(err)
Expand Down
26 changes: 9 additions & 17 deletions lib/interceptor/retry.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
'use strict'

const RetryHandler = require('../handler/retry-handler')
const createResponseErrorInterceptor = require('./response-error')

module.exports = globalOpts => {
return dispatch => {
const responseErrorInterceptor = createResponseErrorInterceptor(dispatch)

return function retryInterceptor (opts, handler) {
const wrappedHandler = {
onConnect: handler.onConnect ? handler.onConnect.bind(handler) : undefined,
onHeaders: handler.onHeaders.bind(handler),
onData: handler.onData.bind(handler),
onComplete: handler.onComplete.bind(handler),
onError: handler.onError.bind(handler)
}

const finalHandler = new RetryHandler(
{ ...opts, retryOptions: { ...globalOpts, ...opts.retryOptions } },
{ handler: wrappedHandler, dispatch }
return dispatch(
opts,
new RetryHandler(
{ ...opts, retryOptions: { ...globalOpts, ...opts.retryOptions } },
{
handler,
dispatch
}
)
)

return responseErrorInterceptor(opts, finalHandler)
}
}
}

0 comments on commit eb29a2a

Please sign in to comment.