$30 off During Our Annual Pro Sale. View Details »

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. JAVASCRIPT ERROR HANDLING FROM A TALK BY @XJAMUNDX AT #MWRC

  2. TRY/CATCH • Use for JSON.parse • Don’t use like a

    Java programmer
  3. DON’T THROW • Only as a last resort • Nothing

    else you can do
  4. THROW - TJ FONTAINE • throw for programmer errors (eg,

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

    contains stack traces • Will provide debug value
  6. 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
  7. 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)
  8. CALLBACK W/ ERROR • Common pattern in Node • If

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

    { if (err) return done(err) // … done(null, data) }) }
  10. NAME ANONYMOUS FUNCTIONS • Provides name in stack trace

  11. NAME ANONYMOUS FUNCTIONS (2) doSomethingWithCallback(function veryCallback() { // if I

    throw an error, stack trace <3 })
  12. DOMAINS AREN’T COOL • core team is not pushing the

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

    a common error serializer • Client can also handle consistently
  14. MAKE ERRORS TO CLIENTS CONSISTENT (2) res.json(400, formatError(err))

  15. USE EXPRESS DEFAULT ERROR HANDLER • Good catch-all • Avoid

    potential infinite loop bug in Express
  16. USE EXPRESS DEFAULT ERROR HANDLER (2) app.use(function (err, req, res,

    next) { res.json(500, formatError(err)) })
  17. 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)
  18. ON(‘UNCAUGHTEXCEPTION’) SHOULD ALWAYS EXIT (2) process.on('uncaughtException', function(err) { hurryAndWriteYourWill(err) process.exit()

    });
  19. 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
  20. https://speakerdeck.com/jaketrent/javascript-error-handling