<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl" -->

---
title: Domain Errors Are Features. System Errors Are Bugs.
description: A practical pattern for classifying errors into two buckets: domain errors (expected, like bad input) and system errors (unexpected, like a dropped DB...
canonical: https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Domain Errors Are Features. System Errors Are Bugs. | daily.dev
og:description: A practical pattern for classifying errors into two buckets: domain errors (expected, like bad input) and system errors (unexpected, like a dropped DB...
og:url: https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl
og:image: https://api.daily.dev/og/posts/d6OCVQ4vL.png
og:image:alt: Domain Errors Are Features. System Errors Are Bugs.
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Domain Errors Are Features. System Errors Are Bugs.

**[The T-Shaped Dev](https://daily.dev/sources/thetshaped-dev)** · 7 min read · 21 upvotes · 6 comments

## Summary

A practical pattern for classifying errors into two buckets: domain errors (expected, like bad input) and system errors (unexpected, like a dropped DB connection). Encoding this distinction directly into the type system via abstract DomainError and SystemError classes lets a single instanceof check drive HTTP status codes, user-facing messages, log levels, and on-call paging decisions automatically. Includes TypeScript code for the base error class, concrete error subclasses, an Express error-handling middleware, and guidance on wrapping raw driver errors at the infrastructure edge so they never leak to clients.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://thetshaped.dev/p/domain-errors-are-features-system-errors-are-bugs-error-handling>

## Questions this post answers

### How do I decide whether to return a 400 or a 500 status code for an error in my API?

Base the decision on whether the error was expected, not on what it's called. Domain errors—bad input, a forbidden action, insufficient funds—are expected and map to 4xx codes with a real user-facing message. System errors—a dropped DB connection, an upstream timeout—are unexpected, map to 5xx, and get a generic message since the user can't fix them.

_For teams standardizing error handling conventions across services, daily.dev surfaces patterns like this domain-versus-system split._

### How can I structure TypeScript error classes so my error handling middleware doesn't need switch statements or string matching?

Create an abstract AppError base class holding a status and a stable code, then two abstract subclasses, DomainError and SystemError. Concrete errors like ValidationError or DatabaseError extend one of these and declare their own status and code. A single instanceof check against DomainError in the middleware then routes the response, log level, and paging decision automatically.

_Developers refining their TypeScript error architecture can find similar type-driven patterns on daily.dev._

### Should a 'not found' error always be treated as a domain error?

No, it depends on why the record is missing. A user requesting a resource by an ID that legitimately doesn't exist, like /users/123, is a domain error and should return a 404. But an application failing to load an ID it just wrote itself moments earlier is a system error, because that failure should be impossible and signals something actually broke.

_Debugging inconsistent 404 handling gets easier when developers compare error-handling conventions on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 0 upvotes

> How do you preserve this classification when an error crosses a worker boundary or gets serialized onto a queue?

**@trevorsuna** · 0 upvotes

> Separating expected business outcomes from operational failures is a small design choice with a big observability payoff. The edge-wrapping rule matters most: if raw driver errors escape, clients get unstable contracts and on-call alerts lose the context needed to fix the real dependency.

## Similar posts on daily.dev

- [The two kinds of error](https://daily.dev/posts/the-two-kinds-of-error-zsfcltzzr) · Lobsters · 0 upvotes · 0 comments
- [The Second Great Error Model Convergence](https://daily.dev/posts/the-second-great-error-model-convergence-e6ligcvol) · matklad · 0 upvotes · 0 comments
- [Error Handling and Hiding Errors is Not the Same Thing](https://daily.dev/posts/error-handling-and-hiding-errors-is-not-the-same-thing-wuqe6lugx) · Atomic Spin · 1 upvotes · 0 comments
- [What Now? Handling Errors in Large Systems](https://daily.dev/posts/what-now-handling-errors-in-large-systems-qq5qke6ya) · Marc Brooker · 2 upvotes · 0 comments

---

Tags: [#architecture](https://daily.dev/tags/architecture), [#nodejs](https://daily.dev/tags/nodejs), [#typescript](https://daily.dev/tags/typescript), [#observability](https://daily.dev/tags/observability)

[View this post on daily.dev](https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Domain Errors Are Features. System Errors Are Bugs.","url":"https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl"},"datePublished":"2026-08-29T04:23:06.284Z","dateModified":"2026-09-13T21:48:38.696Z","description":"A practical pattern for classifying errors into two buckets: domain errors (expected, like bad input) and system errors (unexpected, like a dropped DB...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/fc31e8a96c4671e150e7e999a05aa6a2?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/fc31e8a96c4671e150e7e999a05aa6a2?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"The T-Shaped Dev","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"The T-Shaped Dev","logo":"https://media.daily.dev/image/upload/s--TaOz2Ogr--/f_auto,q_auto/v1774964066/logos/thetshaped-dev?_a=BAMAMiWQ0","url":"https://daily.dev/sources/thetshaped-dev"},"commentCount":6,"discussionUrl":"https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":21},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":6}],"keywords":"architecture,nodejs,typescript,observability","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"The T-Shaped Dev","item":"https://daily.dev/sources/thetshaped-dev"},{"@type":"ListItem","position":3,"name":"Domain Errors Are Features. System Errors Are Bugs."}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl","comment":[{"@type":"Comment","text":"How do you preserve this classification when an error crosses a worker boundary or gets serialized onto a queue?","datePublished":"2026-09-03T18:36:27.068Z","url":"https://daily.dev/posts/d6OCVQ4vL#c-C8IEWzL4d","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"}},{"@type":"Comment","text":"Separating expected business outcomes from operational failures is a small design choice with a big observability payoff. The edge-wrapping rule matters most: if raw driver errors escape, clients get unstable contracts and on-call alerts lose the context needed to fix the real dependency.","datePublished":"2026-09-01T02:41:56.539Z","url":"https://daily.dev/posts/d6OCVQ4vL#c-mHlKPe3Bq","author":{"@type":"Person","name":"Trevor Suna","url":"https://daily.dev/trevorsuna","image":"https://media.daily.dev/image/upload/s--dZ7gXxpp--/f_auto/v1784081551/avatars/avatar_EMoP47rpuw8DNjhp6R1b6?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/domain-errors-are-features-system-errors-are-bugs--d6ocvq4vl#faq","mainEntity":[{"@type":"Question","name":"How do I decide whether to return a 400 or a 500 status code for an error in my API?","acceptedAnswer":{"@type":"Answer","text":"Base the decision on whether the error was expected, not on what it's called. Domain errors—bad input, a forbidden action, insufficient funds—are expected and map to 4xx codes with a real user-facing message. System errors—a dropped DB connection, an upstream timeout—are unexpected, map to 5xx, and get a generic message since the user can't fix them. For teams standardizing error handling conventions across services, daily.dev surfaces patterns like this domain-versus-system split."}},{"@type":"Question","name":"How can I structure TypeScript error classes so my error handling middleware doesn't need switch statements or string matching?","acceptedAnswer":{"@type":"Answer","text":"Create an abstract AppError base class holding a status and a stable code, then two abstract subclasses, DomainError and SystemError. Concrete errors like ValidationError or DatabaseError extend one of these and declare their own status and code. A single instanceof check against DomainError in the middleware then routes the response, log level, and paging decision automatically. Developers refining their TypeScript error architecture can find similar type-driven patterns on daily.dev."}},{"@type":"Question","name":"Should a 'not found' error always be treated as a domain error?","acceptedAnswer":{"@type":"Answer","text":"No, it depends on why the record is missing. A user requesting a resource by an ID that legitimately doesn't exist, like /users/123, is a domain error and should return a 404. But an application failing to load an ID it just wrote itself moments earlier is a system error, because that failure should be impossible and signals something actually broke. Debugging inconsistent 404 handling gets easier when developers compare error-handling conventions on daily.dev."}}]}
```

