Skip to content

Commit 58f50d4

Browse files
committed
refactor: proxy
onProxyError
1 parent 9909232 commit 58f50d4

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

index.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ function proxy (req, resOrSocket, options, onRes, onError) {
156156
return
157157
}
158158
hasError = true
159+
160+
if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') {
161+
err.statusCode = 503
162+
} else if (/HPE_INVALID/.test(err.code)) {
163+
err.statusCode = 502
164+
} else if (err.code === 'ECONNRESET') {
165+
if (!proxyReq.aborted) {
166+
err.statusCode = 502
167+
} else {
168+
return
169+
}
170+
}
171+
159172
req.removeListener('close', abort)
160173
abort()
161174
onError(err)
@@ -165,22 +178,7 @@ function proxy (req, resOrSocket, options, onRes, onError) {
165178

166179
req
167180
.pipe(proxyReq)
168-
.on('error', err => {
169-
if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') {
170-
err.statusCode = 503
171-
onProxyError(err)
172-
} else if (/HPE_INVALID/.test(err.code)) {
173-
err.statusCode = 502
174-
onProxyError(err)
175-
} else if (err.code === 'ECONNRESET') {
176-
if (!proxyReq.aborted) {
177-
err.statusCode = 502
178-
onProxyError(err)
179-
}
180-
} else {
181-
onProxyError(err)
182-
}
183-
})
181+
.on('error', onProxyError)
184182
// NOTE http.ClientRequest emits "socket hang up" error when aborted
185183
// before having received a response, i.e. there is no need to listen for
186184
// proxyReq.on('aborted', ...).
@@ -210,9 +208,7 @@ function proxy (req, resOrSocket, options, onRes, onError) {
210208
}
211209

212210
resOrSocket.writeHead(resOrSocket.statusCode)
213-
proxyRes.on('end', () => {
214-
resOrSocket.addTrailers(proxyRes.trailers)
215-
})
211+
proxyRes.on('end', () => resOrSocket.addTrailers(proxyRes.trailers))
216212
proxyRes
217213
.on('error', onProxyError)
218214
.pipe(resOrSocket)

0 commit comments

Comments
 (0)