Skip to content

HttpApiClient over NodeHttpClient.layerNodeHttp dies when a declared error status has a non-JSON body: Cause.combine hashes the response's throwing jsonUnsafe getter #10966

Done
gjermundgaraba/clankerbox
#102

Description

@gjermundgaraba

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:

  1. The status decoder in http-api/HttpApiClient.ts (decodeMap[status]) catches the body's decode failure and builds Cause.combine(Cause.fail(new HttpClientError({ reason: new StatusCodeError({ request, response }) })), cause).
  2. causeCombine calls dedupeReasons (internal/core.ts), which calls Hash.hash on each reason.
  3. Fail's [Hash.symbol] hashes its error, and Hash.structure walks the error's keys down to the NodeHttpResponse in reason.response, then reads every key on it, including the jsonUnsafe getter.
  4. 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

  1. RubensRafael commented on Nov 26, 2026

    @RubensRafael
    Contributor

    Hi! Was looking into this issue and could reproduce it.

    Tried the reference-based option you mentioned. The Redis Script in persistence/Redis.ts already does the same thing:

    [Hash.symbol](): number {
      return Hash.random(this)
    }
    
    [Equal.symbol](that: unknown): boolean {
      return this === that
    }

    Hash.hash checks for [Hash.symbol] before walking an object, so it stops at the response and never reads jsonUnsafe. A response can only be read once, so comparing it by identity seems to fit. With this the call fails with the HttpClientError instead 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 jsonUnsafe and textUnsafe getters only exist on the Node classes, aren't part of HttpIncomingMessage, 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)

  2. added a commit that references this issue on Nov 28, 2026
    c60943c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions