diff --git a/luatest/capture.lua b/luatest/capture.lua index 7899b94..416fb6a 100644 --- a/luatest/capture.lua +++ b/luatest/capture.lua @@ -137,7 +137,7 @@ function Capture.mt:wrap(enabled, fn) return fn() end, function(err) -- Don't re-wrap error. - if err.type ~= self.class.CAPTURED_ERROR_TYPE then + if type(err) ~= 'table' or rawget(err, 'type') ~= self.class.CAPTURED_ERROR_TYPE then err = { type = self.class.CAPTURED_ERROR_TYPE, original = err, diff --git a/test/capture_test.lua b/test/capture_test.lua index e205a08..394ad7c 100644 --- a/test/capture_test.lua +++ b/test/capture_test.lua @@ -111,6 +111,36 @@ g.test_wrap_with_error = function() t.assert_equals(capture:flush(), {stdout = '', stderr = ''}) end +g.test_wrap_with_boolean_error = function() + local test_capture = Capture:new() + assert(not test_capture.enabled) + local ok, err = pcall(function() test_capture:wrap(true, function() + assert(test_capture.enabled) + error(true) + end) end) + t.assert_equals(ok, false) + assert(not test_capture.enabled) + t.assert_is(err.original, true) + t.assert_str_contains(err.traceback, "true") + t.assert_equals(test_capture:flush(), {stdout = '', stderr = ''}) + t.assert_equals(capture:flush(), {stdout = '', stderr = ''}) +end + +g.test_wrap_with_errorable__index_error = function() + local test_capture = Capture:new() + assert(not test_capture.enabled) + local ok, err = pcall(function() test_capture:wrap(true, function() + assert(test_capture.enabled) + error(setmetatable({"error"}, {__index = error})) + end) end) + t.assert_equals(ok, false) + assert(not test_capture.enabled) + t.assert_is(err.original[1], "error") + t.assert_str_contains(err.traceback, "error") + t.assert_equals(test_capture:flush(), {stdout = '', stderr = ''}) + t.assert_equals(capture:flush(), {stdout = '', stderr = ''}) +end + g.test_wrap_disabled_with_error = function() local test_capture = Capture:new() assert(not test_capture.enabled)