mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Add BooleanHandler
Co-Authored-By: Ludovico Granata <Ludogranata@gmail.com>
This commit is contained in:
parent
0355673a0f
commit
73867f0827
@ -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';
|
||||
|
45
src/util/handlers/BooleanHandler.ts
Normal file
45
src/util/handlers/BooleanHandler.ts
Normal 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)));
|
||||
}
|
||||
}
|
89
src/util/handlers/HandlerUtil.ts
Normal file
89
src/util/handlers/HandlerUtil.ts
Normal 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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
56
test/unit/util/handlers/BooleanHandler.test.ts
Normal file
56
test/unit/util/handlers/BooleanHandler.test.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
|
||||
import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler';
|
||||
import { BooleanHandler } from '../../../../src/util/handlers/BooleanHandler';
|
||||
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';
|
||||
|
||||
describe('A BooleanHandler', (): void => {
|
||||
let handlerFalse: AsyncHandler<any, false>;
|
||||
let handlerTrue: AsyncHandler<any, true>;
|
||||
let handlerError: AsyncHandler<any, never>;
|
||||
let handlerCanNotHandle: AsyncHandler<any, any>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
handlerFalse = new StaticAsyncHandler(true, false);
|
||||
handlerTrue = new StaticAsyncHandler(true, true);
|
||||
handlerError = new StaticAsyncHandler(true, null) as any;
|
||||
handlerError.handle = (): never => {
|
||||
throw new InternalServerError();
|
||||
};
|
||||
handlerCanNotHandle = new StaticAsyncHandler(false, null);
|
||||
});
|
||||
|
||||
it('can handle the input if any of its handlers can.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerFalse, handlerCanNotHandle ]);
|
||||
await expect(handler.canHandle(null)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('errors if none of its handlers supports the input.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerCanNotHandle, handlerCanNotHandle ]);
|
||||
await expect(handler.canHandle(null)).rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
|
||||
it('returns true if any of its handlers returns true.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerFalse, handlerTrue, handlerCanNotHandle ]);
|
||||
await expect(handler.handle(null)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('returns false if none of its handlers returns true.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerFalse, handlerError, handlerCanNotHandle ]);
|
||||
await expect(handler.handle(null)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('throw an internal error when calling handle with unsupported input.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerCanNotHandle, handlerCanNotHandle ]);
|
||||
await expect(handler.handle(null)).rejects.toThrow(InternalServerError);
|
||||
});
|
||||
|
||||
it('returns the same handle results with handleSafe.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerFalse, handlerTrue, handlerCanNotHandle ]);
|
||||
await expect(handler.handleSafe(null)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('throws the canHandle error when calling handleSafe with unsupported input.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerCanNotHandle, handlerCanNotHandle ]);
|
||||
await expect(handler.handleSafe(null)).rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
});
|
84
test/unit/util/handlers/HandlerUtil.test.ts
Normal file
84
test/unit/util/handlers/HandlerUtil.test.ts
Normal file
@ -0,0 +1,84 @@
|
||||
import { HttpError } from '../../../../src/util/errors/HttpError';
|
||||
import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler';
|
||||
import { createAggregateError, filterHandlers, findHandler } from '../../../../src/util/handlers/HandlerUtil';
|
||||
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';
|
||||
|
||||
describe('HandlerUtil', (): void => {
|
||||
describe('createAggregateError', (): void => {
|
||||
const error401 = new HttpError(401, 'UnauthorizedHttpError');
|
||||
const error415 = new HttpError(415, 'UnsupportedMediaTypeHttpError');
|
||||
const error501 = new HttpError(501, 'NotImplementedHttpError');
|
||||
const error = new Error('noStatusCode');
|
||||
|
||||
it('throws an error with matching status code if all errors have the same.', async(): Promise<void> => {
|
||||
expect(createAggregateError([ error401, error401 ])).toMatchObject({
|
||||
statusCode: 401,
|
||||
name: 'UnauthorizedHttpError',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an InternalServerError if one of the errors has status code 5xx.', async(): Promise<void> => {
|
||||
expect(createAggregateError([ error401, error501 ])).toMatchObject({
|
||||
statusCode: 500,
|
||||
name: 'InternalServerError',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an BadRequestHttpError if all handlers have 4xx status codes.', async(): Promise<void> => {
|
||||
expect(createAggregateError([ error401, error415 ])).toMatchObject({
|
||||
statusCode: 400,
|
||||
name: 'BadRequestHttpError',
|
||||
});
|
||||
});
|
||||
|
||||
it('interprets non-HTTP errors as internal errors.', async(): Promise<void> => {
|
||||
expect(createAggregateError([ error ])).toMatchObject({
|
||||
statusCode: 500,
|
||||
name: 'InternalServerError',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('findHandler', (): void => {
|
||||
let handlerTrue: AsyncHandler<any, any>;
|
||||
let handlerFalse: AsyncHandler<any, any>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
handlerTrue = new StaticAsyncHandler(true, null);
|
||||
handlerFalse = new StaticAsyncHandler(false, null);
|
||||
});
|
||||
|
||||
it('finds a matching handler.', async(): Promise<void> => {
|
||||
await expect(findHandler([ handlerFalse, handlerTrue ], null)).resolves.toBe(handlerTrue);
|
||||
});
|
||||
|
||||
it('errors if there is no matching handler.', async(): Promise<void> => {
|
||||
await expect(findHandler([ handlerFalse, handlerFalse ], null)).rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
|
||||
it('supports non-native Errors.', async(): Promise<void> => {
|
||||
handlerFalse.canHandle = jest.fn().mockRejectedValue('apple');
|
||||
await expect(findHandler([ handlerFalse ], null)).rejects.toThrow('[Unknown error: apple]');
|
||||
});
|
||||
});
|
||||
|
||||
describe('filterHandlers', (): void => {
|
||||
let handlerTrue: AsyncHandler<any, any>;
|
||||
let handlerFalse: AsyncHandler<any, any>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
handlerTrue = new StaticAsyncHandler(true, null);
|
||||
handlerFalse = new StaticAsyncHandler(false, null);
|
||||
});
|
||||
|
||||
it('finds matching handlers.', async(): Promise<void> => {
|
||||
await expect(filterHandlers([ handlerTrue, handlerFalse, handlerTrue ], null))
|
||||
.resolves.toEqual([ handlerTrue, handlerTrue ]);
|
||||
});
|
||||
|
||||
it('errors if there is no matching handler.', async(): Promise<void> => {
|
||||
await expect(filterHandlers([ handlerFalse, handlerFalse ], null))
|
||||
.rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
});
|
||||
});
|
@ -1,5 +1,3 @@
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
import { HttpError } from '../../../../src/util/errors/HttpError';
|
||||
import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler';
|
||||
import { WaterfallHandler } from '../../../../src/util/handlers/WaterfallHandler';
|
||||
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';
|
||||
@ -83,44 +81,5 @@ describe('A WaterfallHandler', (): void => {
|
||||
|
||||
await expect(handler.handleSafe(null)).rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
|
||||
it('throws an error with matching status code if all handlers threw the same.', async(): Promise<void> => {
|
||||
handlerTrue.canHandle = async(): Promise<void> => {
|
||||
throw new HttpError(401, 'UnauthorizedHttpError');
|
||||
};
|
||||
const handler = new WaterfallHandler([ handlerTrue, handlerTrue ]);
|
||||
|
||||
await expect(handler.canHandle(null)).rejects.toMatchObject({
|
||||
statusCode: 401,
|
||||
name: 'UnauthorizedHttpError',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an internal server error if one of the handlers threw one.', async(): Promise<void> => {
|
||||
handlerTrue.canHandle = async(): Promise<void> => {
|
||||
throw new HttpError(401, 'UnauthorizedHttpError');
|
||||
};
|
||||
handlerFalse.canHandle = async(): Promise<void> => {
|
||||
throw new Error('Server is crashing!');
|
||||
};
|
||||
const handler = new WaterfallHandler([ handlerTrue, handlerFalse ]);
|
||||
|
||||
await expect(handler.canHandle(null)).rejects.toMatchObject({
|
||||
statusCode: 500,
|
||||
name: 'InternalServerError',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an BadRequestHttpError if handlers throw different errors.', async(): Promise<void> => {
|
||||
handlerTrue.canHandle = async(): Promise<void> => {
|
||||
throw new HttpError(401, 'UnauthorizedHttpError');
|
||||
};
|
||||
handlerFalse.canHandle = async(): Promise<void> => {
|
||||
throw new HttpError(415, 'UnsupportedMediaTypeHttpError');
|
||||
};
|
||||
const handler = new WaterfallHandler([ handlerTrue, handlerFalse ]);
|
||||
|
||||
await expect(handler.canHandle(null)).rejects.toThrow(BadRequestHttpError);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user