Repository navigation
Description
What version of Effect is running?
effect 4.1.6, @effect/platform-node 4.1.6 (also 4.1.3, 4.1.4 and 4.1.5), Node 26.13.1
What steps can reproduce the bug?
An HttpApiClient call over NodeHttpClient.layerNodeHttp gets a response at a status the endpoint declares an error for, and the body isn't JSON (a proxy's HTML 404, for example):
import * as http from "node:http"
import { Cause, Effect, Exit, Schema } from "effect"
import { HttpApi, HttpApiClient, HttpApiEndpoint, HttpApiGroup } from "effect/http-api"
import { NodeHttpClient } from "@effect/platform-node"
class NotFound extends Schema.TaggedError()("NotFound", {}, { httpApiStatus: 404 }) {}
const Api = HttpApi.make("api").add(
HttpApiGroup.make("g").add(
HttpApiEndpoint.post("pause", "/pause", { success: Schema.Struct({ ok: Schema.Boolean }), error: NotFound })
)
)
const server = http.createServer((_req, res) => {
res.writeHead(404, { "content-type": "text/html" })
res.end("<html>404</html>")
})
await new Promise((ok) => server.listen(0, "127.0.0.1", ok))
const baseUrl = `http://127.0.0.1:${server.address().port}`
const layer = NodeHttpClient[process.argv[2] ?? "layerNodeHttp"]
const exit = await Effect.runPromiseExit(
Effect.gen(function* () {
const client = yield* HttpApiClient.make(Api, { baseUrl })
return yield* client.g.pause()
}).pipe(Effect.provide(layer))
)
console.log(Exit.isFailure(exit) && Cause.hasDies(exit.cause) ? "DIES" : "fails", Exit.isFailure(exit) ? Cause.pretty(exit.cause).split("\n")[0] : "")
server.close()$ node repro.mjs layerNodeHttp
DIES HttpClientError: Decode error (404 POST http://127.0.0.1:46645/pause)
$ node repro.mjs layerUndici
fails HttpClientError: StatusCode error (404 POST http://127.0.0.1:43119/pause)
What is the expected behavior?
The call fails with an HttpClientError, as it does over layerUndici.
What do you see instead?
Over layerNodeHttp the call dies. The defect is a SyntaxError: Unexpected token '<', "<html>404</html>" is not valid JSON, thrown from NodeHttpIncomingMessage's jsonUnsafe getter (Effect.runSync(this.json)).
Additional information
The path, traced on 4.1.6:
- The status decoder in
http-api/HttpApiClient.ts(decodeMap[status]) catches the body's decode failure and buildsCause.combine(Cause.fail(new HttpClientError({ reason: new StatusCodeError({ request, response }) })), cause). causeCombinecallsdedupeReasons(internal/core.ts), which callsHash.hashon each reason.Fail's[Hash.symbol]hashes itserror, andHash.structurewalks the error's keys down to theNodeHttpResponseinreason.response, then reads every key on it, including thejsonUnsafegetter.- The body isn't JSON, so the getter throws, and the throw surfaces as a defect instead of the typed failure.
The undici transport's response doesn't throw when it is walked, so only the Node transport dies. Any getter on a response that throws (jsonUnsafe, textUnsafe on a body that can't be read, urlParamsBody...) can trigger this. Two possible fixes: give the client responses (or HttpClientError) a reference-based Hash/Equal, or have Hash.structure not invoke accessors. As a workaround, we catch the defect with Effect.catchDefect and fail with it when it is an HttpClientError.
Activity
RubensRafael commentedon Nov 26, 2026
Hi! Was looking into this issue and could reproduce it.
Tried the reference-based option you mentioned. The Redis
Scriptinpersistence/Redis.tsalready does the same thing:Hash.hashchecks for[Hash.symbol]before walking an object, so it stops at the response and never readsjsonUnsafe. A response can only be read once, so comparing it by identity seems to fit. With this the call fails with theHttpClientErrorinstead of dying.We can put it in
NodeHttpIncomingMessage, the base class, so the server request gets it too. There doesn't seem to be a path that triggers this on the server today, but it has the same getters.Additional info:
Also noticed that
jsonUnsafeandtextUnsafegetters only exist on the Node classes, aren't part ofHttpIncomingMessage, and aren't used anywhere in the repo. Would removing them make sense too? The reference-based change would still be needed, so a new getter like these doesn't bring the problem back. But maybe this needs confirmation from a maintainer.I opened a PR here: #10974. Happy to change it if you'd prefer a different approach. (I did not remove the getters)
Drop failHttpDefects, and test that the Node transport fails typed