How to catch, handle, detect general error on page when app crashs (syntax erorr, program error etc.) in Nodejs app

Hi all,

I have been facing with handling an error in Nodejs app. What I want to do is handle or catch any type of

error and programmatically do something (for example create a bash script which refreshs when error

occurs or other activities).

The problem is if lets say the syntax error happens at the very beginning of my code (for example mistyping

variable value or etc.) then I cant handle it, it is totally impossible to me to handle programmatically when app

crashes. You could see example an an example in the link below

https://glitch.com/~my–firstapp

If I could detect the crash inside my code then I would inject some code which runs a bash script which

does any kind of activity I want (like refresh, import from github or etc).

Could you please help me with this issue, thanks!

For the case where node exits, such as syntax error … One way is to add a shell command on the end of the npm start script. For example, modify package.json to include …

"scripts": {
    "start": "node server.js ; echo 'showing exit value' $? >> /app/mylog.txt"
},

This appends the exit code of server.js to a file, which is 1 on most error exits, and 0 on success.

You could put your own shell script and pass it the exit code, such as …

"scripts": {
    "start": "node server.js ; ./mybashscript.sh $?"
},

It may not prevent the server from automatically restarting, depending on what you have in watch.json

For catching errors normally, use try … catch blocks in your script.

2 Likes

Thank you very much for your reply @mishavee! I have tried to do what you suggested but unfortunately I couldnt do that, may be because of lack of my experience in shell scripting. But definitely you answer gave my a clue for further investigation. I really want to thank you for this reason!!!

So after understanding how "scripts": {} in package.json might be powerful (idea came after reading your suggested solution) I searched on internet and found this article. There it is said that it is possible to trigger nodemon by "events": { "crash": "" } in nodemon.json file. Then I just added to crash event another pop.js file and wrote some logic into that.
What it does actually with node-cmd module it fetchs initial resource from github and updates app.js file with some delay (using setTimeout function).

By using this approach I could realize the scenario when for example lets say in app.js file I have code which scrapes external resource. If some changes happened in external resource then my glitch project would crash. As soon as it happens nodemon’s "events": { "crash": "" } is triggered and pop.js file executed. Which means every 5 minutes it updates app.js file as per the source in github.

So from my side it is enough just make changes locally after being informed about changes in external resource and push them to github and next time when pop.js script updates app.js it will work in normal regime because crash event disappears .

pop.js script continiues the attempt to fetch from github until I fix the reason of crach event.

I knew the way of syncing with github by webhook but I just chose this way, sorry if it is so complicated) I just wanted to share with you with my method of solving. Anyway you can see all of said in live project following this link Thanks again for your reply @mishavee, all the best to you!

1 Like

Nice find @ertugrulakash! nodemon looks really useful for setups like yours. :slight_smile: