uppy/packages/@uppy/utils
Copilot 46f81e2bae
Fix isNetworkError to match MDN spec: readyState === 4 && status === 0 (#6050)
closes #4253

The `isNetworkError` function incorrectly classified XHR states as
network errors. Per MDN, a network error occurs when a request completes
(`readyState === 4`) but has no HTTP status (`status === 0`), indicating
network/CORS/file access failures.

## Changes

- **Logic fix**: Changed from `(xhr.readyState !== 0 && xhr.readyState
!== 4) || xhr.status === 0` to `xhr.readyState === 4 && xhr.status ===
0`
- **Test update**: Removed invalid test expecting `readyState: 2` to be
a network error; added test verifying incomplete requests return `false`

## Example

```typescript
// Before: incorrectly treated in-progress requests as network errors
isNetworkError({ readyState: 2, status: 0 })  // true 

// After: only completed requests with no status are network errors  
isNetworkError({ readyState: 4, status: 0 })  // true ✓
isNetworkError({ readyState: 2, status: 0 })  // false ✓
```

<!-- START COPILOT CODING AGENT SUFFIX -->



<details>

<summary>Original prompt</summary>

> Update the isNetworkError function in
packages/@uppy/utils/src/isNetworkError.ts to correctly detect network
errors according to MDN documentation. The new logic should return true
only if xhr.readyState === 4 and xhr.status === 0. The updated
implementation should be:
> 
> function isNetworkError(xhr?: XMLHttpRequest): boolean {
>   if (!xhr) return false
> // finished but status is 0 — usually indicates a network/CORS/file
error
>   return xhr.readyState === 4 && xhr.status === 0
> }
> 
> export default isNetworkError
> 
> No other logic changes are needed. If you find related commentary
(e.g., outdated comments), clarify as needed.


</details>

*This pull request was created as a result of the following prompt from
Copilot chat.*
> Update the isNetworkError function in
packages/@uppy/utils/src/isNetworkError.ts to correctly detect network
errors according to MDN documentation. The new logic should return true
only if xhr.readyState === 4 and xhr.status === 0. The updated
implementation should be:
> 
> function isNetworkError(xhr?: XMLHttpRequest): boolean {
>   if (!xhr) return false
> // finished but status is 0 — usually indicates a network/CORS/file
error
>   return xhr.readyState === 4 && xhr.status === 0
> }
> 
> export default isNetworkError
> 
> No other logic changes are needed. If you find related commentary
(e.g., outdated comments), clarify as needed.

<!-- START COPILOT CODING AGENT TIPS -->
---

 Let Copilot coding agent [set things up for
you](https://github.com/transloadit/uppy/issues/new?title=+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: mifi <402547+mifi@users.noreply.github.com>
2025-11-05 13:06:21 +08:00
..
src Fix isNetworkError to match MDN spec: readyState === 4 && status === 0 (#6050) 2025-11-05 13:06:21 +08:00
.npmignore meta: exclude tsconfig files from npm bundles (#4916) 2024-02-13 23:21:08 +01:00
CHANGELOG.md [ci] release (#6049) 2025-11-03 14:26:07 +01:00
LICENSE Add @uppy/utils skeleton. 2018-06-14 16:31:19 +02:00
package.json [ci] release (#6049) 2025-11-03 14:26:07 +01:00
README.md Fix links (#5492) 2024-10-29 13:54:00 +01:00
tsconfig.build.json Remove preact/compat (#5935) 2025-08-28 16:57:07 +02:00
tsconfig.json Remove preact/compat (#5935) 2025-08-28 16:57:07 +02:00

@uppy/utils

Uppy logo: a smiling puppy above a pink upwards arrow

npm version CI status for Uppy tests CI status for Companion tests CI status for browser tests

Shared utility functions for Uppy Core and the “official” plugins maintained by the Uppy team.

Uppy is being developed by the folks at Transloadit, a versatile file encoding service.

Installation

Unless you are creating a custom plugin, you should not need to install this manually.

$ npm install @uppy/utils

License

The MIT License.