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 TheRpcExceptionclass is imported from the@nestjs/microservicespackage.
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.
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.