• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chimurai / http-proxy-middleware / 28186656624

25 Jun 2026 04:56PM UTC coverage: 95.227% (+0.04%) from 95.183%
28186656624

Pull #1273

github

web-flow
Merge 384cdb424 into 006f1a45b
Pull Request #1273: feat(debug-proxy-errors-plugin): debug message for POST + bodyParser + ECONNRESET error

237 of 258 branches covered (91.86%)

2 of 2 new or added lines in 1 file covered. (100.0%)

7 existing lines in 1 file now uncovered.

419 of 440 relevant lines covered (95.23%)

49.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.07
/src/plugins/default/debug-proxy-errors-plugin.ts
1
import { styleText } from 'node:util';
2

3
import { Debug } from '../../debug.js';
4
import type { Plugin } from '../../types.js';
5
import { definePlugin } from '../define-plugin.js';
6

7
const debug = Debug.extend('debug-proxy-errors-plugin');
19✔
8

9
const BODY_PARSER_ERROR_MESSAGE = `[HPM] Connection reset (ECONNRESET) detected with non-empty "req.body [ERR_HPM.GH40]".
19✔
10

11
      This usually means that the request body (req.body) was already parsed before reaching the proxy.
12
      When bodyParser runs first, it consumes the request stream, leaving the proxy unable to forward the body data to the target server.
13

14
      How to fix this issue:
15
      - Option 1: Place the proxy middleware before the bodyParser middleware.
16
      - Option 2: Use 'fixRequestBody()' helper to fix this issue.
17

18
      For more details, see: https://github.com/chimurai/http-proxy-middleware/issues/40\n`;
19

20
/**
21
 * Subscribe to {@link https://github.com/unjs/httpxy#events `httpxy` error events} to prevent server from crashing.
22
 * Errors are logged with {@link https://www.npmjs.com/package/debug debug} library.
23
 */
24
export const debugProxyErrorsPlugin: Plugin = definePlugin((proxyServer, options) => {
19✔
25
  /**
26
   * The old `http-proxy` doesn't handle any errors by default (https://github.com/http-party/node-http-proxy#listening-for-proxy-events)
27
   * > We do not do any error handling of messages passed between client and proxy, and messages passed between proxy and target, so it is recommended that you listen on errors and handle them.
28
   * Subscribing to error event to prevent server from crashing
29
   */
30
  proxyServer.on('error', (error, req, res, target) => {
137✔
31
    debug(`httpxy error event: \n%O`, error);
11✔
32

33
    // detect request body (when bodyParser used) and log an error message to help debugging
34
    if (req && req.method === 'POST' && 'body' in (req ?? {})) {
11!
35
      if ((error as any).code === 'ECONNRESET') {
2✔
36
        console.error(styleText('red', BODY_PARSER_ERROR_MESSAGE));
1✔
37
      }
38
    }
39
  });
40

41
  proxyServer.on('proxyReq', (proxyReq, req, socket) => {
137✔
42
    socket.on('error', (error) => {
118✔
UNCOV
43
      debug('Socket error in proxyReq event: \n%O', error);
×
44
    });
45
  });
46

47
  /**
48
   * Fix SSE close events
49
   * @link https://github.com/chimurai/http-proxy-middleware/issues/678
50
   * @link https://github.com/http-party/node-http-proxy/issues/1520#issue-877626125
51
   */
52
  proxyServer.on('proxyRes', (proxyRes, req, res) => {
137✔
53
    res.on('close', () => {
109✔
54
      if (!res.writableEnded) {
109!
UNCOV
55
        debug('Destroying proxyRes in proxyRes close event');
×
UNCOV
56
        proxyRes.destroy();
×
57
      }
58
    });
59
  });
60

61
  /**
62
   * Fix crash when target server restarts
63
   * https://github.com/chimurai/http-proxy-middleware/issues/476#issuecomment-746329030
64
   * https://github.com/webpack/webpack-dev-server/issues/1642#issuecomment-790602225
65
   */
66
  proxyServer.on('proxyReqWs', (proxyReq, req, socket) => {
137✔
67
    socket.on('error', (error) => {
12✔
UNCOV
68
      debug('Socket error in proxyReqWs event: \n%O', error);
×
69
    });
70
  });
71

72
  proxyServer.on('open', (proxySocket) => {
137✔
73
    proxySocket.on('error', (error) => {
11✔
UNCOV
74
      debug('Socket error in open event: \n%O', error);
×
75
    });
76
  });
77

78
  proxyServer.on('close', (req, socket, head) => {
137✔
79
    socket.on('error', (error) => {
11✔
UNCOV
80
      debug('Socket error in close event: \n%O', error);
×
81
    });
82
  });
83

84
  // https://github.com/webpack/webpack-dev-server/issues/1642#issuecomment-1103136590
85
  proxyServer.on('econnreset', (error, req, res, target) => {
137✔
UNCOV
86
    debug(`httpxy econnreset event: \n%O`, error);
×
87
  });
88
});
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc