0
0
NestJSframework~10 mins

Global exception filters in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a global exception filter class in NestJS.

NestJS
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';

@Catch()
export class GlobalExceptionFilter implements [1] {
  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(500).json({ message: 'Internal server error' });
  }
}
Drag options to blanks, or click blank then click option'
AMiddleware
BPipeTransform
CInterceptor
DExceptionFilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using PipeTransform or Interceptor instead of ExceptionFilter
Not implementing any interface
2fill in blank
medium

Complete the code to apply the global exception filter in the main bootstrap function.

NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { GlobalExceptionFilter } from './filters/global-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.[1](new GlobalExceptionFilter());
  await app.listen(3000);
}
bootstrap();
Drag options to blanks, or click blank then click option'
AuseGlobalFilters
BuseGlobalPipes
CuseGlobalInterceptors
DuseMiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using useGlobalPipes or useGlobalInterceptors instead
Trying to use useMiddleware which is not a method on the app instance
3fill in blank
hard

Fix the error in the global exception filter to correctly get the HTTP response object.

NestJS
catch(exception: any, host: ArgumentsHost) {
  const ctx = host.[1]();
  const response = ctx.getResponse();
  response.status(500).json({ message: 'Internal server error' });
}
Drag options to blanks, or click blank then click option'
AswitchToHttp
BswitchToRpc
CswitchToWs
DgetHttpContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using switchToRpc or switchToWs which are for other contexts
Using a non-existent method like getHttpContext
4fill in blank
hard

Fill both blanks to create a global exception filter that returns the exception message and status code.

NestJS
catch(exception: any, host: ArgumentsHost) {
  const ctx = host.[1]();
  const response = ctx.getResponse();
  const status = exception.[2] || 500;
  response.status(status).json({
    statusCode: status,
    message: exception.message || 'Internal server error',
  });
}
Drag options to blanks, or click blank then click option'
AswitchToHttp
BgetStatus
Cstatus
DgetResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using getStatus() method which does not exist on exception
Using getResponse() instead of switchToHttp()
5fill in blank
hard

Fill all three blanks to create a global exception filter that logs the error and sends a JSON response with status and message.

NestJS
catch(exception: any, host: ArgumentsHost) {
  const ctx = host.[1]();
  const response = ctx.getResponse();
  const request = ctx.getRequest();
  const status = exception.[2] || 500;
  console.error('Error:', exception.message);
  response.status(status).json({
    statusCode: status,
    timestamp: new Date().toISOString(),
    path: request.[3],
    message: exception.message || 'Internal server error',
  });
}
Drag options to blanks, or click blank then click option'
AswitchToHttp
Bstatus
Curl
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using getResponse() instead of switchToHttp()
Using message instead of status for HTTP status code
Using path instead of url for request path