node.js exception handling

14
Node.js Exception Handling Do it the right way Minh Hoang - http://minhhoang.de

Upload: minh-hoang

Post on 02-Jul-2015

1.182 views

Category:

Software


1 download

DESCRIPTION

Exception handling is always an important topic. This presentation will provide couple of good practices to deal with Errors & Exceptions happing in Node.js. Keep those in mind, you will write a better code for a more stable software. Happy Coding!

TRANSCRIPT

Page 1: Node.js exception handling

Node.js Exception Handling

Do it the right way

Minh Hoang - http://minhhoang.de

Page 2: Node.js exception handling

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

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

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

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

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

• instead of ‘callback(result)’ use ‘callback(err, result)’

• because: either ‘result’ or ‘err’ is NULL!

Page 8: Node.js exception handling

Error class

• has {name, message, stack}

• if you create an Error instance manually, at least provide {name, message}

Page 9: Node.js exception handling

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

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

and … think about this

Page 12: Node.js exception handling

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

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

–Philip Sidney

“Either I will find a way,or I will make one”