ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.16k stars 504 forks source link

Is there a way to get 'pre-formatted' error message & line number? #1396

Open robnorr opened 2 years ago

robnorr commented 2 years ago

From a sol::protected_function_result, I do the following to check for, and get error messages:

if (!result.valid())
{
  sol::error err = result;
  std::cout << err.what() << std::endl;
}

This gives a nicely formatted message such as:

[string "..."]:3: '(' expected near 'foo'

What I'm wondering is if it's possible to get (from either the protected_function_result, or from the error, or from some other object) the line number (3) and the error message '(' expected near 'foo' separately? The main purpose for this is to highlight the line where the error occurs in an embedded script window, so preferably the number would come as a int type rather than a string type. If there is nothing built-in to sol I can of course extract the line number with regex, but I wanted to first check if there is a neater solution

Rochet2 commented 2 years ago

To me the error looks like normal lua error, which is formatted by lua and sol just captures the final string and gives it to you. So unless you modify/tap into the error handling of lua I suspect you would not be able to get the parts separately.