Dmitry Pavlov, Remote ASP.NET, C#, .NET Developer at Self Employed, FreelanceNice presentation. Thanks! One note regarding the JS code - is to use so much try-catch blocks, if (debugmode) checks, etc. the code will become unreadable. What could help to keep JS code clear with the error handling practices you mentioned?1 year ago
E nterpris e JavaS cript E rror
Handling
Nicholas C . Zakas
P rincipal Front E nd E ngineer, Yahoo!
Who's this g uy?
• P rincipal Front E nd E ngineer, Yahoo!
Front P age
• YUI C ontributor
• Author
Ques tion:
What do us ers s ee when
there's a JavaS cript error on
a web pag e?
Ans wer:
Nothing !*
“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.”
-D onald A. Norman
The D esign of E veryday Things
R ule #1:
As s ume your code will fail
My code is beautiful
and never fails
As s umption?
• What if destination is null?
• What if source is null?
R ule #2:
Log errors to the s erver
S imple Log g ing
R ule #3:
You, not the brows er, handle
errors
try-catch
• Thrown errors contain extra information
• E rrors that are caught are considered to
have been handled
window.onerror
• Last stop before browser responds
• R eturn true to indicate not to respond
• O nly supported in Internet E xplorer and
Firefox
E rror Lifecycle
Browser Error
window.onerror
try-catch
Error
R ule #4:
Identify where errors mig ht
occur
Types of E rrors
• Type coercion errors
Type C oercion E rrors
Types of E rrors
• Type coercion errors
• D ata type errors
Data Type E rrors
• O ften occurs with function arguments
• Typically a symptom of insufficient value
checking
Data Type E rrors
Data Type E rrors
Data Type E rrors - Fixed
Data Type E rrors - Fixed
Types of E rrors
• Type coercion errors
• D ata type errors
• C ommunication errors
C ommunication E rrors
• Invalid UR L/post data
• S erver response status
• No network connection
• S erver response content
Invalid UR L/Pos t Data
• Typically long string concatenations
• D on't forget to use
encodeUR IC omponent() on each part
– Not encodeUR I()
• M ake sure parameters are named
correctly
S erver R es pons e 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
S erver R es pons e S tatus
No Network C onnection
• Internet E xplorer throws an error when
calling open() but then goes through
normal lifecycle
• Firefox fails silently but throws an error if
you try to access any response property
(status, statusText, responseText)
No Network C onnection
S erver R es pons e C ontent
• A status of 200/ 304 is not enough
• S erver errors often return HTM L
• If possible, set status to 500
R ule #5:
Throw your own errors
Throw Your Own E rrors
B rows er R es ponds
Throw or Try-C atch?
• E rrors 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
R ule #6:
Dis ting uis h fatal vers us non-
fatal
Non-Fatal E rrors
• Won't interfere with user's main tasks
• Affects only a portion of the page
– E asily disabled/ignored
• R ecovery is possible
• A repeat of the action may result in the
appropriate result
• D on't tell the user it isn't working unless
absolutely necessary
Fatal E rrors
• The application absolutely cannot
continue
• S ignificantly interferes with user's ability
to be productive
• O ther errors will occur if the application
continues
• M essage the user immediately!
• R eload
Fatal or Non-Fatal?
• D on't allow your code to determine what
is and is not fatal
– Watch out for loops
• The user's experience comes first
Fatal or Non-Fatal?
R ule #7:
Provide a debug mode
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
Debug Mode
Debug Mode
S ummary
R ules
1.Assume your code will fail
2.Log errors to the server
3.You, not the browser, handle errors
4.Identify where errors might occur
5.Throw your own errors
6.D istinguish fatal versus non-fatal
7.P rovide a debug mode
Ques tions ?
E tcetera
• M y blog: www.nczonline.net
• M y email: nzakas@ yahoo-inc.com
Backspacetab 1 year ago