SlideShare a Scribd company logo
2
Most read
4
Most read
6
Most read
Node.js 
Exception Handling 
Do it the right way 
Minh Hoang - http://minhhoang.de
Node.js 
• is a still a relatively new programming language 
• Node.js crashes for any ‘uncatch error’ 
• Most developers are confused when to use 
• try/catch 
• pass error to a callback 
• throw an error
error vs. exception 
• an error = an instance of the Error class 
• can be created 
• can be thrown 
• can passed to callback 
• a thrown error = an Exception
errors 
Operational Errors 
(run-time issue, not bug) 
Programmer Errors 
(bug) 
• out-of-memory 
• connection issues 
• timeout 
• server return 500 HTTP code 
• socket hang-up 
• no ‘undefined’ check 
• no callback for asynchronous 
function 
• object type error 
a correct code must be able to deal with operation error
Best Practices for dealing 
with operation errors 
• Always put asynchronous code into try/catch (otherwise Node.js 
crashes if uncaught error happens) 
• Synchronous code: return error. 
• ENONT error why trying to open a file: create file the first before read it 
• socket hang-up: write code to reconnect if needed 
• ready to deal with invalid JSON - using try/catch 
• retry with 5xx HTTP error 
• no idea how to deal with unthinkable/-solvable issue - such as 
ENOMEM: log the error and crash (intentionally)
Dealing with programmer 
errors 
• crash immediately 
• how: throw new Error(‘ehh’); 
…. if you have a restarter in place (like Bluemix) 
…. or just use: forever.js
• instead of ‘callback(result)’ use ‘callback(err, 
result)’ 
• because: either ‘result’ or ‘err’ is NULL!
Error class 
• has {name, message, stack} 
• if you create an Error instance manually, at least 
provide {name, message}
last chance… 
• use process.on(‘uncaughtException’, function() 
{<do_sth>}); —> Global Catch 
• it’s NOT recommended…but what if your app’s 
just crashed without leaving a trace?
Automated Test 
• Crucially important 
• start with unit-test for all modules 
• then integration test 
• no test, no CI, you are in the coding hell!
and … think about this
Function 
- write it the right way - 
• first argument ist error 
• last ist callback 
• use return (return callback(null, data); or 
return(err)
Sources 
• Error Handling in Node.js https:// 
www.joyent.com/developers/node/design/errors 
• Node.js Best Practices: http:// 
www.slideshare.net/the_undefined/nodejs-best-practices- 
10428790 
• Continuous Development: http:// 
blog.risingstack.com/continuous-deployment-of-node- 
js-applications/
“Either I will find a way, 
or I will make one” 
–Philip Sidney

More Related Content

PDF
Oracle Apps INVENTORY
PPTX
Drop Ship Sales Order Across Operating Units
DOCX
How to demonstrate requisition to sourcing rfq to purchase order
PPTX
Flag Registers (Assembly Language)
PPTX
Causes of Maggie's Tragedy in 'Maggie: A Girl Of Streets'
PDF
Enterprise JavaScript Error Handling (Ajax Experience 2008)
PPTX
PPTX
Macteam may2015 pt v7
Oracle Apps INVENTORY
Drop Ship Sales Order Across Operating Units
How to demonstrate requisition to sourcing rfq to purchase order
Flag Registers (Assembly Language)
Causes of Maggie's Tragedy in 'Maggie: A Girl Of Streets'
Enterprise JavaScript Error Handling (Ajax Experience 2008)
Macteam may2015 pt v7

Viewers also liked (20)

DOCX
Kefolonia Website Mock
PPTX
Media A2 Evaluation Question 2
PDF
Making Mad Men more Agile | Agile on the beach | Sept 2015
DOCX
Case de mecânica
PDF
Circolare n 3 fondo di garanzia
PPT
Veneto turismo
PPTX
Powerpoint 1
PDF
創業管理:兼職創業者完整課程 Week 3
PDF
SEB wireframes
PDF
Презентация проекта Дождь Lite
PPTX
Perencanaan parkir jts
PPTX
Catering Eventos
PPSX
Wishclubteamusa_russia
PDF
New Orleans Saints and Academy Sports + Outdoors announce renewal of long-ter...
PPTX
I lama i_domyhomework_presentation
PPT
Croazia turismo
PPT
Francia PACA
PDF
Element wizard mockup
PPT
Turchia istanbul
PPTX
Part 2 continued
Kefolonia Website Mock
Media A2 Evaluation Question 2
Making Mad Men more Agile | Agile on the beach | Sept 2015
Case de mecânica
Circolare n 3 fondo di garanzia
Veneto turismo
Powerpoint 1
創業管理:兼職創業者完整課程 Week 3
SEB wireframes
Презентация проекта Дождь Lite
Perencanaan parkir jts
Catering Eventos
Wishclubteamusa_russia
New Orleans Saints and Academy Sports + Outdoors announce renewal of long-ter...
I lama i_domyhomework_presentation
Croazia turismo
Francia PACA
Element wizard mockup
Turchia istanbul
Part 2 continued
Ad

Similar to Node.js exception handling (20)

PPT
Chelberg ptcuser 2010
PDF
TDD super mondays-june-2014
PPTX
Task parallel library presentation
PPT
PHP - Introduction to PHP Bugs - Debugging
PDF
Error handling in XPages
PPTX
Building unit tests correctly
PDF
Owasp tds
PDF
intro-to-eclipse.pdf
PPT
exceptionvdffhhhccvvvv-handling-in-java.ppt
PDF
Exception Handling: Designing Robust Software in Ruby
PDF
Presentations Unusual Java Bugs And Detecting Them Using Foss Tools
PPTX
Python_Exception_Handling_Presentation.pptx
PDF
Speed geeking-lotusscript
PDF
Php Debugging from the Trenches
KEY
Cross-platform logging and analytics
PDF
Bug Hunting Safari
ODP
Automated Testing in Django
PDF
The Many Ways to Test Your React App
ODP
Effective TDD - Less is more
PPTX
Chelberg ptcuser 2010
TDD super mondays-june-2014
Task parallel library presentation
PHP - Introduction to PHP Bugs - Debugging
Error handling in XPages
Building unit tests correctly
Owasp tds
intro-to-eclipse.pdf
exceptionvdffhhhccvvvv-handling-in-java.ppt
Exception Handling: Designing Robust Software in Ruby
Presentations Unusual Java Bugs And Detecting Them Using Foss Tools
Python_Exception_Handling_Presentation.pptx
Speed geeking-lotusscript
Php Debugging from the Trenches
Cross-platform logging and analytics
Bug Hunting Safari
Automated Testing in Django
The Many Ways to Test Your React App
Effective TDD - Less is more
Ad

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
AutoCAD Professional Crack 2025 With License Key
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
assetexplorer- product-overview - presentation
Operating system designcfffgfgggggggvggggggggg
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Digital Systems & Binary Numbers (comprehensive )
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Advanced SystemCare Ultimate Crack + Portable (2025)
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
AutoCAD Professional Crack 2025 With License Key
Monitoring Stack: Grafana, Loki & Promtail
Computer Software and OS of computer science of grade 11.pptx
Designing Intelligence for the Shop Floor.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Autodesk AutoCAD Crack Free Download 2025
Why Generative AI is the Future of Content, Code & Creativity?
CHAPTER 2 - PM Management and IT Context
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Adobe Illustrator 28.6 Crack My Vision of Vector Design
assetexplorer- product-overview - presentation

Node.js exception handling

  • 1. Node.js Exception Handling Do it the right way Minh Hoang - http://minhhoang.de
  • 2. Node.js • is a still a relatively new programming language • Node.js crashes for any ‘uncatch error’ • Most developers are confused when to use • try/catch • pass error to a callback • throw an error
  • 3. error vs. exception • an error = an instance of the Error class • can be created • can be thrown • can passed to callback • a thrown error = an Exception
  • 4. errors Operational Errors (run-time issue, not bug) Programmer Errors (bug) • out-of-memory • connection issues • timeout • server return 500 HTTP code • socket hang-up • no ‘undefined’ check • no callback for asynchronous function • object type error a correct code must be able to deal with operation error
  • 5. Best Practices for dealing with operation errors • Always put asynchronous code into try/catch (otherwise Node.js crashes if uncaught error happens) • Synchronous code: return error. • ENONT error why trying to open a file: create file the first before read it • socket hang-up: write code to reconnect if needed • ready to deal with invalid JSON - using try/catch • retry with 5xx HTTP error • no idea how to deal with unthinkable/-solvable issue - such as ENOMEM: log the error and crash (intentionally)
  • 6. Dealing with programmer errors • crash immediately • how: throw new Error(‘ehh’); …. if you have a restarter in place (like Bluemix) …. or just use: forever.js
  • 7. • instead of ‘callback(result)’ use ‘callback(err, result)’ • because: either ‘result’ or ‘err’ is NULL!
  • 8. Error class • has {name, message, stack} • if you create an Error instance manually, at least provide {name, message}
  • 9. last chance… • use process.on(‘uncaughtException’, function() {<do_sth>}); —> Global Catch • it’s NOT recommended…but what if your app’s just crashed without leaving a trace?
  • 10. Automated Test • Crucially important • start with unit-test for all modules • then integration test • no test, no CI, you are in the coding hell!
  • 11. and … think about this
  • 12. Function - write it the right way - • first argument ist error • last ist callback • use return (return callback(null, data); or return(err)
  • 13. Sources • Error Handling in Node.js https:// www.joyent.com/developers/node/design/errors • Node.js Best Practices: http:// www.slideshare.net/the_undefined/nodejs-best-practices- 10428790 • Continuous Development: http:// blog.risingstack.com/continuous-deployment-of-node- js-applications/
  • 14. “Either I will find a way, or I will make one” –Philip Sidney