From 80101c27f8011c96cc90e9785588ae6b11ccc5a3 Mon Sep 17 00:00:00 2001 From: Matthew Warman Date: Tue, 14 Nov 2023 06:12:47 -0500 Subject: [PATCH] SLSCMN-4 middleware logger option --- src/middlewares/error-handler-http.ts | 18 ++++++++++++++---- src/middlewares/validator-joi.ts | 15 +++++++++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/middlewares/error-handler-http.ts b/src/middlewares/error-handler-http.ts index 6a83219..3ed502a 100644 --- a/src/middlewares/error-handler-http.ts +++ b/src/middlewares/error-handler-http.ts @@ -7,7 +7,7 @@ import { HttpError } from '../errors/http.error'; export type HttpErrorHandlerOptions = { defaultMessage: string; defaultStatusCode: number; - logger?(): void; + logger?(message: string): void; }; const DEFAULT_OPTIONS: HttpErrorHandlerOptions = { @@ -26,6 +26,16 @@ export const httpErrorHandler = ( ...opts, }; + /** + * Write a message to the application log. + * @param message string - The message to write. + */ + const log = (message: string): void => { + if (options.logger) { + options.logger(message); + } + }; + /** * `ServiceError` type guard. * @param error An Error. @@ -65,7 +75,7 @@ export const httpErrorHandler = ( if (isHttpError(error)) { // type HttpError - console.debug('middleware::error-handler::HttpError', error); + log(`middleware::error-handler::HttpError ${error}`); request.response = { statusCode: error.statusCode || options.defaultStatusCode, body: JSON.stringify({ @@ -77,7 +87,7 @@ export const httpErrorHandler = ( }; } else if (isServiceError(error)) { // type ServiceError - console.debug('middleware::error-handler::ServiceError', error); + log(`middleware::error-handler::ServiceError ${error}`); request.response = { statusCode: error.statusCode || options.defaultStatusCode, body: JSON.stringify({ @@ -89,7 +99,7 @@ export const httpErrorHandler = ( }; } else { // any other type of Error - console.debug('middleware::error-handler::Error', error); + log(`middleware::error-handler::Error ${error}`); request.response = { statusCode: options.defaultStatusCode, body: JSON.stringify({ diff --git a/src/middlewares/validator-joi.ts b/src/middlewares/validator-joi.ts index 06d1598..01c513f 100644 --- a/src/middlewares/validator-joi.ts +++ b/src/middlewares/validator-joi.ts @@ -7,6 +7,7 @@ import { BadRequestError } from '../errors'; */ export type ValidatorOptions = { eventSchema?: Joi.ObjectSchema; + logger?(message: string): void; }; /** @@ -23,6 +24,16 @@ const validationOptions: Joi.ValidationOptions = { * @returns Middleware to perform validation against a Joi schema. */ export const validator = (options: ValidatorOptions): MiddlewareObj => { + /** + * Write a message to the application log. + * @param message string - The message to write. + */ + const log = (message: string): void => { + if (options.logger) { + options.logger(message); + } + }; + /** * Validate a `value` using a Joi `schema`. * @param schema - Joi schema @@ -39,11 +50,11 @@ export const validator = (options: ValidatorOptions): MiddlewareObj => { * @param request - Middy request context. */ const before: middy.MiddlewareFn = (request): void => { - console.debug(`middleware::validator::before`); + log(`middleware::validator::before`); if (options.eventSchema) { const { error, value } = validate(options.eventSchema, request.event); if (error) { - console.warn(`validation error::${error}`); + log(`validation error::${error}`); throw new BadRequestError(error.message); } request.event = {