Skip to content

ApiError throws when response is undefined – fails to handle network/auth errors gracefully #765

@Goran-n

Description

@Goran-n

SDK you're using (please complete the following information):

Version: 13

Describe the bug
The ApiError class in xero-node does not handle missing response objects from Axios errors, which causes it to throw a secondary error (TypeError: Cannot read property 'status' of undefined) when the underlying cause is something like a network issue, authentication failure, or misconfiguration (e.g. incorrect tenant ID).

To Reproduce

Configure a Xero client with:

An invalid or expired access token

Or a non-existent tenant ID

Make any authenticated request to the API, such as xero.accountingApi.getInvoices(...)

Observe that the request fails, but instead of cleanly reporting the underlying issue, it throws a secondary error due to accessing axiosError.response.status directly when response is undefined.

Expected behavior
ApiError should gracefully handle the absence of a response object in the error, returning a status code of 0 or a sensible fallback value. This would allow users to correctly diagnose configuration or network issues.

Proposed Fix
Update the ApiError constructor in xero-node:

// Current buggy code
this.statusCode = axiosError.response.status;

// Proposed fix
this.statusCode = axiosError.response?.status || 0;
Screenshots / Stack Trace

TypeError: Cannot read property 'status' of undefined
at new ApiError (/node_modules/xero-node/dist/src/XeroClient.js:XXX)
Additional context
In our situation, the request likely failed due to:

An invalid or expired access token

Network issues (API unreachable)

An incorrect tenant ID

This bug suggests that the request never successfully reached Xero's servers. Fixing this would greatly improve developer experience when debugging auth or connectivity issues.

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