Repository: chimurai/http-proxy-middleware (ts)

Issue:
Bug: Error in browser shows original not target host.
### Is this a bug report?

Yes. (A mild one.)

I got referred from parcel: https://github.com/parcel-bundler/parcel/issues/5725

Version: http-proxy-middleware@1.0.6

### TL;DR

When proxy target is not running, the error message in the browser shows the origin, not the target host, which is misleading.

### Long:

I have a .proxyrc.json

```
{
  "/api": {
    "target": "http://foobar/",
    "pathRewrite": {
      "^/api": ""
    }
  }
}
```

When target is not running, I get the err in browser:

`Error occured while trying to proxy to: localhost/upload`

when opening

http://localhost/api/upload

which is misleading. I would prefer

`Error occured while trying to proxy to: foobar/upload`

Tracked it down to https://github.com/chimurai/http-proxy-middleware/blob/master/src/handlers.ts but do not know, what to change for a PR, and for which project.

Pull request description:
fix(error handler): fix confusing error message
fixes #496

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8611e0d..b1e9af0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
 ## next
 
+- fix(errorHandler): fix confusing error message ([#509](https://github.com/chimurai/http-proxy-middleware/pull/509))
 - fix(proxy): close proxy when server closes ([#508](https://github.com/chimurai/http-proxy-middleware/pull/508))
 - refactor(lodash): remove lodash ([#459](https://github.com/chimurai/http-proxy-middleware/pull/459)) ([#507](https://github.com/chimurai/http-proxy-middleware/pull/507)) ([TrySound](https://github.com/TrySound))
 - fix(ETIMEDOUT): return 504 on ETIMEDOUT ([#480](https://github.com/chimurai/http-proxy-middleware/pull/480)) ([aremishevsky](https://github.com/aremishevsky))
diff --git a/src/handlers.ts b/src/handlers.ts
index 562f542..a91fa92 100644
--- a/src/handlers.ts
+++ b/src/handlers.ts
@@ -1,3 +1,4 @@
+import type * as express from 'express';
 import camelcase = require('camelcase');
 import { getInstance } from './logger';
 const logger = getInstance();
@@ -42,7 +43,7 @@ export function getHandlers(options) {
   return handlers;
 }
 
-function defaultErrorHandler(err, req, res) {
+function defaultErrorHandler(err, req: express.Request, res: express.Response) {
   const host = req.headers && req.headers.host;
   const code = err.code;
 
@@ -63,7 +64,7 @@ function defaultErrorHandler(err, req, res) {
     }
   }
 
-  res.end('Error occured while trying to proxy to: ' + host + req.url);
+  res.end(`Error occured while trying to proxy: ${host}${req.url}`);
 }
 
 function logClose(req, socket, head) {


Test patch:
diff --git a/test/unit/handlers.spec.ts b/test/unit/handlers.spec.ts
index b64a850..792b733 100644
--- a/test/unit/handlers.spec.ts
+++ b/test/unit/handlers.spec.ts
@@ -119,7 +119,7 @@ describe('default proxy error handler', () => {
 
   it('should end the response and return error message', () => {
     proxyError(mockError, mockReq, mockRes, proxyOptions);
-    expect(errorMessage).toBe('Error occured while trying to proxy to: localhost:3000/api');
+    expect(errorMessage).toBe('Error occured while trying to proxy: localhost:3000/api');
   });
 
   it('should not set the http status code to: 500 if headers have already been sent', () => {
@@ -131,6 +131,6 @@ describe('default proxy error handler', () => {
   it('should end the response and return error message', () => {
     mockRes.headersSent = true;
     proxyError(mockError, mockReq, mockRes, proxyOptions);
-    expect(errorMessage).toBe('Error occured while trying to proxy to: localhost:3000/api');
+    expect(errorMessage).toBe('Error occured while trying to proxy: localhost:3000/api');
   });
 });