ngATL Conference Atlanta, GA

NestJS on 2018 ngATL Conference Atlanta, GA

NestJS 2018 ngATL

LEARN MORE

Exception Filters

The websockets exceptions layer works exact same as the prime layer. The only difference is that instead of throwing HttpException, you should throw RpcException.

TypeScript

throw new RpcException('Invalid credentials.');
Notice The RpcException class is imported from the @nestjs/microservices package.

Nest would handle this exception and emits the exception message with the following data:

TypeScript

{
  status: 'error',
  message: 'Invalid credentials.'
}

Exception Filters

The Exception Filters are also really similar, and works exactly in the same way as the primary ones. The only difference is that catch() method should returns an Observable.

rpc-exception.filter.ts
JavaScript TypeScript
TypeScript

import { Catch, RpcExceptionFilter } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { RpcException } from '@nestjs/microservices';
import 'rxjs/add/observable/throw';

@Catch(RpcException)
export class ExceptionFilter implements RpcExceptionFilter {
  catch(exception: RpcException): Observable<any> {
    return Observable.throw(exception.getError());
  }
}
TypeScript

import { Catch } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { RpcException } from '@nestjs/microservices';
import 'rxjs/add/observable/throw';

@Catch(RpcException)
export class ExceptionFilter {
  catch(exception) {
    return Observable.throw(exception.getError());
  }
}
Notice It's impossible to set up the microservices exception filters globally.

Sponsors

Nest is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here. Thanks!

Become a sponsor