AleksandrRogov / DynamicsWebApi

DynamicsWebApi is a Microsoft Dataverse Web API helper library for JavaScript & TypeScript
MIT License
268 stars 58 forks source link

Wrong error message in case of network error #153

Closed Gargair closed 1 year ago

Gargair commented 1 year ago

DynamicsWebApi version For example: v1.7.6

Describe the bug In case of a client side network error the returned error message is "Unexpected Error" instead of "Network Error". Additionally a NPE will be logged in the console. (TypeError: Cannot read properties of null (reading 'getAllResponseHeaders'))

Expected behavior A single error with the expected error message

Steps To Reproduce

  1. Open DevTools
  2. Go to Network tab
  3. Set mode to offline
  4. Send request

Your Setup:

Additional context The problem is the incorrect error handling in case of a network error. In case of client side network errors the request will be instantly completed with a status of 0. As such the onreadystatechange function is called with readyState 4 and request.status 0. The library then fails to parse the response and returns an error message of "Unexpected Error". And dereferences the request. After that the onerror function is called which then errors out due to a NPE.

The correct handling would be to explicitly ignore a request.status of 0 in onreadystatechange function. In these cases the onerror function will be called.

From https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status:

Before the request completes, the value of status is 0. Browsers also report a status of 0 in case of XMLHttpRequest errors.

AleksandrRogov commented 1 year ago

@Gargair thank you for submitting an issue. I will look into it.

AleksandrRogov commented 1 year ago

Fixed in v.1.7.11. Thank you for reporting it @Gargair .