`Cannot read properties of undefined (reading 'defaultPort')` when used with `nock`
SDK you're using (please complete the following information):
- Version 5.1.0
Describe the bug
When using Nock to mock requests to Xero then error responses result in a Cannot read properties of undefined (reading 'defaultPort') error triggered in the ApiError class.
This appears to be caused by xero-node using an undocumented http.ClientRequest.agent property to fetch the port, which doesn't exist when using Nock (and possibly other mocking frameworks, too).
To Reproduce
- Setup a test using
nockto generate some kind of error response. For example:nock(/xero/) .get(/Accounts/) .reply(429); client.accountingApi.getAccounts(/*...*/); - Observe that this triggers the above error, instead of throwing an
ApiError
Expected behavior
I expect an ApiError to be thrown, instead of a cannot read properties of undefined
Screenshots
Additional context
http.ClientRequest.agent is an undocumented property, it's likely not intended as a public facing API. There's a documented alternative available to get the port: request.socket.localPort. So replacing
protocol: axiosError.request.protocol,
with
protocol: axiosError.request.socket.localPort
should give the same information, using a documented public api, and compatible with Nock.
PETOSS-396
Thanks for raising an issue, a ticket has been created to track your request
Have you had the chance to consider this issue? I'd be happy to submit a PR if you agree with the suggested change?
Same issue here
@TiddoLangerak The PR can maybe accelerate the fix :)
@sangeet-joy-tw Can we have update on this please ?
Hey, @sangeet-joy-tw Could we have an update on this, please? Thanks in advance 🙏
Hello @BenGDev @GraceYBR @JamesColemanXero @MattR-Api @MJMortimer @TheRegan This issue blocks us to update the xero-node dependency. We rely on nock to test our business services using Xero API.
Can you review and approve this PR please ? https://github.com/XeroAPI/xero-node/pull/682
@manishT72 @manishT72x Could you take a look at this issue and the PR linked, please ?
Hey @TiddoLangerak @QuentinLemCode @c4c1us
We have addressed the port issue in our latest xero-node v7.0.0.
Please install the latest version and kindly let us know if you guys are facing any more "undefined" issues with API Error class.