From 7d4b059404aa8e8b9cda1b431f95b0e9d13cd0ae Mon Sep 17 00:00:00 2001 From: Arun Raghavan Date: Fri, 3 May 2019 11:32:37 +0530 Subject: [PATCH] Add more process event handlers This adds functions to register handlers for the 'uncaughtException' and 'unhandledRejection' event on Process. --- src/Node/Process.js | 16 ++++++++++++++++ src/Node/Process.purs | 24 +++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Node/Process.js b/src/Node/Process.js index 1dc584d..6511b66 100644 --- a/src/Node/Process.js +++ b/src/Node/Process.js @@ -16,6 +16,22 @@ exports.onExit = function (callback) { }; }; +exports.onUncaughtException = function (callback) { + return function () { + process.on("uncaughtException", function (error) { + callback(error)(); + }); + }; +}; + +exports.onUnhandledRejection = function (callback) { + return function () { + process.on("unhandledRejection", function (error, promise) { + callback(error)(promise)(); + }); + }; +}; + exports.onSignalImpl = function (signal) { return function (callback) { return function () { diff --git a/src/Node/Process.purs b/src/Node/Process.purs index e8d28d1..2b36a35 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -3,6 +3,8 @@ module Node.Process ( onBeforeExit , onExit , onSignal + , onUncaughtException + , onUnhandledRejection , argv , execArgv , execPath @@ -24,17 +26,16 @@ module Node.Process import Prelude -import Effect (Effect) - import Data.Maybe (Maybe) import Data.Posix (Pid) import Data.Posix.Signal (Signal) import Data.Posix.Signal as Signal +import Effect (Effect) +import Effect.Exception (Error) import Foreign.Object as FO import Node.Platform (Platform) import Node.Platform as Platform import Node.Stream (Readable, Writable) - import Unsafe.Coerce (unsafeCoerce) -- YOLO @@ -56,6 +57,23 @@ foreign import onBeforeExit :: Effect Unit -> Effect Unit -- | to exit with. foreign import onExit :: (Int -> Effect Unit) -> Effect Unit +-- | Install a handler for uncaught exceptions. The callback will be called +-- | when the process emits the `uncaughtException` event. The handler +-- | currently does not expose the second `origin` argument from the Node 12 +-- | version of this event to maintain compatibility with older Node versions. +foreign import onUncaughtException :: (Error -> Effect Unit) -> Effect Unit + +-- | Install a handler for unhandled promise rejections. The callback will be +-- | called when the process emits the `unhandledRejection` event. +-- | +-- | The first argument to the handler can be whatever type the unhandled +-- | Promise yielded on rejection (typically, but not necessarily, an `Error`). +-- | +-- | The handler currently does not expose the type of the second argument, +-- | which is a `Promise`, in order to allow users of this library to choose +-- | their own PureScript `Promise` bindings. +foreign import onUnhandledRejection :: forall a b. (a -> b -> Effect Unit) -> Effect Unit + foreign import onSignalImpl :: String -> Effect Unit -> Effect Unit -- | Install a handler for a particular signal.