SlideShare a Scribd company logo
1 of 29
Download to read offline
E R RO R H A N D L I N G W / J S
F O N G
Return NULL
Throw Errors
Monad Either
Return NULL
Throw Errors
Monad Either
Vague Signature
Not Expressive
Caller Has No Clue
R E T U R N N U L L
• Requires caller to be aware of NULL
• Silent for caller
• Not expressive at all
• Vague Function Signature
Throw Errors
Monad Either
Return NULL
Deceive Signature
Break the flow
Easy to break
T H RO W E R RO R S
• Caller must handle it by try-catch
• Break the flow of caller
• Less expressive
• Deceive Function Signature
• Try-catch usually for interaction with external systems
G O A L S
• Honest and expressive signature
• Support a number of error types for caller
• Solid way to identify error types
• Don’t break the flow
G O L A N G WAY
• Encapsulate the result to an array
• Return the array
Reference: https://dev.to/oieduardorabelo/javascript-handling-errors-like-go-3efk
S T I L L A L I T T L E B I T
H A S S L E …
Throw Errors
Monad Either
Return NULL
F U N C T I O N A L P R O G R A M M I N G
• Functions are first-class citizens
• Expression > Statement
• Pure Function with no side effects
M O N A D
• A monad is a way of composing functions that require
context in addition to the return value, such as
computation, branching, or I/O.
• It is based on a simple symmetry — A way to wrap a
value into a context, and a way to unwrap the value
from the context
🏣
" #
M O N A D
D ATAD ATA
Expressive
Flow & Solid
Chainable
4 F E AT U R E S
• Map: apply functions to itself
• Context:package
• Type Lift:wrap
• Flatten:unwrap





→ Examples: Array, Promise,…
I N T R O D U C I N G E I T H E R
Always Left Always Right
M O R E P O W E R F U L I N
T RY P E S C R I P T…
source: https://gist.github.com/brunovegreville/1289eba4455de2413724c04d8eab3c78#file-either-ts
Various Error Types
Expressive Response
R E S O U R C E S
• JavaScript Monads Made Simple
• Type-Safe Error Handling In TypeScript
• Functional Error Handling with Express.js and DDD |
Enterprise Node.js + TypeScript
• Expressive error handling in TypeScript and benefits for
domain-driven design (medium star alert)
• Chapter 9: Monadic Onions


More Related Content

Similar to Error Handling In JS

Reduce, Reuse, Refactor
Reduce, Reuse, RefactorReduce, Reuse, Refactor
Reduce, Reuse, Refactorcklosowski
 
ATDD BY Example With Cucumber
ATDD BY Example With CucumberATDD BY Example With Cucumber
ATDD BY Example With CucumberDoug Morgan
 
Domain storytelling – facilitator’s guide to enhance learning in your organiz...
Domain storytelling – facilitator’s guide to enhance learning in your organiz...Domain storytelling – facilitator’s guide to enhance learning in your organiz...
Domain storytelling – facilitator’s guide to enhance learning in your organiz...Mufrid Krilic
 
The 12 Factor App
The 12 Factor AppThe 12 Factor App
The 12 Factor Apprudiyardley
 

Similar to Error Handling In JS (6)

Reduce, Reuse, Refactor
Reduce, Reuse, RefactorReduce, Reuse, Refactor
Reduce, Reuse, Refactor
 
ATDD BY Example With Cucumber
ATDD BY Example With CucumberATDD BY Example With Cucumber
ATDD BY Example With Cucumber
 
Domain storytelling – facilitator’s guide to enhance learning in your organiz...
Domain storytelling – facilitator’s guide to enhance learning in your organiz...Domain storytelling – facilitator’s guide to enhance learning in your organiz...
Domain storytelling – facilitator’s guide to enhance learning in your organiz...
 
Cpu
CpuCpu
Cpu
 
The 12 Factor App
The 12 Factor AppThe 12 Factor App
The 12 Factor App
 
Smoke tests and mirrors
Smoke tests and mirrorsSmoke tests and mirrors
Smoke tests and mirrors
 

More from Fong Liou

拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)Fong Liou
 
黑豹 ch4 ddd pattern practice (2)
黑豹 ch4 ddd pattern practice (2)黑豹 ch4 ddd pattern practice (2)
黑豹 ch4 ddd pattern practice (2)Fong Liou
 
黑豹 ch4 ddd pattern pracrice
黑豹 ch4 ddd pattern pracrice黑豹 ch4 ddd pattern pracrice
黑豹 ch4 ddd pattern pracriceFong Liou
 
2021 DDDTW Study Group 第一場 練習題
2021 DDDTW Study Group 第一場 練習題2021 DDDTW Study Group 第一場 練習題
2021 DDDTW Study Group 第一場 練習題Fong Liou
 
Lerna 的套件管理術 - 2020 JSDC Taiwan
Lerna 的套件管理術 - 2020 JSDC TaiwanLerna 的套件管理術 - 2020 JSDC Taiwan
Lerna 的套件管理術 - 2020 JSDC TaiwanFong Liou
 
Example mapping
Example mappingExample mapping
Example mappingFong Liou
 
Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)Fong Liou
 
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
2019-03-13-ddd taiwan-community-iddd-studygroup-2ndFong Liou
 

More from Fong Liou (8)

拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
 
黑豹 ch4 ddd pattern practice (2)
黑豹 ch4 ddd pattern practice (2)黑豹 ch4 ddd pattern practice (2)
黑豹 ch4 ddd pattern practice (2)
 
黑豹 ch4 ddd pattern pracrice
黑豹 ch4 ddd pattern pracrice黑豹 ch4 ddd pattern pracrice
黑豹 ch4 ddd pattern pracrice
 
2021 DDDTW Study Group 第一場 練習題
2021 DDDTW Study Group 第一場 練習題2021 DDDTW Study Group 第一場 練習題
2021 DDDTW Study Group 第一場 練習題
 
Lerna 的套件管理術 - 2020 JSDC Taiwan
Lerna 的套件管理術 - 2020 JSDC TaiwanLerna 的套件管理術 - 2020 JSDC Taiwan
Lerna 的套件管理術 - 2020 JSDC Taiwan
 
Example mapping
Example mappingExample mapping
Example mapping
 
Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)
 
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
 

Recently uploaded

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 

Recently uploaded (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 

Error Handling In JS

  • 1. E R RO R H A N D L I N G W / J S F O N G
  • 4.
  • 6. R E T U R N N U L L • Requires caller to be aware of NULL • Silent for caller • Not expressive at all • Vague Function Signature
  • 8.
  • 9. Deceive Signature Break the flow Easy to break
  • 10. T H RO W E R RO R S • Caller must handle it by try-catch • Break the flow of caller • Less expressive • Deceive Function Signature • Try-catch usually for interaction with external systems
  • 11. G O A L S • Honest and expressive signature • Support a number of error types for caller • Solid way to identify error types • Don’t break the flow
  • 12. G O L A N G WAY • Encapsulate the result to an array • Return the array Reference: https://dev.to/oieduardorabelo/javascript-handling-errors-like-go-3efk
  • 13.
  • 14. S T I L L A L I T T L E B I T H A S S L E …
  • 16. F U N C T I O N A L P R O G R A M M I N G • Functions are first-class citizens • Expression > Statement • Pure Function with no side effects
  • 17. M O N A D • A monad is a way of composing functions that require context in addition to the return value, such as computation, branching, or I/O. • It is based on a simple symmetry — A way to wrap a value into a context, and a way to unwrap the value from the context
  • 18. 🏣 " # M O N A D D ATAD ATA
  • 20. 4 F E AT U R E S • Map: apply functions to itself • Context:package • Type Lift:wrap • Flatten:unwrap
 
 
 → Examples: Array, Promise,…
  • 21.
  • 22. I N T R O D U C I N G E I T H E R
  • 23.
  • 25.
  • 26. M O R E P O W E R F U L I N T RY P E S C R I P T…
  • 29. R E S O U R C E S • JavaScript Monads Made Simple • Type-Safe Error Handling In TypeScript • Functional Error Handling with Express.js and DDD | Enterprise Node.js + TypeScript • Expressive error handling in TypeScript and benefits for domain-driven design (medium star alert) • Chapter 9: Monadic Onions