Files
cypress/packages/proxy/lib/http/error-middleware.ts
Bill Glesias 50ceb5a924 chore: migrate @packages/extension to TypeScript and tests to Vitest (#32680)
* chore: convert extension to TypeScript and vitest

* chore: typecheck the whole package
2025-10-23 10:32:36 -04:00

71 lines
1.6 KiB
TypeScript

import errors from '@packages/errors'
import type { HttpMiddleware } from '.'
import type { Readable } from 'stream'
import { InterceptError } from '@packages/net-stubbing'
import type { Request } from '@cypress/request'
// do not use a debug namespace in this file - use the per-request `this.debug` instead
// available as cypress-verbose:proxy:http
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const debug = null
export type ErrorMiddleware = HttpMiddleware<{
error: Error
incomingResStream?: Readable
outgoingReq?: Request
}>
const LogError: ErrorMiddleware = function () {
this.debug('error proxying request %o', {
error: this.error,
url: this.req.url,
headers: this.req.headers,
})
this.next()
}
const SendToDriver: ErrorMiddleware = function () {
if (this.req.browserPreRequest) {
this.socket.toDriver('request:event', 'request:error', {
requestId: this.req.browserPreRequest.requestId,
error: errors.cloneErr(this.error),
})
}
this.next()
}
export const AbortRequest: ErrorMiddleware = function () {
if (this.outgoingReq) {
this.debug('aborting outgoingReq')
this.outgoingReq.abort()
}
this.next()
}
export const UnpipeResponse: ErrorMiddleware = function () {
if (this.incomingResStream) {
this.debug('unpiping resStream from response')
this.incomingResStream.unpipe()
}
this.next()
}
export const DestroyResponse: ErrorMiddleware = function () {
this.res.destroy()
this.end()
}
export default {
LogError,
SendToDriver,
InterceptError,
AbortRequest,
UnpipeResponse,
DestroyResponse,
}