Functional error handling with Express.js and DDD | Enterprise Node.js + TypeScript

Click for: original source

How to expressively represent (database, validation and unexpected) errors as domain concepts using functional programming concepts and how to hook those errors up to our Express.js base controller. By Khalil Stemmler, a developer advocate at Apollo GraphQL.

In most programming projects, there’s confusion as to how and where errors should be handled. Do I throw an error and let the client figure out how to handle it? Do I return null?

When we throw errors, we disrupt the flow of the program and make it trickier for someone to walk through the code, since exceptions share similarities with the sometimes criticized GOTO command.

And when we return null, we’re breaking the design principle that “a method should return a single type”. Not adhering to this can lead to misuse of our methods from clients.

The article the deals with:

  • Why expressing errors explicitly is important to domain modeling
  • How to expressively represent errors using types
  • How to and why to organize all errors by Use Cases
  • How to elegantly connect errors to Express.js API responses

This is long article with detailed explanation of the code and design and you also get access to GitHub repository. Great read!

[Read More]

Tags nodejs javascript frontend web-development