exceptions in function nodes - why can't I 'catch' some of them if using 'required' code?

1,216 views
Skip to first unread message

Simon H

unread,
Oct 26, 2017, 4:03:44 AM10/26/17
to Node-RED
Hi all,

I'm known for abusing the function nodes without enough knowledge of the VM structure, etc.  :).

So, one of the things I do quite a lot is expose 'require' and use raw nodejs modules; it's a lot easier than creating a node-red node when just 'playing' with something.

However, often I find that I get exceptions which I can't catch, e.g. inside a function node, after exposing require through globals:

var require = global.get('require');
var net = require('net');
var fs = require('fs');

var sockname = 'test2.sock';
try{
   
var path = '/tmp/'+sockname;
   
var stream = net.connect(path, function(){
            stream
.write(msg.payload);
            node
.warn('sent '+msg.payload.length);
            stream
.end();
   
});
} catch (e){
    node
.warn(e);
}


fails like below when the IPC socket is not present:

Oct 26 08:59:32 raspberrypi Node-RED[12773]: 26 Oct 08:59:32 - [red] Uncaught Exception:
Oct 26 08:59:32 raspberrypi Node-RED[12773]: 26 Oct 08:59:32 - Error: connect ENOENT /tmp/test4.sock
Oct 26 08:59:32 raspberrypi Node-RED[12773]: at Object.exports._errnoException (util.js:1020:11)
Oct 26 08:59:32 raspberrypi Node-RED[12773]: at exports._exceptionWithHostPort (util.js:1043:20)
Oct 26 08:59:32 raspberrypi Node-RED[12773]: at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
Oct 26 08:59:33 raspberrypi kernel: [39760.147119] bcm2835-v4l2: error 0 waiting for frame completion
Oct 26 08:59:33 raspberrypi systemd[1]: nodered.service: main process exited, code=exited, status=1/FAILURE

Other exceptions do seem to be 'catchable' from within a function node, some seem to be catchable with the 'catch' node-red node (but not this one).


Is there an easy way to explain what is happening here at the base level?
(I'm not looking at how to avoid the ENOENT; I'm looking for a way to have in not crash when it happens, 'cos the next error is ECONNREFUSED - also uncatchable).

best regards,

Simon
(p.s. the node-opencv bindings are really bad for this; they seem to throw or blow at the slightest error, and it's very rare that I can catch when it does; I give the above example because it's easily reproducible.  If you remove the fs.existsSync, then it will except with filenotfound for paths which don't exist).


Nick O'Leary

unread,
Oct 26, 2017, 4:13:03 AM10/26/17
to Node-RED Mailing List
Hi Simon,

because of the asynchronous nature of many node.js modules, you need to register a handler for the 'error' event on the appropriate object- in this instance, your 'stream' object:

stream.on('error', function(err) {
   // do something about this error'
});


Nick




--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/376ef5bd-b872-4273-96a9-edee1afda43f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon H

unread,
Oct 26, 2017, 2:00:18 PM10/26/17
to Node-RED
thanks nick, solves the issue with net.
But I'm not sure how to approach the opencv stuff...
s

Julian Knight

unread,
Oct 27, 2017, 8:03:43 AM10/27/17
to Node-RED
Not sure if a function node allows access to node.js's process object but if it does, this may help:

Julian Knight

unread,
Oct 27, 2017, 8:23:10 AM10/27/17
to Node-RED
Ah, no it isn't so you would need to expose it or switch to using unsafe-function instead.

Nick O'Leary

unread,
Oct 27, 2017, 8:25:21 AM10/27/17
to Node-RED Mailing List
Noooo!!!! Do not add your own handler for the global uncaughtException as a lazy way of catching errors you should be handling yourself.

Node-RED already registers a handler for uncaughtException, and stops the runtime when it occurs.



--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Julian Knight

unread,
Oct 27, 2017, 12:16:57 PM10/27/17
to Node-RED
Ha, good point, well made.


On Friday, 27 October 2017 13:25:21 UTC+1, Nick O'Leary wrote:
Noooo!!!! Do not add your own handler for the global uncaughtException as a lazy way of catching errors you should be handling yourself.

Node-RED already registers a handler for uncaughtException, and stops the runtime when it occurs.


On 27 October 2017 at 12:23, Julian Knight <j.kni...@gmail.com> wrote:
Ah, no it isn't so you would need to expose it or switch to using unsafe-function instead.

On Friday, 27 October 2017 13:03:43 UTC+1, Julian Knight wrote:
Not sure if a function node allows access to node.js's process object but if it does, this may help:

https://nodejs.org/docs/latest-v6.x/api/process.html#process_event_uncaughtexception

On Thursday, 26 October 2017 19:00:18 UTC+1, Simon H wrote:
thanks nick, solves the issue with net.
But I'm not sure how to approach the opencv stuff...
s


On Thursday, October 26, 2017 at 9:13:03 AM UTC+1, Nick O'Leary wrote:
Hi Simon,

because of the asynchronous nature of many node.js modules, you need to register a handler for the 'error' event on the appropriate object- in this instance, your 'stream' object:

stream.on('error', function(err) {
   // do something about this error'
});


Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Simon H

unread,
Oct 30, 2017, 4:10:25 AM10/30/17
to Node-RED
Yes, the answer is to use mature libraries which don't throw but rather return errors in async code :).
Now I've got to building node-opencv, when I get big problems, i'll investigate and try to fix....

Julian Knight

unread,
Oct 30, 2017, 6:09:29 PM10/30/17
to Node-RED
I agree but sometimes a library does what you want but may throw an odd exception and it may be hard to get a fix. Node.JS is designed to throw on error of course so that you don't end up in an unknown state. Very laudable but also very hard when you are layering several complex system components above the base (e.g. Express, Node-RED and other libraries).

Anyway, sorry, I went off on a tangent without understanding that Node-RED was already doing it. Hopefully Nicks response has given you the clues to move forward.
Reply all
Reply to author
Forward
0 new messages