mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-01-27 23:22:51 +01:00
Support table errors in socket.newtry/protect
Instead of simply wrapping errors in a table in newtry and considering all tables exceptions in protect, add a metatable to exception wrapper and check that. This allows using protect with functions that may throw error objects. Additionally, assign __tostring metamethod to the exception metatable so that unhandled exceptions can be viewed normally.
This commit is contained in:
parent
4e83ba271a
commit
4adfd7a501
@ -28,6 +28,12 @@
|
||||
local base = _G
|
||||
local _M = {}
|
||||
|
||||
local exception_metat = {}
|
||||
|
||||
function exception_metat:__tostring()
|
||||
return base.tostring(self[1])
|
||||
end
|
||||
|
||||
local function do_nothing() end
|
||||
|
||||
function _M.newtry(finalizer)
|
||||
@ -38,7 +44,7 @@ function _M.newtry(finalizer)
|
||||
return ...
|
||||
else
|
||||
base.pcall(finalizer)
|
||||
base.error({err})
|
||||
base.error(base.setmetatable({err}, exception_metat))
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -48,7 +54,7 @@ local function handle_pcall_returns(ok, ...)
|
||||
return ...
|
||||
else
|
||||
local err = ...
|
||||
if base.type(err) == "table" then
|
||||
if base.getmetatable(err) == exception_metat then
|
||||
return nil, err[1]
|
||||
else
|
||||
base.error(err, 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user