Jump to content
engima

Is is possible to detect script errors?

Recommended Posts

Is there a way to detect script errors in code?

 

I't would have been nice if try catch did that, but it's designed to only catch stuff that is explicitly thrown.

 

So I was wondering, what if I spawn a script and get the handle. Can I use a global variable to see if the spawned script generated a script error? The idea is that the last line of assignment will not be executed if the script run into errors. Is there a better way?

 

My idea:


globVarScriptHadScriptErrors = true;

var _handle = [] spawn { doing stuff...; globVarScriptHadScriptErrors = false; };

 

waitUntil { scriptDone _handle };

 

if (globVarScriptHadScriptErrors) then {

    player sideChat "script had errors";

};

  • Like 1

Share this post


Link to post
Share on other sites

This is just a thought but if you call a script and it has error then I believe nil is returned. I could be wrong though, haven't tested it

myFn =
{
 _ret = true;

// other code

_ret
};


_ret = call myFn;


if(isnil "_ret") then
{
 systemchat "Script failed";
};

 

  • Like 1

Share this post


Link to post
Share on other sites

I hate to ask, but do you have "show script errors" checked? In the Arma launcher it's under parameters, all parameters, author, or you can just pass it as a parameter if using another launcher. You can also check the logs, I have a shortcut to mine. On Win 7 it may be found at C:\Users\{user name}\AppData\Local\Arma 3

 

You may also try something like this which reveals many errors up front which are found in separate scripts you have written: call compile preprocessFileLineNumbers "script_name.sqf";

 

 

  • Like 1

Share this post


Link to post
Share on other sites

Expanding further. -showScriptErrors as @AZCoder explained is always good to have on. If you use params command then you can set default values and required data types.

https://community.bistudio.com/wiki/params

You can also detect certain things manually and use some message output like:

if (isNil "_database") exitWith {["No argument was passed for _database (0)"] call BIS_fnc_error;};

https://community.bistudio.com/wiki/BIS_fnc_error

There is also:

https://community.bistudio.com/wiki/scriptDone

Not sure what is returned if the script breaks down.

And:

https://community.bistudio.com/wiki/scriptNull

One use is to detect non exist scripts.

  • Like 1

Share this post


Link to post
Share on other sites
19 hours ago, gc8 said:

This is just a thought but if you call a script and it has error then I believe nil is returned. I could be wrong though, haven't tested it


myFn =
{
 _ret = true;

// other code

_ret
};


_ret = call myFn;


if(isnil "_ret") then
{
 systemchat "Script failed";
};

 

 

Thanks, sounds promising. I'll try that.

 

But I wonder what will happen if one have a script with lets say 10 lines of code, and there is a script error on first line. Will the script recover on e.g. line 5, and then return normal (not nil). Well, let's see...

Share this post


Link to post
Share on other sites

Usually in that situation, the script will quit at line 5 and not return any data. The script that called it will return an undefined variable when it tries to use the returned variable.

Share this post


Link to post
Share on other sites

Well I tested this script and unfortunately it doesn't work as intended but I learned new lesson:

 

cond = 
{
 true
};

testFn =
{

 if(call cond) then
 {
  systemchat "scope 4";
  
  for "_i" from 0 to 1 do
  {
   _tes = _i / 0; // Error zero divisor!
   
   systemchat "scope 3";
  };
  
  systemchat "scope 1";
 };
 
 systemchat "scope 2";

 false
};

_ret = call testFn;
if(isnil "_ret") then
{
 systemchat "NIL!";
}
else
{
 systemchat format["_ret = %1",_ret];
};

 

It will print:

 

scope 4

scope 3

scope 3

scope 1

scope 2

_ret = false

 

 

So the function nor any scope does not exit any scopes but will keep continuing the execution

 

Share this post


Link to post
Share on other sites

Thank you, gc8, for testing! But too that bad it seems to be a problem that has no solution.

 

I wonder though, how come "Scope 3" is only printed once? I think it should have been printed twice or not at all...?

Share this post


Link to post
Share on other sites
4 minutes ago, engima said:

I wonder though, how come "Scope 3" is only printed once? I think it should have been printed twice or not at all...?

 

That was my bad, it is in fact printed twice. Updated my post

Share this post


Link to post
Share on other sites

Errors (except compilation related errors like wrong syntax etc.) won't stop loops/scripts. If an error occurs, the script continues and most likely will spit out more errors.

 

Dividing through zero returns 0 (probably programming practice, at least it doesn't return positive or negative infinity, heh), so the _tes variable actually holds 0, the error message tells you you're a bad boy but won't put you to sleep, at least you got the syntax right and life (the script) goes on.

If you use player setPos [] the snippet will also continue (syntax is ok, but parameters are not).

Same goes for putting in data types without doing anything with them (string without assigning it to a variable etc.).

Up to you to handle errors, heh.

 

If you want to terminate a script you need to use try, catch and throw.

 

Also using systemchat for debugging is a bit tedious, since it can only display 6 or so lines, better push all messages to an array and hint that, or use diag_log.

 

Cheers

 

  • Like 5

Share this post


Link to post
Share on other sites

Had no idea SQF/ArmA had this. Thought it was a PHP specific thing. Thanks for sharing!

  • Like 1

Share this post


Link to post
Share on other sites

Why not simply check your rpt file for your test and sqf beyond? The link and discussion are rather old but most of msg are understandable.

I can't see the finality of your topic. You can built your work like:

yourTest = compile preprocessFileLineNumbers "yourTest.sqf"

This command is very useful (at least for me!).

 

Now, if you intend to manage some condition along with an error occurrence, I think it's weird. First aim should be to avoid any error!

And, even if some BI commands can throw unexpected result, you can use an arsenal of tools to workaround that, in a decent way.

params  ,  isEqualTypeParams  ,...

  • Like 2

Share this post


Link to post
Share on other sites
14 hours ago, HazJ said:

Expanding further. -showScriptErrors as @AZCoder explained is always good to have on. If you use params command then you can set default values and required data types.

https://community.bistudio.com/wiki/params

You can also detect certain things manually and use some message output like:


if (isNil "_database") exitWith {["No argument was passed for _database (0)"] call BIS_fnc_error;};

https://community.bistudio.com/wiki/BIS_fnc_error

There is also:

https://community.bistudio.com/wiki/scriptDone

Not sure what is returned if the script breaks down.

And:

https://community.bistudio.com/wiki/scriptNull

One use is to detect non exist scripts.

As I stated in my original post.

 

Also, better to use CfgFunctions.

https://community.bistudio.com/wiki/Functions_Library_(Arma_3)

  • Like 2

Share this post


Link to post
Share on other sites

I'm writing a unit test framework, and it would be a bit more convenient to be able to handle the script errors for the test report being generated.

 

http://typesqf.no-ip.org/cpack/details/Sqx.UnitTest

 

try-catch would have been perfect if the general errors would have been catched, but they are not, so I guess I will have to live with it as it is.

 

Thanks everyone for a good discussion!

  • Like 2

Share this post


Link to post
Share on other sites

My Intercept SQF-Assembly mod can emit events or call a function on script error. In my SQF Debugger I use it to print a callstack on error.

You could rewire that to execute a `throw` command such that you can use try/catch with script errors.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×