I'm actually experiencing some troubles with Nodered. Following my raspberry pi crash I needed to redo the complete setup, so I install a fresh Nodered v3 and used a flow backup that I made not long ago.
The trouble is that now a specific node is making a mess and nodered is restarting after the flow start running and the error occuring. I need to modify the flows.json to delete this node so it stops restarting.
The node I believe to get me trouble is the surepetcare one.
Welcome to Node-RED
26 Mar 11:58:30 - [info] Node-RED version: v3.0.2
26 Mar 11:58:30 - [info] Node.js version: v16.19.1
26 Mar 11:58:30 - [info] Linux 6.1.19-v7+ arm LE
26 Mar 11:58:33 - [info] Loading palette nodes
26 Mar 11:58:39 - [info] Worldmap version 2.34.0
26 Mar 11:58:40 - [info] Dashboard version 3.4.0 started at /ui
26 Mar 11:58:40 - [info] Settings file : /home/klose54/.node-red/settings.js
26 Mar 11:58:40 - [info] Context store : 'default' [module=memory]
26 Mar 11:58:40 - [info] User directory : /home/klose54/.node-red
26 Mar 11:58:40 - [warn] Projects disabled : editorTheme.projects.enabled=false
26 Mar 11:58:40 - [info] Flows file : /home/klose54/.node-red/flows.json
26 Mar 11:58:40 - [info] Server now running at http://127.0.0.1:1880/
26 Mar 11:58:40 - [warn]
Your flow credentials file is encrypted using a system-generated key.
If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.
You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
26 Mar 11:58:44 - [info] Starting flows
26 Mar 11:58:44 - [info] [surepet-credentials:b5cc3c783ef12564] We have a username and password, attempting to authenticiate.
....................
26 Mar 11:59:16 - [red] Uncaught Exception:
26 Mar 11:59:16 - [error] Error: Get state failed: unauthenticated
at SurePetCareClient.getState (/home/klose54/.node-red/node_modules/sure-pet-care-client/dist/client.js:39:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
The thing is it seems to be and authentification problem, but when the node is manually called it works. Everytime I put an inject node before so I automatically call the node, it keeps getting me this error and nodered is restarting ...
I'm using it as the function node with only credentials in it and getting payload as I always do. I've never made anything specific to handle errors. How should I make this ?
Thank you for the safe command, this will be useful.
If it is only on startup that you are getting the problem, then a workaround may be to configure the inject node to trigger a little time after startup. Set it to 60 seconds initially and see what happens.
It seems to be working for now, I changed the inject nodes as you said, reboot nodered for a few times and its working fine, let's see for a longer period. Thanks for the idea.
Possibly, the bug is being triggered if node red starts up before the network is connected, which is likely if you are using wifi. In the node red systemd startup script, there is a line you can uncomment which will make it wait for a time sync on boot (which means the network must be running) before starting node-red, so you could try that instead. It does have the disadvantage that if you it cannot connect then node red will never start, which can be confusing.
So since that i delayed for a few seconds the first call of this node does improve and never does the same issue again.
But now it happens a lot of time that nodered restart on this :
[red] Uncaught Exception:
[error] Error: Get state failed: received 504 Gateway Time-out
at SurePetCareClient.getState (/home/klose54/.node-red/node_modules/sure-pet-care-client/dist/client.js:41:31)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Since yesterday I have this like 5 times per hour, which can an issue. Maybe the contrib node is not working well with a new version of something ? It seems to be not updated since 3 years.
I believe that the creator of this contrib is no longer active on this one and that the github message may be a no go ...
The problem is that everytime the server seems to be down for a short time, my nodered keeps rebooting.
Is there anybody that knows what to change in this part of the code for the node to make an error message instead of crashing on a 504 gateway time-out failure ?
function SurePetCredentials(n){
RED.nodes.createNode(this, n);
this.username = n.username || this.credentials.username;
this.password = n.password || this.credentials.password;
this.client = new SurePet.SurePetCareClient();
if (this.username && this.password){
this.log("We have a username and password, attempting to authenticiate.")
try{
this.client.authenticate(this.username, this.password);
this.status({fill:"green",shape:"dot",text:"Connected."});
} catch (err){
this.status({fill:"red",shape:"ring",text:err});
}
}
}
I've made the edit in the surepet.js, It has been well deployed and running for a few minutes now, I will give you some news after some runtime.
EDIT : In fact, I thought I made the changes but I have two surepet.js files in my directory, one in /node_modules/node-red-contrib-surepet/surepet.js where I made the edit, and an other in /.node-red/node_modules/node-red-contrib-surepet/surepet.js where when I try to do the changes you gave me, nodered cant restart and it gaves me this :
[info] Waiting for missing types to be registered:
[info] - surepet-credentials
[info] - surepet
'await' expressions are only allowed within async functions and at the top levels of modules.
So the soft added a async at
async function SurePetCredentials(n){
With that, VSCode stated no problem detected. But I still have a trouble on deploy of nodered that indicate the same error waiting for missing types, with this line showing during the deploy
[node-red-contrib-surepet/surepet] TypeError: Cannot convert undefined or null to object (line:55)
client.authenticate is an async function (I pointed that out several posts above). You need to use await or use promise then/catch as I demonstrated in my previous post.