Skip to content

feat(adapter): surface low‑level network error details; attach origin…#6982

Merged
jasonsaayman merged 3 commits intoaxios:v1.xfrom
mkhani01:feat/networkErrorDetails
Jul 30, 2025
Merged

feat(adapter): surface low‑level network error details; attach origin…#6982
jasonsaayman merged 3 commits intoaxios:v1.xfrom
mkhani01:feat/networkErrorDetails

Conversation

@mkhani01
Copy link
Contributor

✨ Title

feat(adapter): surface low-level network error details; attach original error via cause

🧾 What?

This pull request enhances how network errors are surfaced in both Node HTTP and XHR adapters for Axios:

  • Node HTTP adapter: Promotes the low-level err.code into AxiosError.code, prefixes the error message (e.g. ECONNREFUSED – …), and preserves the original error in Error.cause.
  • XHR adapter: Maintains the browser’s ProgressEvent as error.event and uses its message when available.
  • Tests: Adds Node ESM tests under test/unit/adapters to verify code and cause behavior.
  • Types: Ensures AxiosError.cause?: unknown and event?: ProgressEvent are present in type definitions.

❓ Why?

Provides greater error transparency for developers. By preserving both code and original error context (cause), we enable easier debugging in both server and browser environments. This change improves observability and developer experience.

🔧 How?

  • In the Node HTTP adapter, the low‑level error’s code is now promoted to AxiosError.code, and the message is prefixed accordingly. The original exception is attached via the standard Error.cause.
  • In the XHR adapter, the ProgressEvent from the browser error.event is preserved and used when its message is present.
  • New Node ESM tests assert correct propagation of code, cause, and event.
  • Type declarations updated to include safe optional fields: cause?: unknown and event?: ProgressEvent.

✅ Testing

  • Added Node‑based ESM tests in test/unit/adapters specific to code, cause, and event semantics.
  • Manually tested in browser and Node to verify that both adapters surface network errors consistently with expected formatting.

📎 Related tickets / Issues

6965

🧵 Any breaking changes?

No breaking API changes. This preserves backwards-compatible optional behaviors; adapters will still function normally if no err.code or event is present.

Summary

This PR significantly improves debugging clarity by surface-level error code preservation, original error chaining via cause, and richer type safety—while ensuring both browser and Node adapters behave consistently.

…al error via `cause`

Node http adapter:
- Promote low-level `err.code` to `AxiosError.code`, prefixing message (e.g. `ECONNREFUSED – …`)
- Keep original error on standard `Error.cause`

XHR adapter:
- Preserve browser `ProgressEvent` on `error.event`
- Use event message when available

Tests:
- Add Node ESM tests under `test/unit/adapters` to assert `code` and `cause` behavior

Types:
- Ensure `AxiosError.cause?: unknown` and `event?: ProgressEvent` are present
@jasonsaayman jasonsaayman added this to the v1.12.0 milestone Jul 30, 2025
Copy link
Member

@jasonsaayman jasonsaayman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nicely done @mkhani01! 🔥

@jasonsaayman jasonsaayman merged commit 78b290c into axios:v1.x Jul 30, 2025
12 checks passed
@ialiaslani
Copy link

Had the same problem, thanks @mkhani01 🙏🏻♥️

@github-actions github-actions bot mentioned this pull request Sep 11, 2025
@github-actions
Copy link
Contributor

Hi, @mkhani01! This PR has been published in v1.12.0 release. Thank you for your contribution ❤️!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants