Skip to content

Commit c13f75e

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 c13f75e

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-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: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ module Node.Process
2424

2525
import Prelude
2626

27-
import Effect (Effect)
28-
2927
import Data.Maybe (Maybe)
3028
import Data.Posix (Pid)
3129
import Data.Posix.Signal (Signal)
3230
import Data.Posix.Signal as Signal
31+
import Effect (Effect)
32+
import Effect.Exception (Error)
3333
import Foreign.Object as FO
3434
import Node.Platform (Platform)
3535
import Node.Platform as Platform
3636
import Node.Stream (Readable, Writable)
37-
3837
import Unsafe.Coerce (unsafeCoerce)
3938

4039
-- YOLO
@@ -56,6 +55,23 @@ foreign import onBeforeExit :: Effect Unit -> Effect Unit
5655
-- | to exit with.
5756
foreign import onExit :: (Int -> Effect Unit) -> Effect Unit
5857

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

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

0 commit comments

Comments
 (0)