* Nicer output from debug.traceback

For plugin authors to discuss plugin programming
Post Reply
avatar
shoshk
Superstar
Posts: 280
Joined: 13 May 2015 16:28
Family Historian: V7
Location: Mitzpe Jericho, Israel

Nicer output from debug.traceback

Post by shoshk »

I'm starting to implement error catching in my plugins, using xpcall and debug.traceback.

To start, I created a simple test plugin:

Code: Select all

require("iuplua")
iup.SetGlobal("CUSTOMQUITMESSAGE", "YES")

local function f ()
    return "a" + 2  -- will cause error
end -- f
local function err (x)
    local function dialogError(pText)
        local strText = pText
        local function setArg(txtText)
            strText = txtText
            return iup.CLOSE
        end -- local function setArg
        ----------------------------------------
        -- Define IUP controls for user dialogue
        ----------------------------------------
        local txtText = iup.text({
            RasterSize = "100x100",
            Expand = "Yes",
            MULTILINE = "Yes",
            Tip = "Error Message",
            value = strText,
        })
        local btnLink = iup.button({
            Title = "Close",
            Expand = "No",
            Tip = "Close",
            FgColor = "0 128 0",
            action = function()
                return setArg(txtText.Value)
            end,
        })
        local btnQuit = iup.button({
            Title = "Quit",
            Expand = "No",
            Tip = "Quit",
            FgColor = "255 0 0",
            action = function()
                return setArg(" ")
            end,
        })
        local dialog = iup.dialog({
            Title = "Error",
            iup.vbox({
                Expand = "Yes",
                iup.hbox({
                    Margin = "1x1",
                    Expand = "Yes",
                    txtText,
                }),
                iup.hbox({ Expand = "Yes", btnLink, btnQuit }),
            }),
            RasterSize = "800x200",
            MinSize = "800x200",
            Padding = "4x4",
            Gap = "9",
            Margin = "8x8",
            MinBox = "No",
            MaxBox = "No",
            DefaultEnter = btnLink,
            DefaultEsc = btnQuit,
            close_cb = function()
                return setArg(" ")
            end,
        })
    
        iup.Popup(dialog)
        iup.Destroy(dialog)
    
        return
    end
    --------------------------------------------------------------------------------
    local sTrace = debug.traceback()
    dialogError(x .. '\n' .. sTrace)
end
--------------------------------------------------------------------------------
local function main()
    f()
end
--------------------------------------------------------------------------------
xpcall(main, err)
Which, when run, produces the following output:

[string "C:\ProgramData\Calico Pie\Family Historian\Pl..."]:5: attempt to perform arithmetic on a string value
stack traceback:
[string "C:\ProgramData\Calico Pie\Family Historian\Pl..."]:73: in metamethod '__add'
[string "C:\ProgramData\Calico Pie\Family Historian\Pl..."]:5: in upvalue 'f'
[string "C:\ProgramData\Calico Pie\Family Historian\Pl..."]:81: in function <[string "C:\ProgramData\Calico Pie\Family Historian\Pl..."]:80>
[C]: in function 'xpcall'
[string "C:\ProgramData\Calico Pie\Family Historian\Pl..."]:84: in main chunk

My problem is that everything is being prefixed by the full path, which means that I don't see the actual function names. Is there some way to fix it?

Thanks,
Shosh
Shosh Kalson
User avatar
tatewise
Megastar
Posts: 28488
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Nicer output from debug.traceback

Post by tatewise »

That quoted text is simply the full path of your plugin that is running.
i.e. "C:\ProgramData\Calico Pie\Family Historian\Plugins\<your plugin name>.fh_lua"
It does not provide any function names.
All the information that is needed is the line number enclosed by colons.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
avatar
shoshk
Superstar
Posts: 280
Joined: 13 May 2015 16:28
Family Historian: V7
Location: Mitzpe Jericho, Israel

Re: Nicer output from debug.traceback

Post by shoshk »

Actually, since I use modules, I need the file names. But, after some searching, I found an example of writing a custom traceback function which shows non-truncated names. It uses debug.getinfo. I tweaked it a bit to get the formatting the way I wanted. It doesn’t have to be all that pretty. I mainly wanted a mechanism for my husband to send messages with details when things go wrong.
Last edited by shoshk on 29 Aug 2023 01:38, edited 1 time in total.
Shosh Kalson
User avatar
tatewise
Megastar
Posts: 28488
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Nicer output from debug.traceback

Post by tatewise »

OK, it never occurred to me that you might use modules as that is not feasible with published FH Plugins for other users.
However, I think you mean debug.getinfo(...) ?
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
avatar
shoshk
Superstar
Posts: 280
Joined: 13 May 2015 16:28
Family Historian: V7
Location: Mitzpe Jericho, Israel

Re: Nicer output from debug.traceback

Post by shoshk »

Yes, thank you for the correction. debug not dump

I have written a number of plugins which manage our data entry and sourcing process, with accompanying custom reports. The work which would be necessary to publish them is way beyond what I am able to invest. My husband is my single user, and he’s quite forgiving. He’d much rather that I spend time adding new features, rather than making things bullet proof and writing proper documentation.

BTW, I was nervous about using xpcall; it seemed very complicated when reading the documentation. But, in fact, the implementation is very easy. After writing my error handler, all I need to do is change my call to main() to xpcall(main, ErrorHandler) in the relevant plugins. Easy, peasy…
Shosh Kalson
Post Reply