|
187 | 187 | return ui8a; |
188 | 188 | }; |
189 | 189 |
|
| 190 | + Utils.onHttpRequestErrorHandler = function (xhr, errorHandler, responseParser) { |
| 191 | + return function() { |
| 192 | + var errorMessage = 'Unable to connect to the network'; |
| 193 | + |
| 194 | + //this part is needed for those implementations of XMLHttpRequest |
| 195 | + //which call the onerror handler on an any bad request |
| 196 | + if (xhr.status >= 400) { |
| 197 | + errorMessage = responseParser(xhr); |
| 198 | + } |
| 199 | + |
| 200 | + errorHandler(errorMessage); |
| 201 | + }; |
| 202 | + }; |
| 203 | + |
190 | 204 | function tryParseJSON(s) { |
191 | 205 | try { |
192 | 206 | return typeof s === 'string' ? JSON.parse(s) : s; |
|
336 | 350 |
|
337 | 351 | if (config.isAsync) { |
338 | 352 | xhr.onreadystatechange = function() { |
339 | | - if (xhr.readyState == 4) { |
| 353 | + if (xhr.readyState == 4 && xhr.status) { |
340 | 354 | if (xhr.status >= 200 && xhr.status < 300) { |
341 | 355 | response = parseResponse(xhr); |
342 | 356 | cacheHandler(response); |
|
348 | 362 | } |
349 | 363 | } |
350 | 364 | }; |
| 365 | + |
| 366 | + if (config.asyncHandler.fault) { |
| 367 | + xhr.onerror = Utils.onHttpRequestErrorHandler(xhr, config.asyncHandler.fault, badResponse); |
| 368 | + } |
351 | 369 | } |
352 | 370 |
|
353 | 371 | xhr.send(config.data); |
|
2141 | 2159 | asyncHandler: responder |
2142 | 2160 | }); |
2143 | 2161 | }, |
2144 | | - |
| 2162 | + |
2145 | 2163 | /** @deprecated */ |
2146 | 2164 | addPoint: function(geopoint, async) { |
2147 | 2165 | return this.savePoint.apply(this, arguments); |
|
3417 | 3435 |
|
3418 | 3436 | if (async) { |
3419 | 3437 | xhr.onreadystatechange = function() { |
3420 | | - if (xhr.readyState == 4) { |
| 3438 | + if (xhr.readyState == 4 && xhr.status) { |
3421 | 3439 | if (xhr.status >= 200 && xhr.status < 300) { |
3422 | 3440 | async.success(JSON.parse(xhr.responseText)); |
3423 | 3441 | } else { |
3424 | 3442 | async.fault(JSON.parse(xhr.responseText)); |
3425 | 3443 | } |
3426 | 3444 | } |
3427 | 3445 | }; |
| 3446 | + |
| 3447 | + xhr.onerror = Utils.onHttpRequestErrorHandler(xhr, async.fault, badResponse); |
3428 | 3448 | } |
3429 | 3449 |
|
3430 | 3450 | xhr.send(encoded ? options.data : Utils.stringToBiteArray(getBuilder(options.fileName, options.data, boundary))); |
|
0 commit comments