@nestjs/microservices
Version:
Nest - modern, fast, powerful node.js web framework (@microservices)
24 lines (23 loc) • 852 B
JavaScript
import { isObservable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ExecutionContextHost } from '@nestjs/core/internal';
export class RpcProxy {
create(targetCallback, exceptionsHandler) {
return async (...args) => {
try {
const result = await targetCallback(...args);
return !isObservable(result)
? result
: result.pipe(catchError(error => this.handleError(exceptionsHandler, args, error)));
}
catch (error) {
return this.handleError(exceptionsHandler, args, error);
}
};
}
handleError(exceptionsHandler, args, error) {
const host = new ExecutionContextHost(args);
host.setType('rpc');
return exceptionsHandler.handle(error, host);
}
}