Best practice for use of Done and Error

Is there a best practice recommendation for the use of done() and error() in contrib nodes? I notice that the email node calls done() or error() when it is finished (as picked up by Complete and Catch nodes) but never both, whereas the http request node calls done() or both done() and error(). It may even call just error() under some circumstances, I am not certain.

[Edit] I realise I have been a bit sloppy in my wording above as now done() is used both for normal and error conditions. This may be a better way of phrasing the question:
Is there a best practice recommendation for the triggering of Complete and Catch in contrib nodes? I notice that the email node triggers either Complete or Catch but never both (as far as I can see). The http request node sometimes triggers Complete and sometimes triggers both Catch and Complete. It may even trigger just Catch under some circumstances, I don't know.

I don't know if it's best practice but have You read this info about error handling?

Hi @berijan, welcome to the forum.

I don't think either of those address the issue of whether a node should call done() as well as error() when there is an error. Or perhaps I missed it.

Ok, I missunderstod the problem but I've seen this and there they send Done with an optional error info

" If a node calls done() , it will trigger any ‘Complete’ nodes in the workspace that have been configured to target that node. If done is called with an error, then it will trigger any ‘Catch’ nodes, as with existing calls to node.error(err,msg) ."

After a quick view it looks like You always sen a Done and if an error You send Done(err)

Oh yes, I was forgetting the detail of how it works now. Looking at that blog it seems that the recommended way will either trigger Complete or Catch, but not both, which is what the email node does. That makes the http request node a bit of an anomaly as it sometimes triggers Complete and Catch.

I will edit my initial post to make my question a bit clearer.

Just to keep in mind that’s the new way that is not backwards compatible. Pre 1.0 or 1.2 compatibility would mean to use something like:

(done) ? done(err) : node.error(err);

As those older nodered versions don’t have the done function and in the new functions you should only call node.error() if for some reason you already called done() and an error happens afterwards for example in the on close function or doing async work.
At least that’s how I understand it.

I also previously had the discussion when to call done in some edge cases here:

1 Like

Thanks for that.

Sorry just saw you actually were one of the people who helped me back in the thread i linked :see_no_evil:

1 Like

I am forever searching for stuff, finding helpful answers and then realising that I wrote the helpful answer myself :slight_smile: