-
Notifications
You must be signed in to change notification settings - Fork 584
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
Labels
bug
Something isn't working
Comments
This comment has been minimized.
This comment has been minimized.
blephy
changed the title
ErrorHandling with responseError interceptor
ErrorHandling with responseError interceptor when using MockAgent
Jan 23, 2025
Can you aggregate a Minimum Reproducible Example to support you better? |
Yes there is :) |
Ok. Got this. To be able to get it working :
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Description
When using responseError interceptor, the error thrown is a ResponseError which always include an empty property
body: ''
and an empty propertyheaders: {}
. 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
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 :
So i'm losing the ability to use the responseError interceptor
The text was updated successfully, but these errors were encountered: