Initialization: The application begins by creating a new instance of the Hunter
class, passing in a configuration object. This configuration includes details like the application name, email addresses for error reporting, and various options to customize the error handling behavior.
Start Hunting: startHunting
method is invoked to start listening for uncaught exceptions and unhandled promise rejections in the Node.js application. This is achieved by attaching event listeners to the process object.
Error Occurence: When an uncaught exception or unhandled rejection happens, the corresponding handler function handleUncaughtException
or handleUnhandledRejection
is triggered.
Error Handling: These handler functions parse the error stack, construct the error data, and send it to the server for email reporting. If local logging is enabled, the error data is also written to a log file.
Error Reporting: The Agent
class is responsible for sending the error data to the server. It constructs the error data into an exception template, which includes details like the error message, stack trace, and code context. This data is then sent to the server via HTTP request.
Stop Hunting: If the quitOnError
option is enabled in the configuration, the application will exit when error occurs. Otherwise, the stopHunting()
method can be called to stop listening for errors by removing the event listeners from the process object.