mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import { ExceptionsHandler } from '../exceptions/exceptions-handler';
|
|
import { ExecutionContextHost } from '../helpers/execution-context-host';
|
|
|
|
export type RouterProxyCallback = <TRequest, TResponse>(
|
|
req: TRequest,
|
|
res: TResponse,
|
|
next: () => void,
|
|
) => void | Promise<void>;
|
|
|
|
export class RouterProxy {
|
|
public createProxy(
|
|
targetCallback: RouterProxyCallback,
|
|
exceptionsHandler: ExceptionsHandler,
|
|
) {
|
|
return async <TRequest, TResponse>(
|
|
req: TRequest,
|
|
res: TResponse,
|
|
next: () => void,
|
|
) => {
|
|
try {
|
|
await targetCallback(req, res, next);
|
|
} catch (e) {
|
|
const host = new ExecutionContextHost([req, res, next]);
|
|
exceptionsHandler.next(e, host);
|
|
return res;
|
|
}
|
|
};
|
|
}
|
|
|
|
public createExceptionLayerProxy(
|
|
targetCallback: <TError, TRequest, TResponse>(
|
|
err: TError,
|
|
req: TRequest,
|
|
res: TResponse,
|
|
next: () => void,
|
|
) => void | Promise<void>,
|
|
exceptionsHandler: ExceptionsHandler,
|
|
) {
|
|
return async <TError, TRequest, TResponse>(
|
|
err: TError,
|
|
req: TRequest,
|
|
res: TResponse,
|
|
next: () => void,
|
|
) => {
|
|
try {
|
|
await targetCallback(err, req, res, next);
|
|
} catch (e) {
|
|
const host = new ExecutionContextHost([req, res, next]);
|
|
exceptionsHandler.next(e, host);
|
|
return res;
|
|
}
|
|
};
|
|
}
|
|
}
|