-
-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lua] Clean up uncaught error handling (#11082)
* [lua] Clean up uncaught error handling Removes usage of os.exit, which makes it safer and applicable both to lua scripts embedded in another application and standalone scripts. This removes the need for the lua-standalone flag. * [lua] Mark Exception.toString with @:keep If it is dce'd, we get a mess when lua prints an uncaught error * [lua] Prevent error handler polluting error stack * Add tests for #10979 * Prevent test failures on lua 5.1 Lua 5.1 has slightly different error messages and doesn't print custom error objects. * Minor fix
- Loading branch information
Showing
16 changed files
with
116 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
function _hx_handle_error(obj) | ||
if obj.value then | ||
_G.print("runtime error:\n " .. _hx_tostring(obj.value)); | ||
else | ||
_G.print("runtime error:\n " .. tostring(obj)); | ||
end | ||
|
||
if _G.debug and _G.debug.traceback then | ||
_G.print(_G.debug.traceback()); | ||
end | ||
_G.os.exit(1) | ||
local message = tostring(obj) | ||
if _G.debug and _G.debug.traceback then | ||
-- level 2 to skip _hx_handle_error | ||
message = _G.debug.traceback(message, 2) | ||
end | ||
return setmetatable({}, { __tostring = function() return message end }) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class HaxeException { | ||
static function main() { | ||
throw "Exception thrown from Haxe"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class NativeError { | ||
static function main() { | ||
final object:Dynamic = null; | ||
trace(object.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using StringTools; | ||
|
||
function isLua5_1() { | ||
final proc = new sys.io.Process("lua", ["-v"]); | ||
final out = proc.stderr.readLine(); | ||
proc.close(); | ||
return out.startsWith("Lua 5.1."); | ||
} | ||
|
||
function matchesExpectedMessage(actual:String) { | ||
// lua 5.1 doesn't print custom error objects | ||
if (actual == "(error object is not a string)") { | ||
return true; | ||
} | ||
return new EReg(Sys.args()[1], "").match(actual); | ||
} | ||
|
||
function main() { | ||
final proc = new sys.io.Process("lua", [Sys.args()[0]]); | ||
|
||
// ignore "lua: " | ||
final exceptionMessage = proc.stderr.readLine().substr(5); | ||
|
||
final hasExpectedMessage = matchesExpectedMessage(exceptionMessage); | ||
// we don't want a bare error without a stack trace | ||
final hasStackTrace = try { | ||
proc.stderr.readLine().contains('stack traceback'); | ||
} catch (_:haxe.io.Eof) { | ||
false; | ||
}; | ||
|
||
Sys.println('Error code: ${proc.exitCode()}'); | ||
Sys.println('Has expected exception message: ${hasExpectedMessage}'); | ||
// 5.1 interpreter doesn't handle custom objects | ||
Sys.println('Has call stack: ${hasStackTrace || isLua5_1()}'); | ||
|
||
proc.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--lua bin/native-error.lua | ||
--main NativeError | ||
|
||
--cmd lua runner.lua 'bin/native-error' '%./bin/native%-error%.lua:%d+: attempt to index .+' |
3 changes: 3 additions & 0 deletions
3
tests/misc/lua/projects/Issue10979/embedded-native.hxml.stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Success: false | ||
Has expected exception message: true | ||
Has call stack: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--lua bin/haxe-exception.lua | ||
--main HaxeException | ||
|
||
--cmd lua runner.lua 'bin/haxe-exception' 'Exception thrown from Haxe' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Success: false | ||
Has expected exception message: true | ||
Has call stack: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- error in loaded script should be caught here, instead of exiting everything | ||
local success, err = pcall(require, arg[1]) | ||
|
||
local exception_message = string.match(tostring(err), '^[^\n]+') | ||
local has_expected_message = string.match(exception_message, arg[2]) ~= nil | ||
local has_stack_trace = string.match(tostring(err), 'stack traceback') ~= nil | ||
|
||
print('Success: '..tostring(success)) | ||
print('Has expected exception message: '..tostring(has_expected_message)) | ||
print('Has call stack: '..tostring(has_stack_trace)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--lua bin/native-error.lua | ||
--main NativeError | ||
|
||
--next | ||
--run RunScript | ||
bin/native-error.lua | ||
bin/native-error\.lua:\d+: attempt to index .* |
3 changes: 3 additions & 0 deletions
3
tests/misc/lua/projects/Issue10979/standalone-native.hxml.stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Error code: 1 | ||
Has expected exception message: true | ||
Has call stack: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--lua bin/haxe-exception.lua | ||
--main HaxeException | ||
|
||
--next | ||
--run RunScript | ||
bin/haxe-exception.lua | ||
Exception thrown from Haxe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Error code: 1 | ||
Has expected exception message: true | ||
Has call stack: true |