Error handling is not graceful in callback function
After I have registered a callback to a luv handle, when the callback need to be called, we use luv_call_callback to call it. It's is OK when there is no exception when calling the callback function. But when error occured, it just print the stack traceback and call exit to end the process.
I think it is better to provide an alternative way just like "uncaughtException" in nodejs to allow user to do more control.
And I think luv_call_callback should to be exported just like "MakeCallback" in NAN, so it can provide a consistent error handling machanism when I write a extension for luv.
The idea is that error handling will be handled at the lua level instead of at the C level. The fatal exit here is to prevent strange undefined behavior when errors go uncaught.
After thinking about this for a while, I do think this needs to be handled at the luv level. How exactly does NAN make MakeCallback configurable? My idea is to allow customizing from the lua side a function that intercepts all call stacks:
function luv.makeCallback(fn)
-- do anything custom here if desired
return fn
end
And so a custom one would wrap in xpcall and route errors
function luv.makeCallback(fn)
return function (...)
local args = {...}
local success, err = xpcall(function ()
return fn(unpack(args))
end, debug.traceback)
-- TODO: handle success, and err
end
end
Though I worry about the performance hit from adding all these closures to every event source.
See #128 for more related ideas.