Issues

Issue #702 new

Simplifying the error callback

hahawoo
created an issue

Since the code of the error callback will probably be read and edited by lovers, perhaps it could be good to make the code as simple as it can be.

I've kind of had a go at doing this, and uh, it's not perfect, or rigorously tested, or exactly the same as it was. I don't think setting the debug.traceback level to 2 affected anything when in boot.lua (I could certainly be mistaken), but it seemed to make the error message make more sense when love.errhand was in main.lua, so it could be useful to have for copy-and-pasting purposes if it doesn't change anything else.

function love.errhand(msg)
    msg = tostring(msg)

    local trace = ""

    for l in string.gmatch(debug.traceback("", 2), "(.-)\n") do
        if not string.match(l, "boot.lua") and l ~= "stack traceback:" and l ~= "" then
            trace = trace..l.."\n"
        end
    end

    trace = string.gsub(trace, "\n$", "")

    print("Error: "..msg.."\nTraceback:\n"..trace)

    if not love.window or not love.graphics or not love.event then
        return
    end

    if not love.graphics.isCreated() or not love.window.isCreated() then
        if not pcall(love.window.setMode, 800, 600) then
            return
        end
    end

    if love.audio then
        love.audio.stop()
    end

    love.graphics.reset()
    love.graphics.origin()
    love.graphics.setBackgroundColor(255, 157, 255)
    love.graphics.setNewFont(14)

    local output = "Error\n\n"..msg.."\n\n\n"..trace

    while true do
        love.event.pump()

        for e, a, b, c in love.event.poll() do
            if e == "quit" then
                return
            end
            if e == "keypressed" and a == "escape" then
                return
            end
        end

        love.graphics.clear()
        love.graphics.printf(output, 70, 70, love.graphics.getWidth() - 70)
        love.graphics.present()

        if love.timer then
            love.timer.sleep(0.1)
        end
    end
end

Comments (3)

  1. Alex Szpakowski

    Maybe we could have the default love.errhand call a simpler overridable function to draw the error message, but still keep love.errhand completely overridable for advanced use.

  2. hahawoo reporter

    Hmmm, I'm thinking it's not quite worth the complexity. While it would save copy-and-pasting a large function, it would also exclude doing things that you don't want to happen every frame, like saving to a file, loading resources, or printing to the console.

    I guess there are beginners who might find overiding the error handler confusing and this would make it easier for them, but I'm not sure how many of these people would be concerned about changing the error message just yet. And reading about the difference between love.errhand and love.errormessage might be confusing enough. :P

  3. Log in to comment