Open In App

Error Handling in ElectronJS

ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime. In any application, Error, and Exception handling is a critical part of development. For Error and Exception Handling in NodeJS, Refer to the article: Exception Handling in Node. For Error and Exception Handling using JavaScript, Refer to the articles: 

Most languages and platforms provide built-in support for Error and Exception handling. Developers while developing an application irrespective of the language or the platform, should take care of some of the common exceptions which can occur in code, such as Handling NULL, dividing a number by 0, etc. Developers usually generically handle exceptions and errors apart from a few specific use cases which are related to their functionality. The list of Exceptions that can be thrown is non-exhaustive. We have already covered Error and Exception handling in NodeJS and JavaScript. The same techniques can also be used while developing an Electron application. However, many times a situation arises wherein a working code might break for unknown reasons or a bug in the application may cause an Exception or an Error to be thrown which can break the execution flow. They may also possess a security issue with the application. In such cases, we need a generic way to handle these Exceptions safely without breaking the application and it should also be compliant with the native system. In this tutorial, we will look at Error and Exception handling in an Electron application using the electron-unhandled external npm package. We assume that you are familiar with the prerequisites as covered in the above-mentioned link. For Electron to work, node and npm need to be pre-installed in the system.




Example: Follow the Steps given in Dynamic Styling in ElectronJS to set up the basic Electron Application. Copy the Boilerplate code for the main.js file and the index.html file as provided in the article. We will continue building our application using the same code base. Additionally, install the electron-unhandled package using npm. Using this external package, we can catch Unhandled Errors and Promise Rejections in our Electron Applications and handle them safely. 

npm install electron-unhandled --save

Also, perform the necessary changes mentioned for the package.json file to launch the Electron Application. We will continue building our application using the same code base. The basic steps required to set up the Electron application remain the same. 



package.json: 

{
    "name": "electron-catch",
        "version": "1.0.0",
            "description": "Catch Unhandled Errors and Promise Rejections in Electron",
                "main": "main.js",
                    "scripts": {
        "start": "electron ."
    },
    "keywords": [
        "electron"
    ],
        "author": "Radhesh Khanna",
            "license": "ISC",
                "dependencies": {
        "electron": "^8.3.0",
            "electron-unhandled": "^3.0.2"
    }
}

Output: 

Error Handling in Electron: The electron-unhandled npm package can be directly used in the Main Process and the Renderer Processes of the application to catch unhandled Errors. This package requires Electron 5+ to work without any issues. This package is compliant and works without any errors as of Electron v8.3.0 and it is regularly updated. For more detailed information on this package, version updates and changelogs, refer to the link: https://www.npmjs.com/package/electron-unhandled. This package has been adopted and is used by Electron itself for demo purposes.




<h3>Catch Unhandled Errors and Promise Rejections in Electron</h3>
<button id="generate">
    Generate Errors
</button>
<!-- Adding Individual Renderer Process file -->




const electron = require('electron')
const electron = require('electron');
// Importing unhandled.
const unhandled = require('electron-unhandled');
 
unhandled({
    logger: () => {
        console.error();
    },
    showDialog: true,
    reportButton: (error) => {
        console.log('Report Button Initialized');
    }
});
 
unhandled.logError(Error, {
    title: 'Title of the Box'
});
 
let generate = document.getElementById('generate');
// Throwing a Generic Error from the Error Interface
generate.addEventListener('click', () => {
    throw new Error('THIS IS AN ERROR');
});

Explanation: In the above application, upon clicking the HTML DOM Button element, we are throwing a new Error with a message. Since we have not used try-catch block, this is an unhandled Error and we will handle this using the electron-unhandled npm package. The unhandled(options) Instance method does not have a return type and it takes in the following parameters. 

The electron-unhandled package also provides us with the unhandled.logError(error, options) Instance method which can be used to log/print/display caught errors in Error Handling such as in catch block of the try-catch statement. This Instance method uses the same configurations as defined in the unhandled() Instance method or calls the default configurations. Hence, in our case, we have chosen to display an Error Dialog box in the unhandled() Instance method and the same will be done for this method as well. It takes in the following parameters. 

At this point, upon launching the Electron application, we should be able to see the electron-unhandled package produce two custom Error Dialog boxes, one at the launch of the application and the other at the Generate Errors button click. 

Output: 


Article Tags :