|
3 | 3 | local utf8 = require("utf8") |
4 | 4 |
|
5 | 5 | local function error_printer(msg, layer) |
6 | | - print((debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", ""))) |
| 6 | + print((debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", ""))) |
7 | 7 | end |
8 | 8 |
|
9 | | -function errorhandler(msg) |
10 | | - msg = tostring(msg) |
11 | | - |
12 | | - error_printer(msg, 2) |
13 | | - |
14 | | - if not love.window or not love.graphics or not love.event then |
15 | | - return |
16 | | - end |
17 | | - |
18 | | - if not love.graphics.isCreated() or not love.window.isOpen() then |
19 | | - local success, status = pcall(love.window.setMode, 800, 600) |
20 | | - if not success or not status then |
21 | | - return |
22 | | - end |
23 | | - end |
24 | | - |
25 | | - -- Reset state. |
26 | | - if love.mouse then |
27 | | - love.mouse.setVisible(true) |
28 | | - love.mouse.setGrabbed(false) |
29 | | - love.mouse.setRelativeMode(false) |
30 | | - if love.mouse.isCursorSupported() then |
31 | | - love.mouse.setCursor() |
32 | | - end |
33 | | - end |
34 | | - if love.joystick then |
35 | | - -- Stop all joystick vibrations. |
36 | | - for i, v in ipairs(love.joystick.getJoysticks()) do |
37 | | - v:setVibration() |
38 | | - end |
39 | | - end |
40 | | - if love.audio then |
41 | | - love.audio.stop() |
42 | | - end |
43 | | - |
44 | | - love.graphics.reset() |
45 | | - local font = love.graphics.setNewFont(14) |
46 | | - |
47 | | - love.graphics.setColor(1, 1, 1) |
48 | | - |
49 | | - local trace = debug.traceback() |
50 | | - |
51 | | - love.graphics.origin() |
52 | | - |
53 | | - local sanitizedmsg = {} |
54 | | - for char in msg:gmatch(utf8.charpattern) do |
| 9 | +local function errorhandler(msg) |
| 10 | + msg = tostring(msg) |
| 11 | + |
| 12 | + error_printer(msg, 2) |
| 13 | + |
| 14 | + if not love.window or not love.graphics or not love.event then |
| 15 | + return |
| 16 | + end |
| 17 | + |
| 18 | + if not love.window.isOpen() then |
| 19 | + local success, status = pcall(love.window.setMode, 800, 600) |
| 20 | + if not success or not status then |
| 21 | + return |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + -- Reset state. |
| 26 | + if love.mouse then |
| 27 | + love.mouse.setVisible(true) |
| 28 | + love.mouse.setGrabbed(false) |
| 29 | + love.mouse.setRelativeMode(false) |
| 30 | + if love.mouse.isCursorSupported() then |
| 31 | + love.mouse.setCursor() |
| 32 | + end |
| 33 | + end |
| 34 | + if love.joystick then |
| 35 | + -- Stop all joystick vibrations. |
| 36 | + for _, v in ipairs(love.joystick.getJoysticks()) do |
| 37 | + v:setVibration() |
| 38 | + end |
| 39 | + end |
| 40 | + if love.audio then |
| 41 | + love.audio.stop() |
| 42 | + end |
| 43 | + |
| 44 | + love.graphics.reset() |
| 45 | + love.graphics.setNewFont(14) |
| 46 | + |
| 47 | + love.graphics.setColor(1, 1, 1) |
| 48 | + |
| 49 | + local trace = debug.traceback() |
| 50 | + |
| 51 | + love.graphics.origin() |
| 52 | + |
| 53 | + local sanitizedmsg = {} |
| 54 | + for char in msg:gmatch(utf8.charpattern) do |
55 | 55 | table.insert(sanitizedmsg.msg, char) |
56 | | - end |
| 56 | + end |
57 | 57 | local sanitized = table.concat(sanitizedmsg) |
58 | 58 |
|
59 | 59 | local err = {} |
60 | 60 |
|
61 | 61 | table.insert(err, "Error\n") |
62 | 62 | table.insert(err, sanitized) |
63 | 63 |
|
64 | | - if #sanitized ~= #msg then |
65 | | - table.insert(err, "Invalid UTF-8 string in error message.") |
66 | | - end |
67 | | - |
68 | | - table.insert(err, "\n") |
69 | | - |
70 | | - for l in trace:gmatch("(.-)\n") do |
71 | | - if not l:match("boot.lua") then |
72 | | - l = l:gsub("stack traceback:", "Traceback\n") |
73 | | - table.insert(err, l) |
74 | | - end |
75 | | - end |
76 | | - |
77 | | - local p = table.concat(err, "\n") |
78 | | - |
79 | | - p = p:gsub("\t", "") |
80 | | - p = p:gsub('%[string "(.-)"%]', "%1") |
81 | | - |
82 | | - local function draw() |
83 | | - if not love.graphics.isActive() then |
84 | | - return |
85 | | - end |
86 | | - local pos = 70 |
87 | | - love.graphics.clear(89 / 255, 157 / 255, 220 / 255) |
88 | | - love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos) |
89 | | - love.graphics.present() |
90 | | - end |
91 | | - |
92 | | - local fullErrorText = p |
93 | | - local function copyToClipboard() |
94 | | - if not love.system then |
95 | | - return |
96 | | - end |
97 | | - love.system.setClipboardText(fullErrorText) |
98 | | - p = p .. "\nCopied to clipboard!" |
99 | | - end |
100 | | - |
101 | | - if love.system then |
102 | | - p = p .. "\n\nPress Ctrl+C or tap to copy this error" |
103 | | - end |
104 | | - |
105 | | - return function() |
106 | | - love.event.pump() |
107 | | - |
108 | | - for e, a, b, c in love.event.poll() do |
109 | | - if e == "quit" then |
110 | | - return 1 |
111 | | - elseif e == "keypressed" and a == "escape" then |
112 | | - return 1 |
113 | | - elseif e == "keypressed" and a == "c" and love.keyboard.isDown("lctrl", "rctrl") then |
114 | | - copyToClipboard() |
115 | | - elseif e == "touchpressed" then |
116 | | - local name = love.window.getTitle() |
117 | | - if #name == 0 or name == "Untitled" then |
118 | | - name = "Game" |
119 | | - end |
120 | | - local buttons = { "OK", "Cancel" } |
121 | | - if love.system then |
122 | | - buttons[3] = "Copy to clipboard" |
123 | | - end |
124 | | - local pressed = love.window.showMessageBox("Quit " .. name .. "?", "", buttons) |
125 | | - if pressed == 1 then |
126 | | - return 1 |
127 | | - elseif pressed == 3 then |
128 | | - copyToClipboard() |
129 | | - end |
130 | | - end |
131 | | - end |
132 | | - |
133 | | - draw() |
134 | | - |
135 | | - if love.timer then |
136 | | - love.timer.sleep(0.1) |
137 | | - end |
138 | | - end |
| 64 | + if #sanitized ~= #msg then |
| 65 | + table.insert(err, "Invalid UTF-8 string in error message.") |
| 66 | + end |
| 67 | + |
| 68 | + table.insert(err, "\n") |
| 69 | + |
| 70 | + for l in trace:gmatch("(.-)\n") do |
| 71 | + if not l:match("boot.lua") then |
| 72 | + l = l:gsub("stack traceback:", "Traceback\n") |
| 73 | + table.insert(err, l) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + local p = table.concat(err, "\n") |
| 78 | + |
| 79 | + p = p:gsub("\t", "") |
| 80 | + p = p:gsub('%[string "(.-)"%]', "%1") |
| 81 | + |
| 82 | + local function draw() |
| 83 | + if not love.graphics.isActive() then |
| 84 | + return |
| 85 | + end |
| 86 | + local pos = 70 |
| 87 | + love.graphics.clear(89 / 255, 157 / 255, 220 / 255) |
| 88 | + love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos) |
| 89 | + love.graphics.present() |
| 90 | + end |
| 91 | + |
| 92 | + local fullErrorText = p |
| 93 | + local function copyToClipboard() |
| 94 | + if not love.system then |
| 95 | + return |
| 96 | + end |
| 97 | + love.system.setClipboardText(fullErrorText) |
| 98 | + p = p .. "\nCopied to clipboard!" |
| 99 | + end |
| 100 | + |
| 101 | + if love.system then |
| 102 | + p = p .. "\n\nPress Ctrl+C or tap to copy this error" |
| 103 | + end |
| 104 | + |
| 105 | + return function() |
| 106 | + love.event.pump() |
| 107 | + |
| 108 | + for e, a, _, _ in love.event.poll() do |
| 109 | + if e == "quit" then |
| 110 | + return 1 |
| 111 | + elseif e == "keypressed" and a == "escape" then |
| 112 | + return 1 |
| 113 | + elseif e == "keypressed" and a == "c" and love.keyboard.isDown("lctrl", "rctrl") then |
| 114 | + copyToClipboard() |
| 115 | + elseif e == "touchpressed" then |
| 116 | + local name = love.window.getTitle() |
| 117 | + if #name == 0 or name == "Untitled" then |
| 118 | + name = "Game" |
| 119 | + end |
| 120 | + local buttons = { "OK", "Cancel" } |
| 121 | + if love.system then |
| 122 | + buttons[3] = "Copy to clipboard" |
| 123 | + end |
| 124 | + local pressed = love.window.showMessageBox("Quit " .. name .. "?", "", buttons) |
| 125 | + if pressed == 1 then |
| 126 | + return 1 |
| 127 | + elseif pressed == 3 then |
| 128 | + copyToClipboard() |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + draw() |
| 134 | + |
| 135 | + if love.timer then |
| 136 | + love.timer.sleep(0.1) |
| 137 | + end |
| 138 | + end |
139 | 139 | end |
140 | 140 |
|
141 | 141 | return errorhandler |
0 commit comments