Skip to content

Commit

Permalink
Merge 257b9a9 into e4b4ba2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Aug 22, 2022
2 parents e4b4ba2 + 257b9a9 commit 1ba03df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion copas-cvs-6.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies = {
"luasocket >= 2.1, <= 3.0rc1-2",
"coxpcall >= 1.14",
"binaryheap >= 0.4",
"timerwheel >= 0.2",
"timerwheel ~> 1",
}
build = {
type = "builtin",
Expand Down
18 changes: 12 additions & 6 deletions docs/reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,19 @@ <h3>Copas dispatcher main functions</h3>
<code>copas.finished()</code>.
</dd>

<dt><strong><code>copas.setErrorHandler(func, [default])</code></strong></dt>
<dd>Sets the error handling function for the current thread. If <code>default</code>
is truthy, then the handler will become the new default, used for all threads
that do not have their own set.
Any errors will be forwarded to this handler, which will receive the
error, coroutine, and socket as arguments. See the Copas source code
<dt><strong><code>copas.setErrorHandler([func], [default])</code></strong></dt>
<dd>Sets the error handling function for the current thread.
Any errors will be forwarded to this handler, it will receive the
error, coroutine, and socket as arguments;
<code>function(err, co, skt)</code>. See the Copas source code
on how to deal with the arguments when implementing your own.

<br/>If <code>func</code>
is omitted, then the error handler is cleared (restores the default handler
for this coroutine).
<br/>If <code>default</code>
is truthy, then the handler will become the new default, used for all threads
that do not have their own set (in this case <code>func</code> must be provided).
</dd>

<dt><strong><code>copas.setsocketname(name, skt)</code></strong></dt>
Expand Down
12 changes: 10 additions & 2 deletions src/copas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,10 @@ end
local _errhandlers = setmetatable({}, { __mode = "k" }) -- error handler per coroutine

local function _deferror(msg, co, skt)
msg = ("%s (coroutine: %s, socket: %s)"):format(tostring(msg), object_names[co], object_names[skt])
local co_str = co == nil and "nil" or copas.getthreadname(co)
local skt_str = skt == nil and "nil" or copas.getsocketname(skt)
msg = ("%s (coroutine: %s, socket: %s)"):format(tostring(msg), co_str, skt_str)

if type(co) == "thread" then
-- regular Copas coroutine
msg = debug.traceback(co, msg)
Expand All @@ -974,7 +977,9 @@ local function _deferror(msg, co, skt)
end

function copas.setErrorHandler (err, default)
assert(err == nil or type(err) == "function", "Expected the handler to be a function, or nil")
if default then
assert(err ~= nil, "Expected the handler to be a function when setting the default")
_deferror = err
else
_errhandlers[coroutine_running()] = err
Expand Down Expand Up @@ -1022,7 +1027,10 @@ local function _doTick (co, skt, ...)
end

if not ok then
pcall(_errhandlers[co] or _deferror, res, co, skt)
local k, e = pcall(_errhandlers[co] or _deferror, res, co, skt)
if not k then
print("Failed executing error handler: " .. tostring(e))
end
end

local skt_to_close = _autoclose[co]
Expand Down

0 comments on commit 1ba03df

Please sign in to comment.