Skip to content

interceptors.responseError doesn't work with MockAgent #3928

@Gigioliva

Description

@Gigioliva

interceptors.responseError doesn't work with MockAgent

In version 7.0.0, the throwOnError option was removed.
Here is indicated to use the interceptor interceptors.responseError to get the same behavior as before. This solution seems to work when calling a real endpoint. However, this does not work in the test environment using the MockAgent.

Reproducible By

Here a code example (an example extracted from a test case):

import { MockAgent, Pool, interceptors } from "undici";

const URL = "https://www.foo.com";

async function main() {
  const mockAgent = new MockAgent({ connections: 1 });
  mockAgent.disableNetConnect();
  const mockPool = mockAgent.get(URL);
  mockPool.intercept({ method: "GET", path: "/foo" }).reply(400).persist();

  const client = new Pool(URL, { factory: () => mockPool });
  try {
    const response = await client
      .compose(interceptors.responseError({ throwOnError: true }))
      .request({
        method: "GET",
        path: "/foo",
      });

    console.log(response.statusCode);
  } catch (e) {
    console.log(e);
  }
}

if (require.main === module) {
  void main();
}

Expected Behavior

The expected behavior is that an exception is raised, however this does not occur (it prints undefined as statusCode).

Environment

  • node: 20.18
  • undici: 7.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions