feat: Add BooleanHandler

Co-Authored-By: Ludovico Granata <Ludogranata@gmail.com>
This commit is contained in:
Simone Persiani
2021-08-17 17:15:21 +02:00
committed by Joachim Van Herwegen
parent 0355673a0f
commit 73867f0827
7 changed files with 279 additions and 87 deletions

View File

@@ -292,6 +292,7 @@ export * from './util/errors/UnsupportedMediaTypeHttpError';
// Util/Handlers
export * from './util/handlers/AsyncHandler';
export * from './util/handlers/BooleanHandler';
export * from './util/handlers/ParallelHandler';
export * from './util/handlers/SequenceHandler';
export * from './util/handlers/UnsupportedAsyncHandler';

View File

@@ -0,0 +1,45 @@
import { getLoggerFor } from '../../logging/LogUtil';
import { InternalServerError } from '../errors/InternalServerError';
import { promiseSome } from '../PromiseUtil';
import { AsyncHandler } from './AsyncHandler';
import { filterHandlers } from './HandlerUtil';
/**
* A composite handler that returns true if any of its handlers can handle the input and return true.
* Handler errors are interpreted as false results.
*/
export class BooleanHandler<TIn> extends AsyncHandler<TIn, boolean> {
protected readonly logger = getLoggerFor(this);
private readonly handlers: AsyncHandler<TIn, boolean>[];
/**
* Creates a new BooleanHandler that stores the given handlers.
* @param handlers - Handlers over which it will run.
*/
public constructor(handlers: AsyncHandler<TIn, boolean>[]) {
super();
this.handlers = handlers;
}
public async canHandle(input: TIn): Promise<void> {
// We use this to generate an error if no handler supports the input
await filterHandlers(this.handlers, input);
}
public async handleSafe(input: TIn): Promise<boolean> {
const handlers = await filterHandlers(this.handlers, input);
return promiseSome(handlers.map(async(handler): Promise<boolean> => handler.handle(input)));
}
public async handle(input: TIn): Promise<boolean> {
let handlers: AsyncHandler<TIn, boolean>[];
try {
handlers = await filterHandlers(this.handlers, input);
} catch (error: unknown) {
this.logger.warn('All handlers failed. This might be the consequence of calling handle before canHandle.');
throw new InternalServerError('All handlers failed', { cause: error });
}
return promiseSome(handlers.map(async(handler): Promise<boolean> => handler.handle(input)));
}
}

View File

@@ -0,0 +1,89 @@
import { BadRequestHttpError } from '../errors/BadRequestHttpError';
import { createErrorMessage, isError } from '../errors/ErrorUtil';
import { HttpError } from '../errors/HttpError';
import { InternalServerError } from '../errors/InternalServerError';
import type { AsyncHandler } from './AsyncHandler';
/**
* Combines a list of errors into a single HttpErrors.
* Status code depends on the input errors. If they all share the same status code that code will be re-used.
* If they are all within the 4xx range, 400 will be used, otherwise 500.
*
* @param errors - Errors to combine.
* @param messagePrefix - Prefix for the aggregate error message. Will be followed with an array of all the messages.
*/
export function createAggregateError(errors: Error[], messagePrefix = 'No handler supports the given input:'):
HttpError {
const httpErrors = errors.map((error): HttpError =>
HttpError.isInstance(error) ? error : new InternalServerError(createErrorMessage(error)));
const joined = httpErrors.map((error: Error): string => error.message).join(', ');
const message = `${messagePrefix} [${joined}]`;
// Check if all errors have the same status code
if (httpErrors.length > 0 && httpErrors.every((error): boolean => error.statusCode === httpErrors[0].statusCode)) {
return new HttpError(httpErrors[0].statusCode, httpErrors[0].name, message);
}
// Find the error range (4xx or 5xx)
if (httpErrors.some((error): boolean => error.statusCode >= 500)) {
return new InternalServerError(message);
}
return new BadRequestHttpError(message);
}
/**
* Finds a handler that can handle the given input data.
* Otherwise an error gets thrown.
*
* @param handlers - List of handlers to search in.
* @param input - The input data.
*
* @returns A promise resolving to a handler that supports the data or otherwise rejecting.
*/
export async function findHandler<TIn, TOut>(handlers: AsyncHandler<TIn, TOut>[], input: TIn):
Promise<AsyncHandler<TIn, TOut>> {
const errors: Error[] = [];
for (const handler of handlers) {
try {
await handler.canHandle(input);
return handler;
} catch (error: unknown) {
if (isError(error)) {
errors.push(error);
} else {
errors.push(new Error(createErrorMessage(error)));
}
}
}
throw createAggregateError(errors);
}
/**
* Filters a list of handlers to only keep those that can handle the input.
* Will error if no matching handlers are found.
*
* @param handlers - Handlers to filter.
* @param input - Input that needs to be supported.
*/
export async function filterHandlers<TIn, TOut>(handlers: AsyncHandler<TIn, TOut>[], input: TIn):
Promise<AsyncHandler<TIn, TOut>[]> {
const results = await Promise.allSettled(handlers.map(async(handler): Promise<AsyncHandler<TIn, TOut>> => {
await handler.canHandle(input);
return handler;
}));
const matches = results.filter((result): boolean => result.status === 'fulfilled')
.map((result): AsyncHandler<TIn, TOut> =>
(result as PromiseFulfilledResult<AsyncHandler<TIn, TOut>>).value);
if (matches.length > 0) {
return matches;
}
// Generate error in case no matches were found
const errors = results.map((result): Error => (result as PromiseRejectedResult).reason);
throw createAggregateError(errors);
}

View File

@@ -1,9 +1,7 @@
import { getLoggerFor } from '../../logging/LogUtil';
import { BadRequestHttpError } from '../errors/BadRequestHttpError';
import { createErrorMessage } from '../errors/ErrorUtil';
import { HttpError } from '../errors/HttpError';
import { InternalServerError } from '../errors/InternalServerError';
import type { AsyncHandler } from './AsyncHandler';
import { findHandler } from './HandlerUtil';
/**
* A composite handler that tries multiple handlers one by one
@@ -31,7 +29,7 @@ export class WaterfallHandler<TIn, TOut> implements AsyncHandler<TIn, TOut> {
* @returns A promise resolving if at least 1 handler supports to input, or rejecting if none do.
*/
public async canHandle(input: TIn): Promise<void> {
await this.findHandler(input);
await findHandler(this.handlers, input);
}
/**
@@ -45,7 +43,7 @@ export class WaterfallHandler<TIn, TOut> implements AsyncHandler<TIn, TOut> {
let handler: AsyncHandler<TIn, TOut>;
try {
handler = await this.findHandler(input);
handler = await findHandler(this.handlers, input);
} catch (error: unknown) {
this.logger.warn('All handlers failed. This might be the consequence of calling handle before canHandle.');
throw new InternalServerError('All handlers failed', { cause: error });
@@ -63,48 +61,8 @@ export class WaterfallHandler<TIn, TOut> implements AsyncHandler<TIn, TOut> {
* It rejects if no handlers support the given data.
*/
public async handleSafe(input: TIn): Promise<TOut> {
const handler = await this.findHandler(input);
const handler = await findHandler(this.handlers, input);
return handler.handle(input);
}
/**
* Finds a handler that can handle the given input data.
* Otherwise an error gets thrown.
*
* @param input - The input data.
*
* @returns A promise resolving to a handler that supports the data or otherwise rejecting.
*/
private async findHandler(input: TIn): Promise<AsyncHandler<TIn, TOut>> {
const errors: HttpError[] = [];
for (const handler of this.handlers) {
try {
await handler.canHandle(input);
return handler;
} catch (error: unknown) {
if (HttpError.isInstance(error)) {
errors.push(error);
} else {
errors.push(new InternalServerError(createErrorMessage(error)));
}
}
}
const joined = errors.map((error: Error): string => error.message).join(', ');
const message = `No handler supports the given input: [${joined}]`;
// Check if all errors have the same status code
if (errors.length > 0 && errors.every((error): boolean => error.statusCode === errors[0].statusCode)) {
throw new HttpError(errors[0].statusCode, errors[0].name, message);
}
// Find the error range (4xx or 5xx)
if (errors.some((error): boolean => error.statusCode >= 500)) {
throw new InternalServerError(message);
}
throw new BadRequestHttpError(message);
}
}