Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
JavaScript Error Handling
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Jake Trent
March 19, 2014
Programming
0
100
JavaScript Error Handling
Points mostly from a talk originally given at #MWRC by @xjamundx
Jake Trent
March 19, 2014
Tweet
Share
More Decks by Jake Trent
See All by Jake Trent
How We Make The Design System
jaketrent
4
1.4k
Accessibility in the Pluralsight Design System
jaketrent
0
740
Design System Support Beyond React
jaketrent
0
670
Getting into Frontend Dev Today
jaketrent
0
630
Encourage Great Combinations
jaketrent
0
770
Getting into React
jaketrent
0
740
Ways to Compose in React
jaketrent
0
80
Anatomy of a Blot Post
jaketrent
0
74
Voice of the Leaders
jaketrent
0
76
Other Decks in Programming
See All in Programming
AIエージェントの設計で注意するべきポイント6選
har1101
7
3.4k
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
640
humanlayerのブログから学ぶ、良いCLAUDE.mdの書き方
tsukamoto1783
0
170
CSC307 Lecture 02
javiergs
PRO
1
770
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
530
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
5.9k
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
400
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
180
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.4k
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
370
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
210
Data-Centric Kaggle
isax1015
2
750
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
97
6.5k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
A Modern Web Designer's Workflow
chriscoyier
698
190k
What's in a price? How to price your products and services
michaelherold
247
13k
Facilitating Awesome Meetings
lara
57
6.7k
Music & Morning Musume
bryan
47
7.1k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
0
310
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
170
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
190
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
420
So, you think you're a good person
axbom
PRO
2
1.9k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
170
Transcript
JAVASCRIPT ERROR HANDLING FROM A TALK BY @XJAMUNDX AT #MWRC
TRY/CATCH • Use for JSON.parse • Don’t use like a
Java programmer
DON’T THROW • Only as a last resort • Nothing
else you can do
THROW - TJ FONTAINE • throw for programmer errors (eg,
missing required parameter) • https://us- east.manta.joyent.com/ dap/public/drop/er2.htm
ALWAYS USE ERROR OBJECTS • Not simply strings • Errors
contains stack traces • Will provide debug value
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
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)
CALLBACK W/ ERROR • Common pattern in Node • If
action creates error, pass as first parameter in callback
CALLBACK W/ ERROR (2) function doManyThings(done) { doAsync(function (err, data)
{ if (err) return done(err) // … done(null, data) }) }
NAME ANONYMOUS FUNCTIONS • Provides name in stack trace
NAME ANONYMOUS FUNCTIONS (2) doSomethingWithCallback(function veryCallback() { // if I
throw an error, stack trace <3 })
DOMAINS AREN’T COOL • core team is not pushing the
concept • https:// nodefirm.hackpad.com/ Node-Error-Handling- Summit-uXFi4FUg8Td
MAKE ERRORS TO CLIENTS CONSISTENT • Pass all errors through
a common error serializer • Client can also handle consistently
MAKE ERRORS TO CLIENTS CONSISTENT (2) res.json(400, formatError(err))
USE EXPRESS DEFAULT ERROR HANDLER • Good catch-all • Avoid
potential infinite loop bug in Express
USE EXPRESS DEFAULT ERROR HANDLER (2) app.use(function (err, req, res,
next) { res.json(500, formatError(err)) })
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)
ON(‘UNCAUGHTEXCEPTION’) SHOULD ALWAYS EXIT (2) process.on('uncaughtException', function(err) { hurryAndWriteYourWill(err) process.exit()
});
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
https://speakerdeck.com/jaketrent/javascript-error-handling