How to get the exact cause of an error from async HttpRequest?

I am trying to figure out how to find out the exact cause (async) of HttpRequest (from "dart: html"), and to be honest, I got a little lost here.

The onError callback receives only the HttpRequestProgressError object, which has nothing useful, and the HttpRequest object itself has the status "0" in case of failure, even the console shows "Failed to load the resource" without any details.

I want to know the exact reason - for example, “connection denied” or “hostname not allowed”.

Is this even possible?

Thanks!

+4
source share
2 answers

Unfortunately, there is no way to report an error in as much detail as possible. The reason is because JavaScript does not support this.

The HttpRequest object has the status and statusText (which you can get from your HttpRequestProgressEvent with evt.target , but those that represent HTTP status codes. Every other error has a status code of 0 - the request failed. This can be anything, and the only one the browser console is the place to view because it is an exception thrown by the browser.

If your request was synchronous, you could surround send() with try-catch. If your request is asynchronous, this will not work.

+4
source

See here

 #library('Request'); #import('dart:html'); #import("dart:json"); typedef void RequestHandler(String responseText); typedef void ErrorHandler(String error); class ResourceRequest { XMLHttpRequest request; RequestHandler _callbackOnSuccess; ErrorHandler _callbackOnFailure; ResourceRequest.openGet(String url, RequestHandler callbackOnSuccess, [ErrorHandler callbackOnFailure]) : request = new XMLHttpRequest(), _callbackOnSuccess = callbackOnSuccess, _callbackOnFailure = callbackOnFailure { request.open("GET", url, async : true); request.on.loadEnd.add((XMLHttpRequestProgressEvent e) => onLoadEnd(e)); } void send() { request.send(); } void onLoadEnd(XMLHttpRequestProgressEvent event) { if (request.readyState == 4 && request.status == 200) { _callbackOnSuccess(request.responseText); } else if (_callbackOnFailure != null) { _callbackOnFailure(request.statusText); } } } 
-1
source

Source: https://habr.com/ru/post/1478975/


All Articles