I’m having difficulty with xpcall and hoping for some help. The fundamental problem is that xpcall calls the errorHandler() but does not pass to it the error message. So, to that extent, I’ve very much simplified my code in order to more easier diagnose the problem. This is the function to be wrapped by xpcall. Note that it specifically raises an error:
local function testXpCall(param1, param2 )
error("This is an error.")
return true
end
Here’s how I’m wrapping testXpCall with xpcall().
l
local status, result = xpcall( function() return testXpCall( param1, param2 ) end, utils.errorHandler )
Finally, here’s a simplified errorHandler:
function utils:errorHandler(errorMessage)
print( errorMessage )
return errorMessage
end
Here are the results of running the simplified code:
Error caught: nil
Status: false
Result: nil
Any insights would be very much appreciated.
Missed that real problem. Change:
function utils:errorHandler(errorMessage)
to:
function utils.errorHandler(errorMessage)
Thanks for looking at this. However, I am confused about how the ‘:’ syntax sneaked into the code I posted. So, just be clear the code with the ‘.’ syntax is what was posted in my request above. I ran it again, just to be sure.
So, my question still remains. It seems as thought the system isn’t passing the error message to the handler.
Thanks, again
You posted everything but the first snippet in blockquotes rather than code blocks. Edit your post and wrap you code in ```
1 Like
The : is in the function declaration (right below “Finally, here’s a simplified errorHandler:”), not the xpcall statement.
From the lua manual
The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self
. Thus, the statement
function t.a.b.c:f (params) body end
is syntactic sugar for
t.a.b.c.f = function (self, params) body end
In other words, using the : syntax here, the function “swallows” the first argument it references (in this cast the error message) as an implied “self” parameter (xpcall doesn’t understand that the function is a method of a table)
Some example code
local utils = {}
function utils.OnError(err)
return "|Cffff00ffError:|r " .. err -- returned if there is an error
end
local function testXpCall(param1, param2)
local causeError
print(param1, param2)
print(causeError.Now) -- raise an error causeError is nil not a table
return true
end
local status, result = xpcall(testXpCall, utils.OnError, "A", "B")
if not status then
print("Oops:", result)
end
1 Like