gpt4 book ai didi

parameters - Lua:使用pcall

转载 作者:行者123 更新时间:2023-12-05 06:41:01 32 4
gpt4 key购买 nike

我有一段发送 udp 消息的 Lua 代码,它正在工作。但是我想让它更防错。所以我输入了错误的 dns 名称导致解析失败。我真的不在乎那个 block 中的什么失败,我只想能够优雅地处理错误。在其他语言中,我会使用 try-catch。在这里我明白 pcall 应该填补这个空白。所以我尝试使用 pcall 并尝试将 buttonPin 传递给 sendMessageToServer() 函数。

这个方法不行,什么都抓不到,全节点崩溃:

if(pcall(sendMessageToServer, buttonPin))
then
ledOn(ledGreen)
print("Message sent.")
else
ledOn(ledRed)
print("Error: Message could not be sent.")
end

这种方法恰恰相反:它不会崩溃并且看起来一切正常,所以 pcall 返回 true:

if(pcall(function() sendMessageToServer(buttonPin) end))
then
ledOn(ledGreen)
print("Message sent.")
else
ledOn(ledRed)
print("Error: Message could not be sent.")
end

但它应该运行...如果我简单地删除单词 pcall,使该函数定期运行,就会按预期发生崩溃。

最好的问候,延斯

更新:完整的代码和故意造成的错误

require ("config")
require ("cryptography")

function armAllButtons()

ledOff(ledGreen)
ledOff(ledYellow)
ledOff(ledRed)

for i,v in ipairs(buttonPins)
do
--print(i,v)
armButton(v)
end
end

function armButton(buttonPin)
print("Arming pin "..buttonPin.." for button presses.")

gpio.mode(buttonPin,gpio.INT,gpio.FLOAT)
gpio.trig(buttonPin, direction, function () notifyButtonPressed(buttonPin) end)

print("Waiting for button press on "..buttonPin.."...")
end

function notifyButtonPressed(buttonPin)
--print("Button pressed. Notifiying server at "..serverIp..":"..serverPort)

-- show status
ledOn(ledYellow)

print("Button at pin "..buttonPin.." pressed.")

ledOff(ledGreen)
ledOff(ledYellow)
ledOff(ledRed)

-- show status
--if(pcall(sendMessageToServer,buttonPin))
if(sendMessageToServer(buttonPin))
then
ledOn(ledGreen)
print("Message sent.")
else
ledOn(ledRed)
print("Error: Message could not be sent.")
end

-- Rearm pin for interrupts
armButton(buttonPin)
end

function sendMessageToServer(buttonPin)
print("Notifying server at "..serverIp..":"..serverPort)
--TODO: Include some variable. The current code is vulnerable to replay attacks.

conn = net.createConnection(net.UDP, 0)
conn:connect(serverPort,serverIp)
local msg = node.chipid()..";"..buttonPin..";ButtonPressed"
local hash = getHashValue(msg..encryptionPassword)
print("Sending "..msg.." with hash "..hash.." to server "..serverIp..":"..serverPort)
conn:send(msg..';'..hash)
conn:close()
conn = nil
end

function ledOn(ledPin)
gpio.write(ledPin,gpio.HIGH)
end

function ledOff(ledPin)
gpio.write(ledPin,gpio.LOW)
end


armAllButtons()

不使用 pcall 时出错:

dofile("button.lua")
Arming pin 5 for button presses.
Waiting for button press on 5...
Arming pin 6 for button presses.
Waiting for button press on 6...
> Button at pin 5 pressed.
Notifying server at <incorrectDnsEntry>:36740
Sending 14695197;5;ButtonPressed with hash <someHashValue> to server <incorrectDnsEntry>:36740
Error: Message could not be sent.
Arming pin 5 for button presses.
Waiting for button press on 5...
DNS retry 1!
DNS retry 2!
DNS retry 3!
DNS retry 4!
DNS Fail!
?ˆÈ)ŠâF
‘ŽF
”Œ¦ú

NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
Arming pin 5 for button presses.
Waiting for button press on 5...
Arming pin 6 for button presses.
Waiting for button press on 6...
IP unavaiable, Waiting...
> IP unavaiable, Waiting...
IP unavaiable, Waiting...
IP unavaiable, Waiting...
IP unavaiable, Waiting...
Config done, IP is 192.168.x.x

使用 pcall 显示:

Config done, IP is 192.168.x.x
Button at pin 5 pressed.
Notifying server at <incorrectDnsEntry>:36740
Sending 14695197;5;ButtonPressed with hash <someHashValue> to server <incorrectDnsEntry>:36740
Message sent.
Arming pin 5 for button presses.
Waiting for button press on 5...

所以我没有手动发出错误信号。

最佳答案

您使用的是 LuaSockets 吗?您应该知道 LuaSockets 中的函数实际上不会引发错误。大多数功能,如 udp send ,成功时返回 1,出错时返回 nil + msg。如果您想引发错误,您需要检查返回值并自己引发错误。一种非常常见的做法是将调用包装在断言中,如下所示:

assert(conn:send(msg..';'..hash))

当您说它可以在没有 pcall 的情况下工作时,您实际上是在自欺欺人。看起来你正在这样做:

if(sendMessageToServer(buttonPin))
then
ledOn(ledGreen)
print("Message sent.")
else
ledOn(ledRed)
print("Error: Message could not be sent.")
end

上面的代码将始终打印“错误:消息无法发送。”因为 sendMessageToServer 函数从不返回任何东西,在这种情况下,if 条件将始终评估为 false,并且无论您是否设法发送数据与否。

关于parameters - Lua:使用pcall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535531/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com