Discuss Scratch

Jonathan50
Scratcher
1000+ posts

How to throw error from asynchronous reporter?

I have an asynchronous reporter that uses AJAX calls. If I have this code:
ext.exampleFunction = function(callback) {
	var xmlHttp = new XMLHttpRequest();
	xmlHttp.open("GET", "http://example.com/", true);
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				callback(xmlHttp.responseText);
			} else {
				//I want to throw an error here!
			}
		}
	};
}
If you don't know what I mean, try running this block:
((0) / (0))
Since the operation is impossible, the block has a red outline to show there is an error. I want my block to be able to this.
I don't know if it is currently possible, since it is not in the official documentation here.
Something like:
callback(error);
Jonathan50
Scratcher
1000+ posts

How to throw error from asynchronous reporter?

I tried ‘callback(0 / 0)’ and got NaN (not a number). I tried ‘throw “Could not connect!”’ but it does nothing and just goes forever since there is no callback (although it does log the uncaught error to the console). I tried ‘callback(throw “Could not connect!”)’ without expecting it to work and parsing the javascript failed.
Jonathan50
Scratcher
1000+ posts

How to throw error from asynchronous reporter?

For now I'm just using ‘callback()’ or ‘callback(null)’ and they both just return null but if there is any way I would like to know.
Jonathan50
Scratcher
1000+ posts

How to throw error from asynchronous reporter?

Bump
djdolphin
Scratcher
1000+ posts

How to throw error from asynchronous reporter?

I don't think there's any better way than what you're doing.
Jonathan50
Scratcher
1000+ posts

How to throw error from asynchronous reporter?

djdolphin wrote:

I don't think there's any better way than what you're doing.
Ok… Thanks!
Maybe ST could add that in the future?
Jonathan50
Scratcher
1000+ posts

How to throw error from asynchronous reporter?

What happened? Spam?

Powered by DjangoBB