Enterprise JavaScript Error Handling (Ajax Experience 2008)

52
E nterpris e JavaS cript E rror Handling Nicholas C. Zakas Principal Front End Engineer, Yahoo!

description

My presentation at Ajax Experience 2008 about error handling strategies for JavaScript.

Transcript of Enterprise JavaScript Error Handling (Ajax Experience 2008)

Page 1: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Enterprise JavaS cript Error Handling

Nicholas C. ZakasPrincipal Front End Engineer, Yahoo!

Page 2: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Who's this guy?• Principal Front End Engineer, Yahoo!

Front Page• YUI Contributor• Author

Page 3: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Question:What do users see when there's a JavaS cript error on a web page?

Page 4: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Answer:Nothing!*

Page 5: Enterprise JavaScript Error Handling (Ajax Experience 2008)

“If an error is possible, someone will make it. The designer must assume that all possible errors will occur and design so as to minimize the chance of the error in the first place, or its effects once it gets made. E rrors should be easy to detect, they should have minimal consequences, and, if possible, their effects should be reversible.”

-Donald A. NormanThe Design of Everyday Things

Page 6: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Rule #1:Assume your code will fail

Page 7: Enterprise JavaScript Error Handling (Ajax Experience 2008)

My code is beautiful and never fails

Page 8: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Assumption?

• What if destination is null?• What if source is null?

Page 9: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Rule #2:Log errors to the server

Page 10: Enterprise JavaScript Error Handling (Ajax Experience 2008)

S imple Logging

Page 11: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Rule #3:You, not the browser, handle errors

Page 12: Enterprise JavaScript Error Handling (Ajax Experience 2008)

try-catch

• Thrown errors contain extra information• Errors that are caught are considered to

have been handled

Page 13: Enterprise JavaScript Error Handling (Ajax Experience 2008)

window.onerror

• Last stop before browser responds• Return true to indicate not to respond• Only supported in Internet Explorer and

Firefox

Page 14: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Error Lifecycle

try-catch

window.onerror

Browser Error

Error

Page 15: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Rule #4:Identify where errors might occur

Page 16: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Types of Errors• Type coercion errors

Page 17: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Type Coercion Errors

Page 18: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Types of Errors• Type coercion errors• Data type errors

Page 19: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Data Type Errors• Often occurs with function arguments• Typically a symptom of insufficient value

checking

Page 20: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Data Type Errors

Page 21: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Data Type Errors

Page 22: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Data Type Errors - Fixed

Page 23: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Data Type Errors - Fixed

Page 24: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Types of Errors• Type coercion errors• Data type errors• Communication errors

Page 25: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Communication Errors• Invalid URL/post data• S erver response status• No network connection• S erver response content

Page 26: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Invalid URL/Post Data• Typically long string concatenations• Don't forget to use

encodeUR IComponent() on each part– Not encodeUR I()

• Make sure parameters are named correctly

Page 27: Enterprise JavaScript Error Handling (Ajax Experience 2008)

S erver Response S tatus• 200 is not the only valid status that may

be returned– Beware of 304

• Any other status means you didn't get data

Page 28: Enterprise JavaScript Error Handling (Ajax Experience 2008)

S erver Response S tatus

Page 29: Enterprise JavaScript Error Handling (Ajax Experience 2008)

No Network Connection• Internet Explorer throws an error when

calling open() but then goes through normal lifecycle

• Firefox fails s ilently but throws an error if you try to access any response property (status, statusText, responseText)

Page 30: Enterprise JavaScript Error Handling (Ajax Experience 2008)

No Network Connection

Page 31: Enterprise JavaScript Error Handling (Ajax Experience 2008)

S erver Response Content• A status of 200/304 is not enough• S erver errors often return HTML• If possible, set status to 500

Page 32: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Rule #5:Throw your own errors

Page 33: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Throw Your Own Errors

Page 34: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Browser Responds

Page 35: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Throw or Try-Catch?• Errors should be thrown in the low-level

parts of the application– Utilities, core libraries, etc.

• Use try-catch blocks in higher level parts– Application-specific– C lient-side business logic

Page 36: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Rule #6:Distinguish fatal versus non-fatal

Page 37: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Non-Fatal Errors• Won't interfere with user's main tasks• Affects only a portion of the page

– Easily disabled/ignored

• Recovery is possible• A repeat of the action may result in the

appropriate result• Don't tell the user it isn't working unless

absolutely necessary

Page 38: Enterprise JavaScript Error Handling (Ajax Experience 2008)
Page 39: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Fatal Errors

• The application absolutely cannot continue

• S ignificantly interferes with user's ability to be productive

• Other errors will occur if the application continues

• Message the user immediately!• Reload

Page 40: Enterprise JavaScript Error Handling (Ajax Experience 2008)
Page 41: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Fatal or Non-Fatal?• Don't allow your code to determine what

is and is not fatal– Watch out for loops

• The user's experience comes first

Page 42: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Fatal or Non-Fatal?

Page 43: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Rule #7:Provide a debug mode

Page 44: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Debug Mode• Assign a variable that is globally

available• try-catch should re-throw the error• window.onerror should return false• Allow the browser to handle the error

Page 45: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Debug Mode

Page 46: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Debug Mode

Page 47: Enterprise JavaScript Error Handling (Ajax Experience 2008)

S ummary

Page 48: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Rules1.Assume your code will fail2.Log errors to the server3.You, not the browser, handle errors4.Identify where errors might occur5.Throw your own errors6.Distinguish fatal versus non-fatal7.Provide a debug mode

Page 49: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Questions?

Page 50: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Etcetera

• My blog: www.nczonline.net• My email: nzakas@ yahoo-inc.com

Page 51: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Happy debugging!

Page 52: Enterprise JavaScript Error Handling (Ajax Experience 2008)

Creative Commons Images Used

• http://www.flickr.com/photos/crazytales562/2514843252/

• http://flickr.com/photos/waldoj/126354436/• http://flickr.com/photos/markhillary/289294549/• http://flickr.com/photos/3fold/437853495/• http://flickr.com/photos/ianhampton/65178598/• http://flickr.com/photos/joshlewis/1596018210/• http://flickr.com/photos/oberazzi/318947873/• http://flickr.com/photos/mc/27061495/