forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-middleware.ts
More file actions
70 lines (57 loc) · 1.6 KB
/
error-middleware.ts
File metadata and controls
70 lines (57 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as errors from '@packages/server/lib/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,
}