Exception Handling

I am not used to javascript and want to add an exception handling when the ajax-request to the host could not be enabled. I use the example Bitbucket and tried a try-catch-block and in jquery the error: {}-statement. But nothing seem to work.

My goal is to display “only” the macro-block-content when an error or an exception to the host occurs. Is that possible?

Hi Matthias,

Am I correct in assuming that you are talking about this request:

$.ajax({
	url: "data",
	beforeSend: addJwtHeader
}).done(function (data) {
	$("#data").text("Host: " + JSON.stringify(data, null, '\t'));
	AP.resize();
});

The way you would “catch an error” here is not by using try catch blocks but adding a fail handler:

$.ajax({
	url: "data",
	beforeSend: addJwtHeader
}).done(function (data) {
	alert("it worked!");
	console.log(data);
}).fail(function (error) {
	alert("it failed!");
	console.log(error);
});

If the request was successful the function you provided to done will be called. If it failed then the function you provided to fail will be called.

My goal is to display “only” the macro-block-content when an error or an exception to the host occurs. Is that possible?

So… not sure if I understand correctly but in that case just show your content in the fail function and do nothing in the done part (might as well delete it then). :slight_smile: