Difference between revisions of "love.errhand"

m (The default function used if you don't supply your own.)
m (Deleted the unnecessary text.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{deprecatedin|[[0.11.0]]|110|type=function|text=This function has been renamed to [[love.errorhandler]]}}
+
{{deprecatedin|[[11.0]]|110|type=function|text=This function has been renamed to [[love.errorhandler]]}}
 
The error handler, used to display error messages.
 
The error handler, used to display error messages.
 
== Function ==
 
== Function ==
Line 14: Line 14:
 
=== The default function used if you don't supply your own. ===
 
=== The default function used if you don't supply your own. ===
 
<source lang="Lua">
 
<source lang="Lua">
local utf8 = require("utf8")
 
 
 
local function error_printer(msg, layer)
 
local function error_printer(msg, layer)
 
print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
 
print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
Line 41: Line 39:
 
love.mouse.setGrabbed(false)
 
love.mouse.setGrabbed(false)
 
love.mouse.setRelativeMode(false)
 
love.mouse.setRelativeMode(false)
if love.mouse.isCursorSupported() then
 
love.mouse.setCursor()
 
end
 
 
end
 
end
 
if love.joystick then
 
if love.joystick then
Line 52: Line 47:
 
end
 
end
 
if love.audio then love.audio.stop() end
 
if love.audio then love.audio.stop() end
 
 
love.graphics.reset()
 
love.graphics.reset()
local font = love.graphics.setNewFont(14)
+
local font = love.graphics.setNewFont(math.floor(love.window.toPixels(14)))
  
love.graphics.setColor(1, 1, 1, 1)
+
love.graphics.setBackgroundColor(89, 157, 220)
 +
love.graphics.setColor(255, 255, 255, 255)
  
 
local trace = debug.traceback()
 
local trace = debug.traceback()
  
 +
love.graphics.clear(love.graphics.getBackgroundColor())
 
love.graphics.origin()
 
love.graphics.origin()
 
local sanitizedmsg = {}
 
for char in msg:gmatch(utf8.charpattern) do
 
table.insert(sanitizedmsg, char)
 
end
 
sanitizedmsg = table.concat(sanitizedmsg)
 
  
 
local err = {}
 
local err = {}
  
 
table.insert(err, "Error\n")
 
table.insert(err, "Error\n")
table.insert(err, sanitizedmsg)
+
table.insert(err, msg.."\n\n")
  
if #sanitizedmsg ~= #msg then
+
for l in string.gmatch(trace, "(.-)\n") do
table.insert(err, "Invalid UTF-8 string in error message.")
+
if not string.match(l, "boot.lua") then
end
+
l = string.gsub(l, "stack traceback:", "Traceback\n")
 
 
table.insert(err, "\n")
 
 
 
for l in trace:gmatch("(.-)\n") do
 
if not l:match("boot.lua") then
 
l = l:gsub("stack traceback:", "Traceback\n")
 
 
table.insert(err, l)
 
table.insert(err, l)
 
end
 
end
Line 88: Line 72:
 
local p = table.concat(err, "\n")
 
local p = table.concat(err, "\n")
  
p = p:gsub("\t", "")
+
p = string.gsub(p, "\t", "")
p = p:gsub("%[string \"(.-)\"%]", "%1")
+
p = string.gsub(p, "%[string \"(.-)\"%]", "%1")
  
 
local function draw()
 
local function draw()
local pos = 70
+
local pos = love.window.toPixels(70)
love.graphics.clear(89/255, 157/255, 220/255)
+
love.graphics.clear(love.graphics.getBackgroundColor())
 
love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos)
 
love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos)
 
love.graphics.present()
 
love.graphics.present()
 
end
 
end
  
local fullErrorText = p
+
while true do
local function copyToClipboard()
 
if not love.system then return end
 
love.system.setClipboardText(fullErrorText)
 
p = p .. "\nCopied to clipboard!"
 
draw()
 
end
 
 
 
if love.system then
 
p = p .. "\n\nPress Ctrl+C or tap to copy this error"
 
end
 
 
 
return function()
 
 
love.event.pump()
 
love.event.pump()
  
 
for e, a, b, c in love.event.poll() do
 
for e, a, b, c in love.event.poll() do
 
if e == "quit" then
 
if e == "quit" then
return 1
+
return
 
elseif e == "keypressed" and a == "escape" then
 
elseif e == "keypressed" and a == "escape" then
return 1
+
return
elseif e == "keypressed" and a == "c" and love.keyboard.isDown("lctrl", "rctrl") then
 
copyToClipboard()
 
 
elseif e == "touchpressed" then
 
elseif e == "touchpressed" then
 
local name = love.window.getTitle()
 
local name = love.window.getTitle()
 
if #name == 0 or name == "Untitled" then name = "Game" end
 
if #name == 0 or name == "Untitled" then name = "Game" end
 
local buttons = {"OK", "Cancel"}
 
local buttons = {"OK", "Cancel"}
if love.system then
 
buttons[3] = "Copy to clipboard"
 
end
 
 
local pressed = love.window.showMessageBox("Quit "..name.."?", "", buttons)
 
local pressed = love.window.showMessageBox("Quit "..name.."?", "", buttons)
 
if pressed == 1 then
 
if pressed == 1 then
return 1
+
return
elseif pressed == 3 then
 
copyToClipboard()
 
 
end
 
end
 
end
 
end
Line 148: Line 113:
 
----
 
----
 
{{newinoldin|[[0.9.2]]|092|[[0.10.0]]|100|type=variant}}
 
{{newinoldin|[[0.9.2]]|092|[[0.10.0]]|100|type=variant}}
 
 
=== The default function used if you don't supply your own. ===
 
=== The default function used if you don't supply your own. ===
 
<source lang="Lua">
 
<source lang="Lua">
Line 334: Line 298:
 
</source>
 
</source>
 
----
 
----
{{oldin|[[0.9.0]]|090|type=variant|text=This variant is not used in that and later versions.}}
+
{{oldin|[[0.9.0]]|090|type=variant}}
 
=== The default function used if you don't supply your own. ===
 
=== The default function used if you don't supply your own. ===
 
<source lang="lua">
 
<source lang="lua">

Latest revision as of 09:59, 14 December 2019

Deprecated in LÖVE 11.0
This function has been renamed to love.errorhandler.

The error handler, used to display error messages.

Function

Synopsis

love.errhand( msg )

Arguments

string msg
The error message.

Returns

Nothing.

Examples

Available since LÖVE 0.10.0
This variant is not supported in earlier versions.

The default function used if you don't supply your own.

local function error_printer(msg, layer)
	print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
end

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

	error_printer(msg, 2)

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

	if not love.graphics.isCreated() or not love.window.isOpen() then
		local success, status = pcall(love.window.setMode, 800, 600)
		if not success or not status then
			return
		end
	end

	-- Reset state.
	if love.mouse then
		love.mouse.setVisible(true)
		love.mouse.setGrabbed(false)
		love.mouse.setRelativeMode(false)
	end
	if love.joystick then
		-- Stop all joystick vibrations.
		for i,v in ipairs(love.joystick.getJoysticks()) do
			v:setVibration()
		end
	end
	if love.audio then love.audio.stop() end
	love.graphics.reset()
	local font = love.graphics.setNewFont(math.floor(love.window.toPixels(14)))

	love.graphics.setBackgroundColor(89, 157, 220)
	love.graphics.setColor(255, 255, 255, 255)

	local trace = debug.traceback()

	love.graphics.clear(love.graphics.getBackgroundColor())
	love.graphics.origin()

	local err = {}

	table.insert(err, "Error\n")
	table.insert(err, msg.."\n\n")

	for l in string.gmatch(trace, "(.-)\n") do
		if not string.match(l, "boot.lua") then
			l = string.gsub(l, "stack traceback:", "Traceback\n")
			table.insert(err, l)
		end
	end

	local p = table.concat(err, "\n")

	p = string.gsub(p, "\t", "")
	p = string.gsub(p, "%[string \"(.-)\"%]", "%1")

	local function draw()
		local pos = love.window.toPixels(70)
		love.graphics.clear(love.graphics.getBackgroundColor())
		love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos)
		love.graphics.present()
	end

	while true do
		love.event.pump()

		for e, a, b, c in love.event.poll() do
			if e == "quit" then
				return
			elseif e == "keypressed" and a == "escape" then
				return
			elseif e == "touchpressed" then
				local name = love.window.getTitle()
				if #name == 0 or name == "Untitled" then name = "Game" end
				local buttons = {"OK", "Cancel"}
				local pressed = love.window.showMessageBox("Quit "..name.."?", "", buttons)
				if pressed == 1 then
					return
				end
			end
		end

		draw()

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

end

Available since LÖVE 0.9.2 and removed in LÖVE 0.10.0
This variant is not supported in earlier or later versions.

The default function used if you don't supply your own.

local function error_printer(msg, layer)
	print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
end

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

	error_printer(msg, 2)

	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
		local success, status = pcall(love.window.setMode, 800, 600)
		if not success or not status then
			return
		end
	end

	-- Reset state.
	if love.mouse then
		love.mouse.setVisible(true)
		love.mouse.setGrabbed(false)
	end
	if love.joystick then
		-- Stop all joystick vibrations.
		for i,v in ipairs(love.joystick.getJoysticks()) do
			v:setVibration()
		end
	end
	if love.audio then love.audio.stop() end
	love.graphics.reset()
	local font = love.graphics.setNewFont(math.floor(love.window.toPixels(14)))

	local sRGB = select(3, love.window.getMode()).srgb
	if sRGB and love.math then
		love.graphics.setBackgroundColor(love.math.gammaToLinear(89, 157, 220))
	else
		love.graphics.setBackgroundColor(89, 157, 220)
	end

	love.graphics.setColor(255, 255, 255, 255)

	local trace = debug.traceback()

	love.graphics.clear()
	love.graphics.origin()

	local err = {}

	table.insert(err, "Error\n")
	table.insert(err, msg.."\n\n")

	for l in string.gmatch(trace, "(.-)\n") do
		if not string.match(l, "boot.lua") then
			l = string.gsub(l, "stack traceback:", "Traceback\n")
			table.insert(err, l)
		end
	end

	local p = table.concat(err, "\n")

	p = string.gsub(p, "\t", "")
	p = string.gsub(p, "%[string \"(.-)\"%]", "%1")

	local function draw()
		local pos = love.window.toPixels(70)
		love.graphics.clear()
		love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos)
		love.graphics.present()
	end

	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

		draw()

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

Available since LÖVE 0.9.0 and removed in LÖVE 0.9.2
This variant is not supported in earlier or later versions.

The default function used if you don't supply your own.

local function error_printer(msg, layer)
	print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
end

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

	error_printer(msg, 2)

	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

	-- Reset state.
	if love.mouse then
		love.mouse.setVisible(true)
		love.mouse.setGrabbed(false)
	end
	if love.joystick then
		for i,v in ipairs(love.joystick.getJoysticks()) do
			v:setVibration() -- Stop all joystick vibrations.
		end
	end
	if love.audio then love.audio.stop() end
	love.graphics.reset()
	love.graphics.setBackgroundColor(89, 157, 220)
	local font = love.graphics.setNewFont(14)

	love.graphics.setColor(255, 255, 255, 255)

	local trace = debug.traceback()

	love.graphics.clear()
	love.graphics.origin()

	local err = {}

	table.insert(err, "Error\n")
	table.insert(err, msg.."\n\n")

	for l in string.gmatch(trace, "(.-)\n") do
		if not string.match(l, "boot.lua") then
			l = string.gsub(l, "stack traceback:", "Traceback\n")
			table.insert(err, l)
		end
	end

	local p = table.concat(err, "\n")

	p = string.gsub(p, "\t", "")
	p = string.gsub(p, "%[string \"(.-)\"%]", "%1")

	local function draw()
		love.graphics.clear()
		love.graphics.printf(p, 70, 70, love.graphics.getWidth() - 70)
		love.graphics.present()
	end

	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

		draw()

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

end

Removed in LÖVE 0.9.0
This variant is not supported in that and later versions.

The default function used if you don't supply your own.

local function error_printer(msg, layer)
	print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
end

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

	error_printer(msg, 2)

	if not love.graphics or not love.event or not love.graphics.isCreated() then
		return
	end

	-- Load.
	if love.audio then love.audio.stop() end
	love.graphics.reset()
	love.graphics.setBackgroundColor(89, 157, 220)
	local font = love.graphics.newFont(14)
	love.graphics.setFont(font)

	love.graphics.setColor(255, 255, 255, 255)

	local trace = debug.traceback()

	love.graphics.clear()

	local err = {}

	table.insert(err, "Error\n")
	table.insert(err, msg.."\n\n")

	for l in string.gmatch(trace, "(.-)\n") do
		if not string.match(l, "boot.lua") then
			l = string.gsub(l, "stack traceback:", "Traceback\n")
			table.insert(err, l)
		end
	end

	local p = table.concat(err, "\n")

	p = string.gsub(p, "\t", "")
	p = string.gsub(p, "%[string \"(.-)\"%]", "%1")

	local function draw()
		love.graphics.clear()
		love.graphics.printf(p, 70, 70, love.graphics.getWidth() - 70)
		love.graphics.present()
	end

	draw()

	local e, a, b, c
	while true do
		e, a, b, c = love.event.wait()

		if e == "quit" then
			return
		end
		if e == "keypressed" and a == "escape" then
			return
		end

		draw()

	end

end

See Also


Other Languages