Search code examples
node.jscrashelectrongoogle-chrome-devtoolslibp2p

Electron handle crashes gracefully


I'm trying to implement libp2p inside an electron application, which (obviously) has node support turned on.

I tried catching errors with the following code:

const errorHandle = `
window.onerror = (err) => {
    console.log(err);
};
process.on("uncaughtException", function (err) {
    console.log(err);
});
process.on("unhandledRejection", function (err) {
    console.log(err);
});
console.log("Injected startup code")`
await mainWindow.loadFile("./public/index.html");
await mainWindow.webContents.executeJavaScript(errorHandle);

However if I throw an error deliberately, or by accident, the app crashes, and gets reloaded, and I can't see the error, because chrome devtools clears its console, and prints out the "Devtools disconnected" message.

2 examples of error throwing:

console.error("test");
node.connect("INVALID_ADDRESS");

How can I properly handle errors, so electron (or node) doesn't crash?


Solution

  • As it turns out, the error was handled correctly, however the event was connected to a form, which by default refreshes the page if submitted. I had to cancel the default event by:

    <form onsubmit="return false" on:submit={submit}></form>