Skip to content

Commit

Permalink
feat: make errors loggable (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianNoise authored May 10, 2023
1 parent cbeb138 commit 261864d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/handlersjs-http/lib/handlers/error.handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { getLogger } from '@digita-ai/handlersjs-logging';
import { getLogger, makeErrorLoggable } from '@digita-ai/handlersjs-logging';
import { HttpHandlerResponse } from '../models/http-handler-response';
import { HttpHandler } from '../models/http-handler';
import { HttpHandlerContext } from '../models/http-handler-context';
Expand Down Expand Up @@ -75,7 +75,7 @@ export class ErrorHandler implements HttpHandler {
return this.nestedHandler.handle(context).pipe(
catchError((error) => {

this.logger.warn('Error occurred: ', { error });
this.logger.warn('Error occurred: ', { error: makeErrorLoggable(error) });

const status = error?.statusCode ?? error.status;
const message = error?.message ?? error.body;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Observable, of, Subject, throwError } from 'rxjs';
import { map, switchMap, toArray, catchError } from 'rxjs/operators';
import { v4 } from 'uuid';
import { getLogger } from '@digita-ai/handlersjs-logging';
import { getLogger, makeErrorLoggable } from '@digita-ai/handlersjs-logging';
import { BadRequestHttpError } from '../../errors/bad-request-http-error';
import { HttpHandler } from '../../models/http-handler';
import { HttpHandlerContext } from '../../models/http-handler-context';
Expand Down Expand Up @@ -204,7 +204,7 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {
const status = error?.statusCode ?? error.status;
const message = error?.message ?? error.body;

this.logger.warn(`${error.name}:`, { error });
this.logger.warn(`Unhandled error is handled by Handlersjs :`, { error: makeErrorLoggable(error) });

return of({ headers: {}, ... error, body: message ?? 'Internal Server Error', status: statusCodes[status] ? status : 500 });

Expand Down
1 change: 1 addition & 0 deletions packages/handlersjs-logging/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './models/logger-options';
export * from './loggers/logger';
export * from './loggers/console-logger';
export * from './loggers/pino-logger';
export * from './util/make-error-loggable';
export * from './main';
36 changes: 36 additions & 0 deletions packages/handlersjs-logging/lib/util/make-error-loggable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { makeErrorLoggable } from './make-error-loggable';

describe('makeErrorLoggable', () => {

it('should return a set values when passed something that is not an Error', async () => {

expect(makeErrorLoggable('foo')).toEqual({ 'error': 'not-an-error' });

});

it('should return a parsed object when passed an Error', async () => {

const error = new Error('foo');

expect(makeErrorLoggable(error)).toEqual({
message: 'foo',
name: 'Error',
stack: expect.any(Array),
});

});

it('should return an empty stack when passed an Error without a stack', async () => {

const error = new Error('foo');
delete error.stack;

expect(makeErrorLoggable(error)).toEqual({
message: 'foo',
name: 'Error',
stack: [],
});

});

});
15 changes: 15 additions & 0 deletions packages/handlersjs-logging/lib/util/make-error-loggable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const makeErrorLoggable = (error: unknown): Record<string, unknown> => {

if (error instanceof Error) {

return {
message: error.message,
name: error.name,
stack: error.stack ? error.stack.split(/\n/) : [],
};

}

return { 'error': 'not-an-error' };

};

0 comments on commit 261864d

Please sign in to comment.