All files / websockets/exceptions base-ws-exception-filter.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 1/1
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  1x 1x 1x   1x   3x 3x   3x 1x 1x   2x 2x           2x      
import { ArgumentsHost, WsExceptionFilter } from '@nestjs/common';
import { isObject } from '@nestjs/common/utils/shared.utils';
import { MESSAGES } from '@nestjs/core/constants';
import { WsException } from './ws-exception';
 
export class BaseWsExceptionFilter<T = any> implements WsExceptionFilter<T> {
  catch(exception: T, host: ArgumentsHost) {
    const client = host.switchToWs().getClient();
    const status = 'error';
 
    if (!(exception instanceof WsException)) {
      const errorMessage = MESSAGES.UNKNOWN_EXCEPTION_MESSAGE;
      return client.emit('exception', { status, message: errorMessage });
    }
    const result = exception.getError();
    const message = isObject(result)
      ? result
      : {
          status,
          message: result,
        };
    client.emit('exception', message);
  }
}