5.0 KiB
HttpClientError Reason Pattern
Overview
Refactor HttpClientError to follow the reason pattern used by SocketError and AiError. The new design introduces per-reason error classes and a top-level HttpClientError wrapper with a reason field. This provides ergonomic error handling with Effect.catchReason, while preserving rich request/response context.
Goals
- Replace the string
reasonunion with per-reason error classes. - Introduce a top-level
HttpClientErrorwrapper that contains areasonfield. - Use
Data.TaggedErrorfor all new error types. - Preserve existing error messages and request/response metadata.
- Update call sites to construct the new wrapper error consistently.
Non-Goals
- Changing HTTP client behavior beyond error modeling.
- Refactoring
HttpServerErroror other unrelated error types. - Adding retry policy or backoff logic.
- Adding new HTTP status handling rules.
Current State
HttpClientError is a type alias for RequestError | ResponseError, both implemented as Data.TaggedError with a string reason field. Call sites instantiate new RequestError(...) or new ResponseError(...) directly. Many APIs report RequestError or ResponseError as the error type.
Proposed Design
Reason Classes
Introduce per-reason classes using Data.TaggedError. All reason classes include the request, optional description, and optional cause. Response reason classes also include the response.
Reason class fields:
request: HttpClientRequest.HttpClientRequestresponse?: HttpClientResponse.HttpClientResponse(response reasons only)description?: stringcause?: unknown
Request reasons:
TransportError(_tag: "TransportError")EncodeError(_tag: "EncodeError")InvalidUrlError(_tag: "InvalidUrlError")
Response reasons:
StatusCodeError(_tag: "StatusCodeError")DecodeError(_tag: "DecodeError")EmptyBodyError(_tag: "EmptyBodyError")
Each reason class keeps the current message formatting behavior:
- Request reasons keep
"<Reason>: <description> (<METHOD> <URL>)" - Response reasons keep
"<Reason>: <description> (<STATUS> <METHOD> <URL>)"
Reason Union Types
HttpClientErrorReasonunion of all per-reason classes.RequestErrorandResponseErrorbecome type aliases that group request and response reasons respectively.- Export the reason classes from the
HttpClientErrormodule; keepRequestError/ResponseErroras type aliases only.
HttpClientError Wrapper
Add a top-level HttpClientError class using Data.TaggedError:
export class HttpClientError extends Data.TaggedError("HttpClientError")<{
readonly reason: HttpClientErrorReason
}> {
readonly [TypeId] = TypeId
override get message(): string {
return this.reason.message
}
}
Behavior:
messagedelegates toreason.message.isHttpClientErrorguard checks only the wrapper (AiError-style).- If the reason has a
cause, the wrapper constructor copies it onto the wrapper (SocketError-style). - Preserve the existing
TypeIdstring (~effect/http/HttpClientError) for guard compatibility.
API Surface Changes
HttpClientErrorbecomes a class instead of a union type.RequestErrorandResponseErrorare no longer constructible classes.- All APIs that previously exposed
RequestErrororResponseErroras error types now exposeHttpClientError. - Call sites must wrap reasons in
new HttpClientError({ reason }). Effect.catchTag("RequestError")/"ResponseError"flows must move tocatchTag("HttpClientError")and inspecterror.reason._tag.
Usage Examples
Before:
Effect.fail(
new HttpClientError.RequestError({
request,
reason: "InvalidUrl",
description: "Invalid base URL",
cause
})
)
After:
Effect.fail(
new HttpClientError({
reason: new HttpClientError.InvalidUrlError({
request,
description: "Invalid base URL",
cause
})
})
)
Handling:
Effect.catchTag("HttpClientError", (error) => {
if (error.reason._tag === "StatusCodeError") {
return Effect.logError(error.message)
}
return Effect.fail(error)
})
With Effect.catchReason:
Effect.catchReason("HttpClientError", "StatusCodeError", (reason) => Effect.logError(reason.message))
Impacted Areas
packages/effect/src/unstable/http/HttpClientError.ts- HTTP client core modules (
HttpClient.ts,HttpClientResponse.ts,FetchHttpClient.ts,HttpEffect.ts) - Platform implementations (
packages/platform-browser,packages/platform-node,packages/platform-bun) - Downstream usages (OpenAPI generator, AI error conversions, tests)
Test Plan
- Add new tests for
HttpClientErrorreason classes and wrapper behavior. - Update existing tests that reference
RequestErrororResponseError. - Verify error message strings remain unchanged.
Validation
pnpm lint-fixpnpm test <affected_test_file.ts>pnpm check(runpnpm cleanif check fails)pnpm buildpnpm docgen