How to get data from exception in lua?

Viewed 58

how to get data as table from exception in lua??

x * 2

-- stdin:1: attempt to perform arithmetic on a nil value (global 'x')
-- stack traceback:
--         stdin:1: in main chunk
--         [C]: in ?

how to get info from exception? here example

print(getErrorInfo()['message'])
print(getErrorInfo()['line'])

-- attempt to perform arithmetic on a nil value (global 'x')
-- 1
1 Answers

You have to parse error message

local function func_to_call() x = x*2 end

local ok, err_obj = pcall(func_to_call)
if not ok then
  local line_no, err_mes = tostring(err_obj):match":(%d+): (.*)"
  local err_info = {message = err_mes, line = tonumber(line_no)}
  print(err_info.line, err_info.message)
end
Related