是否可以更改Lua错误消息的输出?

时间:2018-08-13 18:49:36

标签: lua esp8266 nodemcu firmware

我设法通过修改dbg_printf方法来更改错误消息的输出。但是,该方法不能处理以下错误消息:

lua: ?:0: attempt to call global 'log' (a nil value)

哪种方法可以处理这些类型的错误?

2 个答案:

答案 0 :(得分:1)

错误消息来自函数ldebug.c中的文件luaG_typeerror。但是我想您使用的是较旧的Lua版本,因为我的信息有些不同:

attempt to call a nil value (global 'log')

如果可以的话,您可以尝试防止该错误:

if type(log) == "function" then
   log() 
end

或@lhf说使用https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Comparing_Objects

if pcall(log) then
    -- no errors while running 'log'
    ...
else
    -- 'log' raised an error: take appropriate actions
    ...
end

答案 1 :(得分:0)

它应该比研究C api更简单。

就像@lhf一样:

if pcal(risky) then 
  print("this works")
else
  print("phooey!")
end

或者,您可以停止程序并获得如下错误消息:

if pcal(risky) then 
  print("this works")
else
  error("your error message here")
end