0
0
NestJSframework~10 mins

Logging exceptions in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logging exceptions
Exception occurs in code
Catch exception in catch block
Log exception details
Optional: Re-throw or handle gracefully
Continue execution or exit
When an error happens, the program catches it, logs the details for review, then decides to continue or stop.
Execution Sample
NestJS
try {
  // code that may fail
} catch (error) {
  this.logger.error('Error caught:', error.stack);
}
This code tries to run something risky, and if it fails, it logs the error stack trace.
Execution Table
StepActionCode ExecutedResultLog Output
1Start try blocktry { riskyCode(); }riskyCode runsNo log yet
2Exception thrownriskyCode() throws ErrorException caughtNo log yet
3Catch block enteredcatch (error) { ... }Error object availableNo log yet
4Log errorthis.logger.error('Error caught:', error.stack);Error loggedError caught: <stack trace>
5End catch block}Catch block endsLog recorded
6Continue or exitAfter catch blockProgram continues or stopsLog remains for review
💡 Exception caught and logged, program flow continues or stops based on handling
Variable Tracker
VariableStartAfter Step 2After Step 3Final
errorundefinedError instance createdError instance available in catchError instance remains
Key Moments - 3 Insights
Why do we log error.stack instead of just error?
Logging error.stack shows the full trace of where the error happened, which helps debugging better than just the error message. See execution_table step 4.
What happens if we don't catch the exception?
If not caught, the program may crash or stop unexpectedly. Catching lets us log and handle errors gracefully, as shown in steps 2 and 3.
Can the program continue after logging the exception?
Yes, depending on handling after catch block (step 6), the program can continue or exit safely.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the error object first available?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check the 'Result' column for when the error object is accessible in the catch block.
At which step does the actual logging happen?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the 'Log error' action and the 'Log Output' showing the error message.
If the catch block did not log the error, what would be missing in the execution table?
AError object creation
BException thrown
CLog Output with error details
DTry block execution
💡 Hint
Refer to the 'Log Output' column in steps 4 and 5 to see what logging adds.
Concept Snapshot
try {
  // risky code
} catch (error) {
  this.logger.error('Error caught:', error.stack);
}

- Catch exceptions to prevent crashes
- Log error.stack for full trace
- Decide to continue or exit after logging
Full Transcript
In NestJS, when code might fail, we wrap it in a try block. If an error happens, the catch block runs. Inside catch, we log the error details using this.logger.error and include error.stack to see where the error happened. This helps us understand and fix issues. After logging, we can choose to continue running or stop safely. This process prevents unexpected crashes and keeps logs for debugging.