0
0
Node.jsframework~10 mins

Unhandled rejection handling in Node.js - Interactive Code Practice

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

Complete the code to listen for unhandled promise rejections.

Node.js
process.on('unhandledRejection', [1] => {
  console.error('Unhandled rejection:', [1]);
});
Drag options to blanks, or click blank then click option'
Ae
Berr
Creason
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that is not descriptive like 'e' or 'err' can confuse the purpose.
Not handling the event at all causes the process to crash on unhandled rejections.
2fill in blank
medium

Complete the code to exit the process after logging an unhandled rejection.

Node.js
process.on('unhandledRejection', reason => {
  console.error('Unhandled rejection:', reason);
  process.[1](1);
});
Drag options to blanks, or click blank then click option'
Aexit
Bquit
Cstop
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'stop' or 'quit' causes runtime errors.
Not exiting the process may leave the app in an unstable state.
3fill in blank
hard

Fix the error in the unhandled rejection handler to correctly log the error stack.

Node.js
process.on('unhandledRejection', (reason) => {
  console.error('Error stack:', [1]);
});
Drag options to blanks, or click blank then click option'
Areason.toString()
Breason.error
Creason.message
Dreason.stack
Attempts:
3 left
💡 Hint
Common Mistakes
Logging 'reason.message' instead of 'reason.stack' loses debugging details.
Accessing a non-existent property like 'reason.error' causes undefined output.
4fill in blank
hard

Fill both blanks to create a handler that logs the error and then exits the process.

Node.js
process.on('unhandledRejection', ([1]) => {
  console.error('Unhandled rejection:', [2]);
  process.exit(1);
});
Drag options to blanks, or click blank then click option'
Areason
Berror
Cerr
De
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and the logged variable causes reference errors.
Using generic names like 'e' can reduce code readability.
5fill in blank
hard

Fill all three blanks to create a robust unhandled rejection handler that logs the error stack, warns, and exits.

Node.js
process.on('unhandledRejection', ([1]) => {
  console.error('Unhandled rejection stack:', [2]);
  console.warn('Warning: process will exit due to unhandled rejection');
  process.[3](1);
});
Drag options to blanks, or click blank then click option'
Areason
Breason.stack
Cexit
Derr
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'err' instead of 'reason' can confuse the event parameter.
Logging 'reason' instead of 'reason.stack' loses stack trace info.
Calling a non-existent method instead of 'exit' causes runtime errors.