flukso/lua-mosquitto — pull request #5 by karlp (2014-11-11 20:13:49+00:00)
Title: rfc: allow a handler to be run in the case of errors

Currently, any sort of syntax or runtime error in a ON_XXX handler is
simply swallowed by the pcall() This change demonstrates how to supply
a lua handler that can at least get the primary error message.

This is _incomplete_, only implemented for the ON_MESSAGE callback, but
can be added to all of them if this is desired.

I attempted to use the "errfunc" callback, but it's more confusing, and
was giving me stack overflows when I had errors inside the error
handler. The current code simply uses pcall again to protect from
errors in the handler.

Example usage:

```
mqtt:set_callback(mosq.ON_ERROR, function(msg, rc)
print("Karl's error handler:", msg, rc)
-- we can only support a single layer of error handling!
print(q + 45)
end)

mqtt:set_callback(mosq.ON_MESSAGE, function(mid, topic, jpayload, qos,
retain)
print(string.format("Received mid: %d on topic: %s", mid, topic))
error("Simulate a syntax or other error inside your callback")
print(".... and, exited happily ;)")
end)
```