Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: Is it possible to catch a "TypeError" and keep the server up?

Tags:

node.js

heroku

Somtimes I have a typerror in my code that crashes my server. I'm wondering if there's something I can add so it does not crashes after that happens. Here's more details to the crash:

2011-06-08T11:23:09+00:00 app[worker.1]: /app/lib/imap.js:1019
2011-06-08T11:23:09+00:00 app[worker.1]:         type: cur[0].toLowerCase(), subtype: cur[1].toLowerCase(),
2011-06-08T11:23:09+00:00 app[worker.1]:                                                     ^
2011-06-08T11:23:09+00:00 app[worker.1]: TypeError: Object FILENAME,Oportunidad especial de has no method 'toLowerCase'
2011-06-08T11:23:09+00:00 app[worker.1]:     at parseBodyStructure (/app/lib/imap.js:1019:53)
2011-06-08T11:23:09+00:00 app[worker.1]:     at parseBodyStructure (/app/lib/imap.js:1090:23)
2011-06-08T11:23:09+00:00 app[worker.1]:     at parseBodyStructure (/app/lib/imap.js:1000:18)
2011-06-08T11:23:09+00:00 app[worker.1]:     at parseBodyStructure (/app/lib/imap.js:994:13)
2011-06-08T11:23:09+00:00 app[worker.1]:     at parseFetch (/app/lib/imap.js:974:29)
2011-06-08T11:23:09+00:00 app[worker.1]:     at CleartextStream.<anonymous> (/app/lib/imap.js:180:11)
2011-06-08T11:23:09+00:00 app[worker.1]:     at CleartextStream.emit (events.js:64:17)
2011-06-08T11:23:09+00:00 app[worker.1]:     at CleartextStream._push (tls.js:285:31)
2011-06-08T11:23:09+00:00 app[worker.1]:     at SecurePair._cycle (tls.js:565:20)
2011-06-08T11:23:09+00:00 app[worker.1]:     at EncryptedStream.write (tls.js:97:13)
2011-06-08T11:23:09+00:00 heroku[worker.1]: Process exited
2011-06-08T11:23:09+00:00 heroku[worker.1]: State changed from up to crashed

Thanks

like image 369
donald Avatar asked Jun 08 '11 11:06

donald


1 Answers

Handle the uncaughtException event on the process.

process.on('uncaughtException', function (err) {
  console.log('Oh shit recover somehow');
});

Warning: This will catch all exceptions. If you don't bother to handle them properly you can corrupt state very badly.

like image 139
Raynos Avatar answered Nov 15 '22 11:11

Raynos