Skip to content

fix(error handler): fix confusing error message #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 type * 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');
});
});