UNPKG

@nestjs/websockets

Version:

Nest - modern, fast, powerful node.js web framework (@websockets)

28 lines (27 loc) 1 kB
import { EMPTY, isObservable } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { ExecutionContextHost } from '@nestjs/core/internal'; export class WsProxy { create(targetCallback, exceptionsHandler, targetPattern) { return async (...args) => { args = [...args, targetPattern ?? 'unknown']; try { const result = await targetCallback(...args); return !isObservable(result) ? result : result.pipe(catchError(error => { this.handleError(exceptionsHandler, args, error); return EMPTY; })); } catch (error) { this.handleError(exceptionsHandler, args, error); } }; } handleError(exceptionsHandler, args, error) { const host = new ExecutionContextHost(args); host.setType('ws'); exceptionsHandler.handle(error, host); } }