All quizzesHard
Filter Hierarchy
Preview — 3 of 10 questions
What advantage does registering a global filter with the APP_FILTER token have over app.useGlobalFilters(new MyFilter())?
javascript
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { AllExceptionsFilter } from './all-exceptions.filter';
@Module({
providers: [{ provide: APP_FILTER, useClass: AllExceptionsFilter }],
})
export class AppModule {}AIt runs filters faster at runtime
BThe filter is instantiated by the DI container, so it can inject providers like a logger or config service
CIt disables all other filters in the app
DIt only works for WebSocket gateways
In a global catch-all filter that needs Nest's default behavior as a fallback, what must you provide if extending BaseExceptionFilter without DI?
javascript
import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseExceptionFilter, HttpAdapterHost } from '@nestjs/core';
@Catch()
export class GlobalFilter extends BaseExceptionFilter {
constructor(adapterHost: HttpAdapterHost) {
super(adapterHost.httpAdapter);
}
catch(exception: unknown, host: ArgumentsHost): void {
super.catch(exception, host);
}
}AThe HttpAdapterHost (or applicationRef) so the base class can send a response
BNothing — it works automatically
CA Redis client
DA custom RxJS scheduler
How do you customize error handling for a @WebSocketGateway?
javascript
import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseWsExceptionFilter, WsException } from '@nestjs/websockets';
import { Socket } from 'socket.io';
@Catch(WsException)
export class WsExceptionFilter extends BaseWsExceptionFilter {
catch(exception: WsException, host: ArgumentsHost): void {
const client = host.switchToWs().getClient<Socket>();
client.emit('error', { message: exception.message });
}
}AUse the same BaseExceptionFilter unchanged
BImplement a filter that handles WsException and uses host.switchToWs() to access the client
CWebSocket errors cannot be filtered in NestJS
DThrow HttpException from the gateway
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.