0
0
NestJSframework~10 mins

Logging exceptions 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 Logger from NestJS.

NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
AConsole
BLogger
CLogManager
DLogService
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent import like LogService or LogManager.
Trying to import Console which is not from NestJS.
2fill in blank
medium

Complete the code to create a Logger instance inside a service class.

NestJS
private readonly logger = new [1](MyService.name);
Drag options to blanks, or click blank then click option'
ALogger
BLogManager
CLogService
DConsole
Attempts:
3 left
💡 Hint
Common Mistakes
Using Console instead of Logger.
Trying to instantiate a non-existent LogService.
3fill in blank
hard

Fix the error in logging an exception message inside a catch block.

NestJS
catch (error) {
  this.logger.[1]('Error occurred:', error.message);
}
Drag options to blanks, or click blank then click option'
Aerror
Blog
Cwarn
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using log or info which are lower severity levels.
Using warn which is for warnings, not errors.
4fill in blank
hard

Fill both blanks to log the stack trace of an exception.

NestJS
catch (error) {
  this.logger.[1]('Exception stack:', error.[2]);
}
Drag options to blanks, or click blank then click option'
Aerror
Bstack
Cmessage
Dlog
Attempts:
3 left
💡 Hint
Common Mistakes
Logging error.message instead of error.stack for full trace.
Using log or warn instead of error method.
5fill in blank
hard

Fill all three blanks to create a custom exception filter that logs exceptions.

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

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

  catch(exception: any, host: [3]) {
    this.logger.error('Exception caught:', exception.stack);
    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'
ALogger
CArgumentsHost
DHttpException
Attempts:
3 left
💡 Hint
Common Mistakes
Importing HttpException instead of ArgumentsHost.
Using different names for Logger import and instance.
Wrong type for host parameter.