* assert, pcall, xpcall

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
Ron Melby
Megastar
Posts: 917
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

assert, pcall, xpcall

Post by Ron Melby »

I am currently using this statement to load tables from disk. I find the error message from load* deplorable.
for my first test, I am using a table that does load with this statement, and it is trivial to make a table saved to a file that will not load, that is next.

_otbl, _err = assert(loadfile(_pth)())


I am trying to get pcall to work, or xpcall and have never gotten them to work without help, I simply do not understand the documentation, and useful examples are non-extant.
if I include the exec () I get true for the _otbl:

_otbl, _err = pcall(function() loadfile(_pth)() end)
if _err then
error(('File %s is corrupt. Cannot continue.'):format(_pth))
end

_otbl, _err = xpcall(function() loadfile(_pth)() end,
function(_err) -- err is the error message
return (error(('File %s is corrupt. Cannot continue.'):format(_pth)))
end)

if I do not include the exec () I get an empty _otbl, so it is not 'exec-ing':

_otbl, _err = pcall(function() loadfile(_pth) end)
if _err then
error(('File %s is corrupt. Cannot continue.'):format(_pth))
end

_otbl, _err = xpcall(function() loadfile(_pth) end,
function(_err) -- err is the error message
return (error(('File %s is corrupt. Cannot continue.'):format(_pth)))
end)

I must be missing something very fundamental here, I am just trying to make a better error statement.
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 28341
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: assert, pcall, xpcall

Post by tatewise »

There are several syntax errors in what you have posted, so they need fixing first.
  1. loadfile(_pth)() is an odd construction with trailing () so should be local _otbl, _err = loadfile(_pth)
  2. _otbl, _err = assert(loadfile(_pth)()) will fail for the same reason so try local _otbl, _err = assert(loadfile(_pth))
  3. _otbl, _err = pcall(function() loadfile(_pth)() end) is the wrong syntax.
    The first parameter f is the name of the function to call and the rest are its parameters.
    So try local _pc, _otbl, _err = pcall(loadfile,_pth)
However, I don't think loadfile() is designed to load a table but I am not sure.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 917
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: assert, pcall, xpcall

Post by Ron Melby »

this is what gets the job done, I now can know where tables on file goes wrong, and works well if tables are right.
now to revisit filesystemobject instead of rename.


_, _otbl, _err = pcall(loadfile, _pth)
if _err then
_err = _err:gsub(('%s:'):format(_pth or ''), '')
error(('File %s is corrupt. %s'):format(_pth, _err))
end
_otbl = _otbl()
FH V.6.2.7 Win 10 64 bit
Post Reply