Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript Error Handling

JavaScript Error Handling

Points mostly from a talk originally given at #MWRC by @xjamundx

Jake Trent

March 19, 2014
Tweet

More Decks by Jake Trent

Other Decks in Programming

Transcript

  1. THROW - TJ FONTAINE • throw for programmer errors (eg,

    missing required parameter) • https://us- east.manta.joyent.com/ dap/public/drop/er2.htm
  2. ALWAYS USE ERROR OBJECTS • Not simply strings • Errors

    contains stack traces • Will provide debug value
  3. CREATE CUSTOM ERRORS • Extend Error - still of type

    Error • New, specific type • Can attach other helpful properties (eg, data associated at time of error) • Other standard properties in Appendix: https://us- east.manta.joyent.com/dap/public/drop/er2.htm
  4. CREATE CUSTOM ERRORS (2) ! function SpecialError(message, specialInfo) { Error.captureStackTrace(this,

    arguments.callee) this.message = message this.name = ‘SpecialError' this.specialInfo = specialInfo } ! SpecialError.prototype = Object.create(Error.prototype)
  5. CALLBACK W/ ERROR • Common pattern in Node • If

    action creates error, pass as first parameter in callback
  6. CALLBACK W/ ERROR (2) function doManyThings(done) { doAsync(function (err, data)

    { if (err) return done(err) // … done(null, data) }) }
  7. DOMAINS AREN’T COOL • core team is not pushing the

    concept • https:// nodefirm.hackpad.com/ Node-Error-Handling- Summit-uXFi4FUg8Td
  8. MAKE ERRORS TO CLIENTS CONSISTENT • Pass all errors through

    a common error serializer • Client can also handle consistently
  9. USE EXPRESS DEFAULT ERROR HANDLER • Good catch-all • Avoid

    potential infinite loop bug in Express
  10. ON(‘UNCAUGHTEXCEPTION’) SHOULD ALWAYS EXIT • Call process.exit() • Otherwise, stuff

    hangs • Long-running process might never complete for client; wait for timeout • Resources can be leaked (eg, db connections)
  11. RESOURCES • MWRC SLIDES FROM @XJAMUNDX 
 HTTPS://CLOUDUP.COM/IHRJZBVDIFZ ! •

    CORE TEAM NOTES
 HTTPS://NODEFIRM.HACKPAD.COM/NODE-ERROR-HANDLING-SUMMIT-UXFI4FUG8TD ! • TJ FONTAINE ERROR HANDLING
 HTTPS://US-EAST.MANTA.JOYENT.COM/DAP/PUBLIC/DROP/ER2.HTM