Skip to content

Commit

Permalink
fix(error handler): fix confusing error message
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Mar 28, 2021
1 parent be99bc4 commit ce79555
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as express from 'express';
import camelcase = require('camelcase');
import { getInstance } from './logger';
const logger = getInstance();
Expand Down Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
});
});

0 comments on commit ce79555

Please sign in to comment.