Download - Node.js exception handling
![Page 2: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/2.jpg)
Node.js • is a still a relatively new programming language
• Node.js crashes for any ‘uncatch error’
• Most developers are confused when to use
• try/catch
• pass error to a callback
• throw an error
![Page 3: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/3.jpg)
error vs. exception• an error = an instance of the Error class
• can be created
• can be thrown
• can passed to callback
• a thrown error = an Exception
![Page 4: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/4.jpg)
errorsOperational Errors
(run-time issue, not bug)Programmer Errors
(bug)
• out-of-memory • connection issues • timeout • server return 500 HTTP code • socket hang-up
• no ‘undefined’ check • no callback for asynchronous
function • object type error
a correct code must be able to deal with operation error
![Page 5: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/5.jpg)
Best Practices for dealing with operation errors
• Always put asynchronous code into try/catch (otherwise Node.js crashes if uncaught error happens)
• Synchronous code: return error.
• ENONT error why trying to open a file: create file the first before read it
• socket hang-up: write code to reconnect if needed
• ready to deal with invalid JSON - using try/catch
• retry with 5xx HTTP error
• no idea how to deal with unthinkable/-solvable issue - such as ENOMEM: log the error and crash (intentionally)
![Page 6: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/6.jpg)
Dealing with programmer errors
• crash immediately
• how: throw new Error(‘ehh’);
…. if you have a restarter in place (like Bluemix)
…. or just use: forever.js
![Page 7: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/7.jpg)
• instead of ‘callback(result)’ use ‘callback(err, result)’
• because: either ‘result’ or ‘err’ is NULL!
![Page 8: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/8.jpg)
Error class
• has {name, message, stack}
• if you create an Error instance manually, at least provide {name, message}
![Page 9: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/9.jpg)
last chance…
• use process.on(‘uncaughtException’, function() {<do_sth>}); —> Global Catch
• it’s NOT recommended…but what if your app’s just crashed without leaving a trace?
![Page 10: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/10.jpg)
Automated Test
• Crucially important
• start with unit-test for all modules
• then integration test
• no test, no CI, you are in the coding hell!
![Page 11: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/11.jpg)
and … think about this
![Page 12: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/12.jpg)
Function - write it the right way -
• first argument ist error
• last ist callback
• use return (return callback(null, data); or return(err)
![Page 13: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/13.jpg)
Sources• Error Handling in Node.js https://
www.joyent.com/developers/node/design/errors
• Node.js Best Practices: http://www.slideshare.net/the_undefined/nodejs-best-practices-10428790
• Continuous Development: http://blog.risingstack.com/continuous-deployment-of-node-js-applications/
![Page 14: Node.js exception handling](https://reader034.vdocument.in/reader034/viewer/2022042700/559444521a28ab0c308b4781/html5/thumbnails/14.jpg)
–Philip Sidney
“Either I will find a way,or I will make one”