0
0
NestJSframework~10 mins

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 basic exception filter class in NestJS.

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

@Catch()
export class AllExceptionsFilter implements [1] {
  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(500).json({
      statusCode: 500,
      message: 'Internal server error',
    });
  }
}
Drag options to blanks, or click blank then click option'
AController
BExceptionFilter
CHttpException
DMiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using HttpException instead of ExceptionFilter as the implemented interface.
Not implementing any interface.
Using Controller or Middleware which are unrelated here.
2fill in blank
medium

Complete the code to catch only HTTP exceptions in the filter.

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

@Catch([1])
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception.getStatus();
    response.status(status).json({
      statusCode: status,
      message: exception.message,
    });
  }
}
Drag options to blanks, or click blank then click option'
AExceptionFilter
BError
CHttpException
DAllExceptionsFilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using Error which is too general.
Using ExceptionFilter which is an interface, not an exception class.
Using a custom filter class name instead of an exception class.
3fill in blank
hard

Fix the error in the catch method to correctly get the request object.

NestJS
catch(exception: HttpException, host: ArgumentsHost) {
  const ctx = host.switchToHttp();
  const response = ctx.getResponse();
  const request = ctx.[1]();
  const status = exception.getStatus();
  response.status(status).json({
    statusCode: status,
    timestamp: new Date().toISOString(),
    path: request.url,
  });
}
Drag options to blanks, or click blank then click option'
AgetReq
Brequest
CgetRequestObject
DgetRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using getReq which is not a method on ArgumentsHost.
Using getRequestObject which does not exist.
Using request which is a property, not a method.
4fill in blank
hard

Fill both blanks to create a filter that catches multiple exception types.

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

@Catch([1], [2])
export class MultiExceptionFilter implements ExceptionFilter {
  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception instanceof HttpException ? exception.getStatus() : 500;
    response.status(status).json({
      statusCode: status,
      message: exception.message || 'Unknown error',
    });
  }
}
Drag options to blanks, or click blank then click option'
AHttpException
BExceptionFilter
CBadRequestException
DError
Attempts:
3 left
💡 Hint
Common Mistakes
Using ExceptionFilter which is an interface, not an exception class.
Using Error which is too general.
Using the same exception class twice.
5fill in blank
hard

Fill all three blanks to create a custom exception filter that logs the error and sends a JSON response.

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

@Catch([1])
export class LoggingExceptionFilter implements ExceptionFilter {
  private readonly logger = new [2](LoggingExceptionFilter.name);

  catch(exception: HttpException, host: ArgumentsHost) {
    this.logger.[3](exception.message);
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception.getStatus();
    response.status(status).json({
      statusCode: status,
      message: exception.message,
    });
  }
}
Drag options to blanks, or click blank then click option'
AHttpException
BLogger
Cerror
DExceptionFilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using ExceptionFilter instead of HttpException in the decorator.
Using a wrong logger method like 'log' instead of 'error'.
Not creating a Logger instance properly.