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.

- 4,507
- 4
- 49
- 45
-
Electon is just a backend server app on node and chrome based engine on frontend. Don't wait from browser to ignore errors. Just write clean, good code with checking `if (something.MemberList) { then do operation }` You've to handle possible situations to avoid errors interrupting code execution. – num8er Nov 18 '18 at 14:09
-
2Wrap the code with `try` `catch` – Oram Nov 18 '18 at 14:12
-
@num8er No, your code will stop the software as well, `if(something&&Object.keys(something).includes('MemberList')){` is right! I have too many code like this. – Gank Nov 18 '18 at 14:27
-
@Oram I use `ErrorBoundary`, but it can't print the error code like `A.test is undefined` – Gank Nov 18 '18 at 14:29
-
`if(something && something.MemberList)` or `if(something && something.hasOwnProperty('MemberList'))` is enough. ok. but then what's Your problem? just make sure Your app works correct and You'll not get that errors. Find that issue and fix it. – num8er Nov 18 '18 at 14:32
-
@num8er `if (!self.sessions.map(e => e ? e.UserName : '').includes(userid)) {` like this, the `e` can be null, then the software stop working util the user restart it . I mean the code is too much, and this can't be a fatal error, how to make the software continue working, but react will white screen, the user can't click any more. – Gank Nov 18 '18 at 14:37
-
@Gank unfortunately You cannot make it continue to work. It's not nodejs (backend) part of code where You can do: `process.on('uncaughtException', (err) => { /* do nothing */ });` – num8er Nov 18 '18 at 14:43
-
2maybe You want this? https://stackoverflow.com/questions/39249886/is-it-possible-to-catch-exceptions-of-renderer-processes-in-electrons-main-proce – num8er Nov 18 '18 at 14:44
-
@num8er It works good in js code, but in the view code: `this.state.ServerNameList.map((value,key) => )` The `vadlue` will make the react white screen as well. – Gank Nov 18 '18 at 15:03
-
What is `vadlue`? Whether is it `value`? – Kousic Nov 19 '18 at 13:33
-
You should show the code for the component that is failing. Otherwise, you're only going to get generic suggestions, "use try/catch", "use error boundaries" etc. – CodeDraken Nov 20 '18 at 21:33
-
The better way is using a component lifecycle method to handle an error view. you can see [my naswer](https://stackoverflow.com/a/53419941/6877799) – AmerllicA Nov 23 '18 at 18:25
6 Answers
NOTE
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:
<MyReactApp>
<ErrorBoundary>
<ChatContent />
</ErrorBoundary>
</MyReactApp>
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

- 1,822
- 15
- 32
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) {
Obj.test();
} else {
// Any action you want
}
}

- 2,208
- 9
- 38
- 49
-
If you look at the comments on the question it also makes reference to this scenario: `map((value,key) => )` which is the opposite to safe code. The user is probably working in a project coded by third parties with untrustable code quality. – Gatsbimantico Nov 19 '18 at 12:22
-
23rd-party code - probably. But I didn't see anything about it. What I really read, was `The code has many A.test, I can't write every where if(A) A.test` – indapublic Nov 19 '18 at 12:57
It is hard to offer an answer to your question because I don't see your project codes, but if your react version is 16 you can use a special component lifecycle method that name is componentDidCatch
.
Inside this method you will have these values:
componentDidCatch(error, info) {
// Do something with error and info
}
Even you can use setState
inside this method and show you want. I think this method can help you for your second desire, the printing error
in web view.

- 29,059
- 15
- 130
- 154
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.

- 581
- 3
- 8
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 orawait
-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.

- 6,433
- 5
- 39
- 60
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.

- 1,614
- 1
- 16
- 24