Slide 1

Slide 1 text

JAVASCRIPT ERROR HANDLING FROM A TALK BY @XJAMUNDX AT #MWRC

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

DON’T THROW • Only as a last resort • Nothing else you can do

Slide 4

Slide 4 text

THROW - TJ FONTAINE • throw for programmer errors (eg, missing required parameter) • https://us- east.manta.joyent.com/ dap/public/drop/er2.htm

Slide 5

Slide 5 text

ALWAYS USE ERROR OBJECTS • Not simply strings • Errors contains stack traces • Will provide debug value

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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)

Slide 8

Slide 8 text

CALLBACK W/ ERROR • Common pattern in Node • If action creates error, pass as first parameter in callback

Slide 9

Slide 9 text

CALLBACK W/ ERROR (2) function doManyThings(done) { doAsync(function (err, data) { if (err) return done(err) // … done(null, data) }) }

Slide 10

Slide 10 text

NAME ANONYMOUS FUNCTIONS • Provides name in stack trace

Slide 11

Slide 11 text

NAME ANONYMOUS FUNCTIONS (2) doSomethingWithCallback(function veryCallback() { // if I throw an error, stack trace <3 })

Slide 12

Slide 12 text

DOMAINS AREN’T COOL • core team is not pushing the concept • https:// nodefirm.hackpad.com/ Node-Error-Handling- Summit-uXFi4FUg8Td

Slide 13

Slide 13 text

MAKE ERRORS TO CLIENTS CONSISTENT • Pass all errors through a common error serializer • Client can also handle consistently

Slide 14

Slide 14 text

MAKE ERRORS TO CLIENTS CONSISTENT (2) res.json(400, formatError(err))

Slide 15

Slide 15 text

USE EXPRESS DEFAULT ERROR HANDLER • Good catch-all • Avoid potential infinite loop bug in Express

Slide 16

Slide 16 text

USE EXPRESS DEFAULT ERROR HANDLER (2) app.use(function (err, req, res, next) { res.json(500, formatError(err)) })

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

ON(‘UNCAUGHTEXCEPTION’) SHOULD ALWAYS EXIT (2) process.on('uncaughtException', function(err) { hurryAndWriteYourWill(err) process.exit() });

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

https://speakerdeck.com/jaketrent/javascript-error-handling