0
0
NestJSframework~10 mins

Custom exception classes 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 import the base exception class from NestJS.

NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
AHttpException
BController
CModule
DInjectable
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated decorators like Controller or Module.
Using Injectable instead of HttpException.
2fill in blank
medium

Complete the code to define a custom exception class extending the base exception.

NestJS
export class CustomError extends [1] {
  constructor(message: string = 'Custom error occurred', status: number = 400) {
    super(message, status);
  }
}
Drag options to blanks, or click blank then click option'
AController
BHttpException
CModule
DInjectable
Attempts:
3 left
💡 Hint
Common Mistakes
Extending decorators like Controller or Injectable.
Not extending any class.
3fill in blank
hard

Fix the error in the constructor to correctly call the base class constructor with message and status.

NestJS
constructor() {
  super([1], 404);
}
Drag options to blanks, or click blank then click option'
A'Error occurred'
B404
Cthis.message
D'Not Found Error'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the status code as the first argument.
Using this.message before calling super.
4fill in blank
hard

Fill both blanks to throw the custom exception with a message and status code.

NestJS
throw new CustomError([1], [2]);
Drag options to blanks, or click blank then click option'
A'Resource missing'
B404
C500
D'Internal error'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing message and status code order.
Using wrong status codes for the message.
5fill in blank
hard

Fill all three blanks to create a custom exception class with a dynamic message and status code.

NestJS
export class [1] extends HttpException {
  constructor(message: string, status: number) {
    super(message, [2]);
    this.[3] = message;
  }
}
Drag options to blanks, or click blank then click option'
ADynamicError
Bstatus
Cmessage
DCustomException
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names.
Not passing status code correctly.