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