Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorHandling with responseError interceptor when using MockAgent #4026

Closed
blephy opened this issue Jan 23, 2025 · 4 comments
Closed

ErrorHandling with responseError interceptor when using MockAgent #4026

blephy opened this issue Jan 23, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@blephy
Copy link

blephy commented Jan 23, 2025

Bug Description

When using responseError interceptor, the error thrown is a ResponseError which always include an empty property body: '' and an empty property headers: {}. This always happens when using MockAgent.

Reproducible By

https://github.com/blephy/undici-issue-mock

Expected Behavior

Body, headers, and statusCode should be populated in the error thrown and this error should be an instance of ResponseError when using MockAgent.

Environment

  • undici 7.3.0
  • node 22.13.0

Additional context

I'm trying to make a library where an option exist : throwOnError. Depending on the value, i'm setting the interceptor. When this is set to true, i want to be able to catch the error and to return the response body / statusCode / headers.
Actually, the only way i can do it is by always settings it to false, and use a code like :

if (option.throwOnError && !response.ok) {
  throw new ResponseError('', {
    body: await response.json()
    headers: response.headers,
    statusCode: response.statusCode
    // ...
  })
}

So i'm losing the ability to use the responseError interceptor

@blephy blephy added the bug Something isn't working label Jan 23, 2025
@blephy

This comment has been minimized.

@blephy blephy changed the title ErrorHandling with responseError interceptor ErrorHandling with responseError interceptor when using MockAgent Jan 23, 2025
@metcoder95
Copy link
Member

Can you aggregate a Minimum Reproducible Example to support you better?

@blephy
Copy link
Author

blephy commented Jan 24, 2025

Yes there is :)

https://github.com/blephy/undici-issue-mock

@blephy
Copy link
Author

blephy commented Jan 24, 2025

Ok.

Got this.

To be able to get it working :

  • you must set headers manually returned by the mock. I can understand that for a mock as it cannot know what a real server can return.
  • a body passed to reply can be returned in the error thrown only if 'content-type': 'application/json' is set. Unless, it's returned as '' (empty string)

So a working code is :

mockAgent
 .get('http://localhost:3000')
 .intercept({
        path: '/error',
        method: 'POST',
      })
 .replyContentLength()
 .reply(
        400,
        { data: 'error occurred' },
        {
          headers: {
            'content-type': 'application/json',
            connection: 'keep-alive',
            'keep-alive': 'timeout=5',
          },
        },
      )

// calling this mock is then throwing as expected : 

new errors.ResponseError('Response Error', 400, {
  body: { data: 'error occurred' },
  headers: expect.objectContaining({
    connection: 'keep-alive',
    'content-length': '25',
    'content-type': 'application/json',
    'keep-alive': 'timeout=5',
  }),
});

So this is not really a bug, this is a lack of documentation.

MockAgent could read the body passed through reply and set a content-type automatically but real dispatcher is not doing it when using undici. So it's cannot be a feature request too.

@blephy blephy closed this as completed Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants