Jump to content

Lua error handling


Recommended Posts

Hey all,

Can anybody help me understand error handling with things like pcall?

I'd like to setup some scripts that are prone to failure with pcall, so that my dedicated server doesn't stop when/if they fail.

I have a script that writes files, and that can fail for various reasons, and when that happens, the error message comes up on the dedicated server machine, and the server freezes until I remote in and close the error window.

 

What I'd like to do is use pcall, and have that trigger a trigger.action.outText rather than soft crash the server.

I've looked at some documentation on pcall, but haven't gotten it to work.

 

As a second option, IIRC there is a configuration file one can edit to stop those error windows showing as well, but I'm not certain if that's true, and what I'd have to do if it is.

Link to comment
Share on other sites

1 hour ago, Wrench said:

...As a second option, IIRC there is a configuration file one can edit to stop those error windows showing as well, but I'm not certain if that's true, and what I'd have to do if it is.

You maybe refer to this :

"Here's the good news: It's really easy to prevent DCS from displaying a message box when it encounters an error in a Lua script: Simply set env.setErrorMessageBoxEnabled(boolean on) to false: env.setErrorMessageBoxEnabled(false)"

From

 

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
On 12/14/2021 at 3:06 PM, Wrench said:

Huh, I'd have thought that would have to go in a global config. 

Thanks, toutenglisse

I'd still like to understand pcall better, though.

 

I don't know if you know any other programming languages or scripting languages but PCALL is kinda like a try catch, kinda. 

Here is the documentation: 

 

Quote

pcall (f, arg1, ···)

Calls function f with the given arguments in protected mode. This means that any error inside f is not propagated; instead, pcall catches the error and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false plus the error message.

 

You can use it like this:
 

function safelyCall(func, args)
  local success, result = pcall(func, args)
  if not success then
    --log, try to determine why it failed with the 'result' variable or just fail gracefully
    return 0 
  else 
    return result
  end
end



function divide(a, b)
  return a / b
end



function catchDivideByZero()
  local result = self:safelyCall(self:divide, { 4, 0 })
  -- print / use result
end

 

I've just done this from memory without testing so I can't promise it's perfect, but it should give you an idea on how you can best use it.

Hope this helps.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...