Skip to content

Commit

Permalink
SLSCMN-4 middleware logger option
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarman committed Nov 14, 2023
1 parent e93c721 commit 80101c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/middlewares/error-handler-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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.
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand Down
15 changes: 13 additions & 2 deletions src/middlewares/validator-joi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BadRequestError } from '../errors';
*/
export type ValidatorOptions = {
eventSchema?: Joi.ObjectSchema;
logger?(message: string): void;
};

/**
Expand All @@ -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
Expand All @@ -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 = {
Expand Down

0 comments on commit 80101c2

Please sign in to comment.