Skip to content

Commit fd4c76b

Browse files
committed
Add more process event handlers
This adds functions to register handlers for the 'uncaughtException' and 'unhandledRejection' event on Process.
1 parent 88871d8 commit fd4c76b

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Node/Process.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ exports.onExit = function (callback) {
1616
};
1717
};
1818

19+
exports.onUncaughtException = function (callback) {
20+
return function () {
21+
process.on("uncaughtException", function (error) {
22+
callback(error)();
23+
});
24+
};
25+
};
26+
27+
exports.onUnhandledRejection = function (callback) {
28+
return function () {
29+
process.on("unhandledRejection", function (error, promise) {
30+
callback(error)(promise)();
31+
});
32+
};
33+
};
34+
1935
exports.onSignalImpl = function (signal) {
2036
return function (callback) {
2137
return function () {

src/Node/Process.purs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Node.Process
33
( onBeforeExit
44
, onExit
55
, onSignal
6+
, onUncaughtException
7+
, onUnhandledRejection
68
, argv
79
, execArgv
810
, execPath
@@ -24,17 +26,16 @@ module Node.Process
2426

2527
import Prelude
2628

27-
import Effect (Effect)
28-
2929
import Data.Maybe (Maybe)
3030
import Data.Posix (Pid)
3131
import Data.Posix.Signal (Signal)
3232
import Data.Posix.Signal as Signal
33+
import Effect (Effect)
34+
import Effect.Exception (Error)
3335
import Foreign.Object as FO
3436
import Node.Platform (Platform)
3537
import Node.Platform as Platform
3638
import Node.Stream (Readable, Writable)
37-
3839
import Unsafe.Coerce (unsafeCoerce)
3940

4041
-- YOLO
@@ -56,6 +57,23 @@ foreign import onBeforeExit :: Effect Unit -> Effect Unit
5657
-- | to exit with.
5758
foreign import onExit :: (Int -> Effect Unit) -> Effect Unit
5859

60+
-- | Install a handler for uncaught exceptions. The callback will be called
61+
-- | when the process emits the `uncaughtException` event. The handler
62+
-- | currently does not expose the second `origin` argument from the Node 12
63+
-- | version of this event to maintain compatibility with older Node versions.
64+
foreign import onUncaughtException :: (Error -> Effect Unit) -> Effect Unit
65+
66+
-- | Install a handler for unhandled promise rejectionsgq. The callback will be
67+
-- | called when the process emits the `unhandledRejection` event.
68+
-- |
69+
-- | The first argument to the handler can be whatever type the unhandled
70+
-- | Promise yielded on rejection (typically, but not necessarily, an `Error`).
71+
-- |
72+
-- | The handler currently does not expose the type of the second argument,
73+
-- | which is a `Promise`, in order to allow users of this library to choose
74+
-- | their own PureScript `Promise` bindings.
75+
foreign import onUnhandledRejection :: forall a b. (a -> b -> Effect Unit) -> Effect Unit
76+
5977
foreign import onSignalImpl :: String -> Effect Unit -> Effect Unit
6078

6179
-- | Install a handler for a particular signal.

0 commit comments

Comments
 (0)