javascript js - How to let react electron ignore undefined error?

create app (6)

The code has many A.test, I can't write every where if(A) A.test

But why? You can use some editor for multi file editing. So you can replace A.test() to safeTest(A) function.

export const safeTest = (Obj) => {
 if (Obj) {
 } else {
  // Any action you want

React electron on windows, if A is null, call A.test will make the application stop working, then the user has to close the application and restart it. How to let react ignore the error, and continue work. The code has many A.test, I can't write everywhere if(A) A.test. If this can't be resolved, can I print the error on the web view? So I don't have to remote visit the user's computer to see the console error.

If you want to make the minimal effort to catch all the unhandled errors from both main and renderer processes within Electron as well as showing them to the user via a dialog, the easy way is to use electron-unhandled which does exactly that:

After having installed it (npm i electron-unhandled), in both your main and renderer entry files (likely their root index.js), you just have to add, at the beginning:

const unhandled = require('electron-unhandled');

unhandled({ showDialog: true });

Now, that being said, it's a good practice to use a global error catcher but it's a really bad one if you use only that. You should try covering your error handling more accurately, at least method by method:

  • .then() { ... }.catch(err => ...) for your promises,
  • (..., (err, res) => { if (err !== null) { ... } ... ) for your callbacks,
  • try { ... } catch(err) { ... } for non-async or await-based code code parts.

And, as a side-note, I myself created a dependenciless library to make it safe and easy to create a global errors dictionary in order to well-organize your errors but there are other alternatives if this one doesn't fit your needs.

I guess the best possible solution to this would be surrounding your A.test in try and catch. In this case what you can do is catch the error is A is null and perform some error page from your side incase you want it or just keep the error silent incase you dont want to perform any operation and suppress the error and continue execution.

You can also wrap A.test in a function with try-catch and use that function instead of A.test. In this way you can avoid multiple try-catch block and you can handle the error as per your requirement here.


I think the solution is to use react error boundaries, as suggested in the console.

You already pointed out that you're using error boundaries, so after testing your scenarios in this fiddle I believe your implementation might be incorrect.

Given a similar implementation for ErrorBoundary in the docs:

class ErrorBoundary extends React.Component {
  state = { hasError: '' };
  render() {
    return this.state.hasError ? (
      <span>Oops! Something went wrong:<br />{this.state.hasError}</span>
    ) : this.props.children;
ErrorBoundary.getDerivedStateFromError = (error) => ({ hasError: error.toString() });

This component will render the fallback when any of its children breaks.

Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI

It will look similar to:

    <ChatContent />

Now any error in ChatContent will be catch by ErrorBoundary giving you the opportunity to render the fallback like:

Oops! Something went wrong:
ReferenceError: test is not defined

I tend to favor using default props. You can set a value for the component to assign to a prop if the prop is passed in undefined. For example, if your component depends on an array nested within an object, you could set that value as an empty array by default. This is especially handy when your component depends on an array of results from an API call, but the component renders before the request finishes.

Your code is functionally the same to this code:

var a = 1;
function b() {
    var a = function() {}
    a = 10;

Using the function NAME() { ... } notation effectively puts that function declaration at the beginning of the current scope as a local (to that scope) declaration.

in fact if you execute

var a = 1;
var c= 2;
function b() {
    a = 10;
    function a() { alert(c) }

It will output:


Personally I don't use this kind of notation, I always explicitly use assignments.


javascript reactjs electron