Jason Cheek
Posts: 0
Joined: Sun Jan 12, 2014 8:00 am

Server code: exit, return, abort?

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Server code: exit, return, abort?

Hi Jason,

You can do it with following line of JS code:

pre

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

/pre

Regards.

Jason Cheek
Posts: 0
Joined: Sun Jan 12, 2014 8:00 am

Server code: exit, return, abort?

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.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Server code: exit, return, abort?

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.

Jason Cheek
Posts: 0
Joined: Sun Jan 12, 2014 8:00 am

Server code: exit, return, abort?

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Server code: exit, return, abort?

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.

Jason Cheek
Posts: 0
Joined: Sun Jan 12, 2014 8:00 am

Server code: exit, return, abort?

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

Return to “Issues”