Errors, Diagnostics & Reliability
Failure handling, custom errors, crash behavior, and production diagnostics.
Q61: How should uncaught exceptions be handled in Node.js?
Listen to process.on('uncaughtException') to log diagnostic info, then perform a graceful application shutdown to prevent unpredictable state corruptions.
- Uncaught exceptions leave applications in an unreliable state.
- Always log error diagnostics and restart the application process.
What is the recommended practice after capturing an uncaughtException?
Q62: How do you handle unhandled promise rejections in Node.js?
Handle unhandled promise rejections using process.on('unhandledRejection', callback). In modern Node.js versions, unhandled rejections terminate the process with a non-zero exit code by default.
- Failing to catch promise rejections triggers unhandled rejections.
- Process listener enables central logging and clean teardown.
Which process event fires when a Promise is rejected without a .catch() block?
Q63: How do you create custom Error classes in Node.js?
Extend the built-in Error class and invoke super(message). Capture stack traces using Error.captureStackTrace(this, CustomErrorClass).
- Extend the standard JavaScript
Errorclass. - Use
Error.captureStackTrace()to preserve clean stack trace histories.
Which V8 method captures formatted call stack lines on custom error objects?
Q64: What are Node.js Diagnostic Reports and how are they generated?
Diagnostic Reports are JSON-formatted summary files generated by Node.js containing detailed runtime information, including call stacks, native stack traces, heap statistics, system environment variables, resource usage, and loaded native modules. They can be triggered automatically on crashes, uncaught exceptions, or fatal memory exhaustion, or manually using process.report.writeReport() or the command-line flag --report-uncaught-exception.
- Diagnostic reports output comprehensive JSON diagnostics for troubleshooting production crashes.
- They capture call stacks, heap stats, native OS information, and process arguments without external tools.
What format do Node.js Diagnostic Reports use to store runtime statistics?