DEV Community

Cover image for Are You Bored of Try Catch😒

Are You Bored of Try Catch😒

Nurul Islam Rimon on March 01, 2025

Handling Errors in Express with a Custom catchAsync Middleware When building web applications using Express, error handling is one of the most cruc...
Collapse
 
m__mdy__m profile image
genix

The approach in this article is great, but it’s not always the best solution. While wrapping async functions with catchAsync removes repetitive try-catch blocks, it still ties error handling to Express’s middleware flow. This means that if you need more control over error propagation, custom error reactions, or want to handle errors outside the typical HTTP request cycle, this pattern can feel limiting.

In contrast, with Gland you can leverage its event-driven nature to decouple error handling from the core logic. Instead of just using try-catch, you could emit an error event like so:

try {
  // some code
} catch (e) {
  ctx.emit("handler:error", ctx);
}
Enter fullscreen mode Exit fullscreen mode

This gives you the flexibility to manage errors in a more modular way, which can be especially useful in complex systems. Overall, while the catchAsync pattern works well for many Express applications, using an event-based approach like in Gland can offer a more adaptable solution depending on your project’s needs.

Collapse
 
nurulislamrimon profile image
Nurul Islam Rimon • Edited

Thanks, Genix💚
It will be very helpful for me.

Collapse
 
gamelord2011 profile image
Reid Burton

I want the tc39 for the const[error, resp] ?= await(response); format to go through.

Collapse
 
larastewart_engdev profile image
Lara Stewart - DevOps Cloud Engineer

I'm also waiting on this. 🫤

Collapse
 
nurulislamrimon profile image
Nurul Islam Rimon

Thank Reid Burton!

Collapse
 
vitalets profile image
Vitaliy Potapov

The article makes a great point! For a more general approach, there's a try operator proposal for JavaScript that will hopefully reduce the need to write try-catch blocks as often.

Collapse
 
nurulislamrimon profile image
Nurul Islam Rimon

Thank you Vitaliy Potapov❤️

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

article content is unique but mostly dev's used try catch

Collapse
 
nurulislamrimon profile image
Nurul Islam Rimon

Thanks, Syed Muhammad Ali Raza bro! That's right, but I love it and use it the most.

Collapse
 
odys profile image
Odys

Good to use!

Collapse
 
nurulislamrimon profile image
Nurul Islam Rimon

Thank you Odys💚