Menu

Make TypeScript errors visible: Result types in Lambda handlers
πŸ“°

Make TypeScript errors visible: Result types in Lambda handlers

DEV Community: serverlessΒ·Sascha Mirow-StanitzokΒ·7 days ago
#kAFL1fVf
#devtoserverless#devto#dev#error#kind
Long article (861 words) β€” no AI summary yet.
Reading 0:00
15s threshold

A pattern I see in almost every code review: a Lambda handler with a large catch block, a cascade of instanceof checks and a generic 500 at the end for everything nobody thought of. Then someone adds a new error class, the handler does not know it, and a customer gets a 500 where a 409 would have been correct. The compiler never complained, because in TypeScript a thrown error is invisible in the signature.

I wrote a detailed guide on how I deal with this, TypeScript error handling: Result types, not throw , with a runnable bike rental example, tests and a serverless.yml . Here are the core ideas.

Why throw does not scale in TypeScript

A signature like Promise<Reservation> promises a reservation and says nothing about the ways it can fail. TypeScript has no checked exceptions, and JavaScript lets you throw literally anything. In strict mode the catch variable is therefore unknown , which is honest but does not help much.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.