Page 1 of 1

Server code: exit, return, abort?

Posted: Tue Dec 02, 2014 11:21 pm
by Jason Cheek

How do I end a server script early without running to the end of the file?

For example:

codeif(...) {
response.error("POST JSON parse error: " + e, 'Bad Request');
return;
} else {
...
}/code


Server code: exit, return, abort?

Posted: Wed Dec 03, 2014 3:47 am
by Yurii Orishchuk

Hi Jason,

You can do it with following line of JS code:

pre

response.success("Some response content.");

/pre

Regards.


Server code: exit, return, abort?

Posted: Wed Dec 03, 2014 4:48 am
by Jason Cheek

But it seems when I have a situation like:

pre
response.error("Some response content #1.");
...
response.success("Some response content #2.");
...
response.error("Some response content #3.");
/pre

Only "content #3" is returned. I'm looking for a way to end the script immediately when I want to return a error.

Right now I have a lot of

pre
if (!abort) {
...
}
...
if (!abort) {
...
}
...
/pre

And I'm looking for a cleaner way to code the scripts.


Server code: exit, return, abort?

Posted: Wed Dec 03, 2014 7:49 pm
by Evgene Karachevtsev

Hello Jason,

Please try using return after your if clause.
Also please try to write proper javascript which exclude several conditions to have 'true' value.


Server code: exit, return, abort?

Posted: Wed Dec 03, 2014 10:23 pm
by Jason Cheek

When I try I get this error:

preScript test: SyntaxError: Illegal return statement ( @ 3 : 2 ) - return;/pre

My test script was:

preif (request.get('val') == 'yes') {
response.success("End #1", "text/plain");
return;
}

response.success("End #2", "text/plain");/pre


Server code: exit, return, abort?

Posted: Thu Dec 04, 2014 4:44 am
by Yurii Orishchuk

Hi Jason,

"return" statement could be used only withing the function.

Thus please try to wrap all your code with some function and call this function:

pre

var InvokeServiceFunction(){

Code: Select all

//Here is all your needed JS code. Here you can use "return" statement. 

};

InvokeServiceFunction();

/pre

Regards.


Server code: exit, return, abort?

Posted: Thu Dec 04, 2014 5:02 am
by Jason Cheek

Oh! That makes sense. I'll give that a try.