3

Am I right in saying that a simple error like...

arr.forEach   --> when arr is undefined

Will bring down THE ENTIRE node thread? Meaning all users would be disconnected?

I am amazed if this is the case because surely this is insanely inconvenient. Especially if I have multiple users all running chat applications.

And if this is the case how do I ensure there are no errors in my code EVER?

UPDATE :

I don't really see auto restart as a feature to get round this problem...

Pavlo
  • 43,301
  • 14
  • 77
  • 113
Exitos
  • 29,230
  • 38
  • 123
  • 178
  • possible duplicate of [how to automatically restart a node server?](http://stackoverflow.com/questions/16547996/how-to-automatically-restart-a-node-server) – JJJ May 14 '15 at 10:20
  • 1
    possible duplicate of [How do I prevent node.js from crashing? try-catch doesn't work](http://stackoverflow.com/questions/5999373/how-do-i-prevent-node-js-from-crashing-try-catch-doesnt-work) – Ben Fortune May 14 '15 at 10:35
  • That is a more appropriate answer than mine – jas- May 14 '15 at 11:05
  • If this error is in the main line of execution then yes. However, 99% of the time node is running a listener or handler or callback, which will crash yes, but node will of course continue running and listening and dispatching. In modern frameworks like koa 99% of the execution is happening within the context of what amount to big try-catch constructs which will simply fail, get caught by the nearest handler, and then life goes on. –  May 14 '15 at 11:21
  • What do you mean that auto restart doesn't get around the problem? That's how everyone does it. What downsides would it have? – JJJ May 14 '15 at 13:19
  • @Juhana Think of what it means treating the symptoms vs. treating the problem. You wouldn't need a server restart 99.9% of the time if your code is correct except for uncontrollable factors involving the server's integrity like internet connection, HDD space limited that might cause file errors, etc. – Patrick Roberts May 14 '15 at 16:09

3 Answers3

1

Whether in the browser or node, 99% of JS execution is running a listener or handler or callback or dispatched routine. If the listener crashes, then sure, it will crash and bad things could happen, but node (or the browser) will of course continue running and listening and dispatching.

In modern frameworks like koa, 99% of the execution is happening within the context of what amount to big try-catch constructs which will simply fail, perhaps trigger a promise failure, then get caught by the nearest handler, and life goes on.

If the error is in the main line of execution, where things are getting set up or launching or initializing, then yes, you will be out of luck, but all you have to do is get to the point of app.listen() without crashing.

So the answer to your question is: no, node error handling is not really this dangerous.

  • This is probably a reason why people don't write webservers in C++ much. (C++ has exceptions, but they don't help much if people write `thisIsNull->performAction();`) – Katana314 May 14 '15 at 13:43
  • When you don't set up an error handler for sockets of the `net` module, node *will* crash. If there is no catch along the way, any code in handling new data from the socket that generates an exception will also make node crash. – coyotte508 May 14 '15 at 13:43
0

You should probably use domains. Domains provide us a way to act on error events for all the event emitters and callbacks that are bound to a specific domain.

All the errors propogate to that domain without losing the context of the error. You can wrap a function in a domain call, subscribe to the error events and handle all the errors in one place.

Domains ensure that your function runs in isolation and it's errors don't bubble up to the process. It is a good way to prevent the node process to exit if the error is something the app should be able to recover from.

http://www.howtojs.org/understanding-exceptions-domains-in-nodejs/

Jeno Laszlo
  • 2,023
  • 18
  • 36
-1

typeof arr === 'array' prior to any iterations on objects created dynamically. Other languages crash on this exception as well.

peter_the_oak
  • 3,529
  • 3
  • 23
  • 37
jas-
  • 1,801
  • 1
  • 18
  • 30
  • @Juhana I don't think so, but the answer could use some improvement. – E_net4 May 14 '15 at 11:01
  • The code in the question is just an example. The question is "how to prevent Node from crashing on *any* error." – JJJ May 14 '15 at 11:02
  • 3
    Jas I am not looking for a code review for that single line, I was providing an example of code which could throw an error....I was looking for a solution like some sort of global exception catcher....thanks.... – Exitos May 14 '15 at 11:11
  • 1
    There is no type ``"array"``. ``typeof [] === "object"``. Which is unfortunate since ``typeof null === "object"`` too. This doesn't even solve the _wrong_ problem. – Patrick Roberts May 14 '15 at 16:16