Skip to content

Commit

Permalink
Do not add method and URL prefix when logging error
Browse files Browse the repository at this point in the history
  • Loading branch information
surol committed Nov 8, 2020
1 parent 47385d9 commit 6edcbd7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/http/http-listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('httpListener', () => {
const response = await server.get('/test');

expect(await response.body()).toContain('ERROR test');
expect(logErrorSpy).toHaveBeenCalledWith('[GET /test]', error);
expect(logErrorSpy).toHaveBeenCalledWith(error);
expect(errorHandler).toHaveBeenCalledWith(expect.objectContaining({
request: expect.objectContaining({ method: 'GET', url: '/test' }),
error,
Expand All @@ -205,7 +205,7 @@ describe('httpListener', () => {
const body = await response.body();

expect(body).toContain('ERROR 404 Never Found');
expect(logErrorSpy).toHaveBeenCalledWith('[GET /test]', '404', 'Never Found');
expect(logErrorSpy).toHaveBeenCalledWith('404', 'Never Found');
expect(errorHandler).toHaveBeenCalledWith(expect.objectContaining({
request: expect.objectContaining({ method: 'GET', url: '/test' }),
error,
Expand All @@ -223,7 +223,7 @@ describe('httpListener', () => {
const response = await server.get('/test');

expect(await response.body()).toBe('');
expect(logErrorSpy).toHaveBeenCalledWith('[GET /test]', error);
expect(logErrorSpy).toHaveBeenCalledWith(error);
});
it('does not log ERROR when there is no error handler', async () => {

Expand Down Expand Up @@ -256,7 +256,7 @@ describe('httpListener', () => {
const response = await server.get('/test');

expect(await response.body()).toBe('NO RESPONSE');
expect(logErrorSpy).toHaveBeenCalledWith('[GET /test]', error);
expect(logErrorSpy).toHaveBeenCalledWith(error);
});
it('logs unhandled error', async () => {

Expand Down
9 changes: 3 additions & 6 deletions src/http/http-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,12 @@ function httpErrorHandler<TExt, TRequest extends IncomingMessage, TResponse exte
* @internal
*/
function logHttpError(
{ request: { method, url }, log, error }: RequestContext<HttpMeans & ErrorMeans & LoggerMeans>,
{ log, error }: RequestContext<HttpMeans & ErrorMeans & LoggerMeans>,
): void {

const prefix = `[${method} ${url}]`;

if (error instanceof HttpError) {
log.error(prefix, ...error.toLog());
log.error(...error.toLog());
} else {
log.error(prefix, error);
log.error(error);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/http/request/json-parsing.capability.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ describe('JsonParsing', () => {
expect(response.statusCode).toBe(400);
expect(await response.body()).toContain('Malformed JSON');
expect(errorSpy).toHaveBeenCalledWith(
expect.any(String),
'400',
'Malformed JSON',
expect.objectContaining({ message: expect.stringContaining('Unexpected token') }),
Expand Down

0 comments on commit 6edcbd7

Please sign in to comment.