Skip to main content

TypeScript Error Handling Architecture

Design a type-safe TypeScript error-handling architecture with a Result monad, a custom error class hierarchy, HTTP status mapping, and centralized formatting.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Design a comprehensive error handling architecture for a TypeScript Express REST API with Prisma ORM. Include: 1) Result<T, E> monad implementation — replace try/catch with typed results, 2) Custom error class hierarchy: ValidationError, NotFoundError, AuthError, ConflictError, ExternalServiceError, 3) Error codes enum with HTTP status mapping and user-friendly messages, 4) Centralized error handler middleware that formats errors as RFC 7807 Problem Details JSON, 5) Async error boundary — wrap async operations with automatic error capture, 6) Domain-specific error factories with fluent builder pattern, 7) Error serialization for logging (include stack, context, request ID) without exposing internals to clients, 8) Type-safe error narrowing with discriminated unions, 9) Error aggregation for batch operations (collect all errors, do not fail on first), 10) Unit tests verifying error types, messages, and HTTP status mapping. Show before/after comparison: try/catch spaghetti vs Result-based error handling.

What this prompt does

This prompt designs a type-safe error handling architecture for a TypeScript [app_type]. It asks for a Result<T, E> monad to replace try/catch, a custom error hierarchy ([error_classes]), an error-code enum with HTTP status mapping, centralized middleware that formats responses as [error_format], async error boundaries, domain error factories, safe serialization for logging, discriminated-union narrowing, error aggregation for batch operations, and tests verifying types, messages, and status codes.

The structure works because scattered try/catch is where production bugs hide: swallowed errors, leaked internals, inconsistent status codes. By centralizing formatting to [error_format] (RFC 7807 by default) and demanding a before/after comparison, the output makes the shift from ad-hoc handling to typed, predictable error contracts explicit. The serialization rule — full stack and context to logs, nothing internal to clients — is the security-relevant part that the prompt bakes in deliberately.

When to use it

  • When a TypeScript [app_type] has try/catch scattered everywhere and inconsistent errors
  • When API error responses need to follow a standard like [error_format]
  • When you want compile-time guarantees that callers handle error cases
  • When error responses risk leaking stack traces or internal details to clients
  • When batch operations must collect all errors instead of failing on the first
  • When mapping the [error_classes] hierarchy to consistent HTTP status codes
  • When you want compile-time exhaustiveness so adding a new error variant forces every caller to handle it

Example output

Expect a TypeScript design: a Result<T, E> implementation, the [error_classes] hierarchy, an error-code enum with HTTP status and user-friendly message mapping, the centralized middleware emitting [error_format], async error boundaries that wrap operations automatically, domain error factories with a fluent builder, logging serialization that includes stack, context, and request ID without leaking internals, and tests asserting status and type. Discriminated-union narrowing lets the compiler force every branch to be handled, and the batch aggregation collects all errors instead of failing on the first. The prompt also produces a side-by-side before/after showing try/catch spaghetti versus the Result-based approach, which makes the migration concrete rather than theoretical.

Pro tips

  • List your real [error_classes] so the hierarchy and status mapping match your domain rather than generic categories
  • Keep [error_format] as RFC 7807 unless a client contract requires otherwise; standard problem details save integrators effort
  • Verify the serialization rule holds: full stack and request ID to logs, never to the client response body
  • Use the discriminated-union narrowing so the compiler forces every error branch to be handled instead of silently dropped
  • For batch flows, lean on the aggregation feature so one bad record does not abort the whole operation
  • Treat the Result monad as a gradual migration — wrap new code first rather than rewriting every existing try/catch at once

Frequently Asked Questions

What is the benefit of a Result monad over try/catch?
A `Result<T, E>` makes errors part of the return type, so the compiler forces callers to handle the failure case. This catches at build time the swallowed or unhandled errors that try/catch lets slip through to production silently.
Does it prevent leaking internal details to API clients?
Yes. The serialization step sends the full stack, context, and request ID to your logs while returning only safe, user-friendly messages to clients. Confirm this split holds in the output, since leaking stack traces is a real security concern.
What error response format does it produce?
By default it formats errors as RFC 7807 Problem Details JSON via the `[error_format]` variable. This is a widely adopted standard that gives API consumers a predictable error shape; you can override it if a client contract requires a different format.
Can it handle batch operations without stopping at the first error?
It includes error aggregation that collects all errors across a batch rather than failing fast. This is important when processing many records, so one invalid item does not abort the entire operation and hide other failures.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Node.js & TypeScript Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support