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
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
730
Design System Support Beyond React
jaketrent
0
660
Getting into Frontend Dev Today
jaketrent
0
620
Encourage Great Combinations
jaketrent
0
720
Getting into React
jaketrent
0
720
Ways to Compose in React
jaketrent
0
71
Anatomy of a Blot Post
jaketrent
0
67
Voice of the Leaders
jaketrent
0
66
Other Decks in Programming
See All in Programming
Range on Rails ―「多重範囲型」という新たな選択肢が、複雑ロジックを劇的にシンプルにしたワケ
rizap_tech
0
6.7k
CSC305 Lecture 05
javiergs
PRO
0
230
オープンソースソフトウェアへの解像度🔬
utam0k
17
3.1k
組込みだけじゃない!TinyGo で始める無料クラウド開発入門
otakakot
1
340
EMこそClaude Codeでコード調査しよう
shibayu36
0
270
bootcamp2025_バックエンド研修_WebAPIサーバ作成.pdf
geniee_inc
0
120
テーブル定義書の構造化抽出して、生成AIでDWH分析を試してみた / devio2025tokyo
kasacchiful
0
230
いま中途半端なSwift 6対応をするより、Default ActorやApproachable Concurrencyを有効にしてからでいいんじゃない?
yimajo
2
450
あなたとKaigi on Rails / Kaigi on Rails + You
shimoju
0
170
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
870
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
600
CSC509 Lecture 06
javiergs
PRO
0
260
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
340
57k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
54k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
Java REST API Framework Comparison - PWX 2021
mraible
34
8.9k
Thoughts on Productivity
jonyablonski
70
4.9k
Docker and Python
trallard
46
3.6k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.2k
Large-scale JavaScript Application Architecture
addyosmani
514
110k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
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