4.6 KiB
HttpServerError Reason Pattern
Overview
Refactor HttpServerError to use the reason pattern (see HttpClientError and
SocketError). HttpServerError becomes a thin wrapper around a reason union
of semantic reason classes. The existing reason tags and response mappings stay
the same while enabling Effect.catchReason flows.
Goals
- Replace the string
reasonunion with per-reason classes. - Introduce a top-level
HttpServerErrorwrapper containing areasonfield. - Preserve existing reason tags (
RequestParseError,RouteNotFound,InternalError,ResponseError). - Keep the current message formatting and response status mapping.
- Update call sites to construct the wrapper with reason instances.
- Keep
isHttpServerErroraligned with the wrapper only. - Keep
ServeErrorbehavior separate fromHttpServerError.
Non-Goals
- Changing response status behavior or routing semantics.
- Adding new error categories or altering error messages.
- Refactoring unrelated HTTP server modules.
Current State
HttpServerError is a type alias of RequestError | ResponseError, implemented
as Data.TaggedError classes that store a literal reason field. Call sites
instantiate new RequestError({ reason: "RouteNotFound", ... }) or
new ResponseError({ ... }) directly, and Respondable handling is implemented
on these classes.
Proposed Design
Reason Classes
Introduce reason classes using Data.TaggedError with the existing tags:
RequestParseErrorRouteNotFoundInternalErrorResponseError
Shared fields (all reasons):
request: HttpServerRequest.HttpServerRequestdescription?: stringcause?: unknown
Response-specific fields:
response: HttpServerResponse.HttpServerResponse
Each reason implements Respondable.Respondable and defines
[Respondable.TypeId] with the same status mapping as today:
RequestParseError-> 400RouteNotFound-> 404InternalError-> 500ResponseError-> 500
Message Formatting
Reason classes preserve the existing message strings:
- Request reasons:
<Reason> (<METHOD> <URL>), with: <description>when set. ResponseError:<Reason> (<STATUS> <METHOD> <URL>), with: <description>when set.
Reason Union Types
RequestErrorbecomes a type alias for request reasons.HttpServerErrorReasonis the union of all reason classes.
HttpServerError Wrapper
Add a HttpServerError wrapper class:
export class HttpServerError extends Data.TaggedError("HttpServerError")<{
readonly reason: HttpServerErrorReason
}> {
readonly [TypeId] = TypeId
override get message(): string {
return this.reason.message
}
}
Behavior:
- Copies
reason.causeonto the wrapper when present (SocketError-style). - Implements
Respondable.Respondable, delegating[Respondable.TypeId]to the reason. - Optional convenience getters for
requestandresponsedelegate to the reason. isHttpServerErrorguards only the wrapper.- Preserve the existing
stackformat (${this.name}: ${this.message}) on the wrapper.
ServeError Guarding
ServeError continues to represent server startup failures, but
isHttpServerError must not match it. Implement this by tightening the guard to
check the wrapper _tag or by moving ServeError onto its own type id.
Construction Pattern
Effect.fail(
new HttpServerError({
reason: new RouteNotFound({ request })
})
)
Handling:
Effect.catchReason("HttpServerError", "RouteNotFound", (reason) => Effect.logWarning(reason.message))
Impacted Areas
packages/effect/src/unstable/http/HttpServerError.tspackages/effect/src/unstable/http/HttpRouter.tspackages/effect/src/unstable/http/HttpEffect.tspackages/effect/src/unstable/http/HttpServerRequest.tspackages/effect/src/unstable/http/HttpMiddleware.tspackages/platform-node/src/NodeHttpServer.tspackages/platform-node/src/NodeClusterHttp.tspackages/platform-bun/src/BunHttpServer.tspackages/platform-bun/src/BunClusterHttp.tspackages/effect/src/unstable/eventlog/EventLogServer.ts
Test Plan
- Add tests for reason message formatting and status mapping.
- Verify wrapper delegates
message,request, andresponse. - Verify
isHttpServerErroronly matches the wrapper. - Verify
ServeErroris excluded fromisHttpServerError. - Validate
Respondabledelegation (wrapper -> reason).
Follow the it.effect pattern and existing Effect test conventions.
Validation
pnpm lint-fixpnpm test <affected_test_file.ts>pnpm check(runpnpm cleanif check fails)pnpm buildpnpm docgen