Rxjs: Ajax response with status 0 must throw an error

Created on 26 Apr 2018  路  6Comments  路  Source: ReactiveX/rxjs

If ajax observable has parameter timeout configured, with a slow request, ajax is emitting a next instead of an error.

RxJS version:
6.0.0

Code to reproduce:

ajax({
  url: 'https://reqres.in/api/users?delay=5',
  timeout: 500,
}).subscribe({
  next: v => console.log('next', v),
  error: err => console.log('err', err),
});

Expected behavior:

Emit an error event with an AjaxError object

Actual behavior:

Emit a next event with ajax null response and status 0

bug

Most helpful comment

Workaround:

tap(response => {
            if (response.status === 0) {
                throw Object.assign(new Error('Ajax status 0'), response)
            }
        }),

All 6 comments

This bug prevents me from catching errors when a user makes an XHR request with a downed network, independent of a timeout value being set.

Workaround:

tap(response => {
            if (response.status === 0) {
                throw Object.assign(new Error('Ajax status 0'), response)
            }
        }),

I believe #3653 also fixes the issue with down network: moving the next() logic from onreadystatechange to onload allows onerror to trigger first.

Tested and working on 6.4.*

rxjs-ajax

In which version was this actually fixed?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dooreelko picture dooreelko  路  3Comments

haf picture haf  路  3Comments

chalin picture chalin  路  4Comments

peterbakonyi05 picture peterbakonyi05  路  4Comments

Zzzen picture Zzzen  路  3Comments