From cab46b4fef88da21bae12e8f17ab61af082b1fa3 Mon Sep 17 00:00:00 2001 From: Georgiy Lebedev Date: Wed, 21 Feb 2024 13:51:48 +0300 Subject: [PATCH 1/2] Change conversion of `box.error` to string in error message assertions Currently, error message assertions convert `box.error` to string using `tostring`. The string conversion metamethod of `box.error` is currently equivalent to the `message` field of `box.error`. In scope of tarantool/tarantool#9105, we are going to increase the verbosity of the string metamethod, and it will no longer be suitable for testing error messages. Let's replace `tostring(error)` conversions with `error.message` to retain the old behaviour. Needed for tarantool/tarantool#9105 --- luatest/assertions.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/luatest/assertions.lua b/luatest/assertions.lua index 6902133..8d61f9a 100644 --- a/luatest/assertions.lua +++ b/luatest/assertions.lua @@ -458,6 +458,18 @@ function M.assert_str_matches(value, pattern, start, final, message) end end +-- Convert an error object to an error message +-- @param err error object +-- @return error message +local function error_to_msg(err) + if type(err) == 'cdata' then + -- We assume that this is a `box.error` instance. + return err.message + else + return tostring(err) + end +end + local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...) local no_error, error_msg = pcall(func, ...) if no_error then @@ -467,8 +479,7 @@ local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ... failure(failure_message, nil, 3) end if type(expectedMsg) == "string" and type(error_msg) ~= "string" then - -- table are converted to string automatically - error_msg = tostring(error_msg) + error_msg = error_to_msg(error_msg) end local differ = false if stripFileAndLine then @@ -533,7 +544,7 @@ function M.assert_error_msg_contains(expected_partial, fn, ...) failure(failure_message, nil, 2) end if type(error_msg) ~= "string" then - error_msg = tostring(error_msg) + error_msg = error_to_msg(error_msg) end if not string.find(error_msg, expected_partial, nil, true) then error_msg, expected_partial = prettystr_pairs(error_msg, expected_partial) @@ -555,7 +566,7 @@ function M.assert_error_msg_matches(pattern, fn, ...) failure(failure_message, nil, 2) end if type(error_msg) ~= "string" then - error_msg = tostring(error_msg) + error_msg = error_to_msg(error_msg) end if not str_match(error_msg, pattern) then pattern, error_msg = prettystr_pairs(pattern, error_msg) From 000997260070b9916d2c698d254741135d3b30d1 Mon Sep 17 00:00:00 2001 From: Georgiy Lebedev Date: Fri, 23 Feb 2024 02:32:40 +0300 Subject: [PATCH 2/2] server: convert `box.error` thrown from net.box's eval to string net.box's `eval` method always throws a `box.error` in case of an error. When we decode the `eval` we result, in case of an error we currently rely on the fact that the string conversion metamethod returns the error's message. However, in scope of tarantool/tarantool#9105, we are going to increase the verbosity of the string conversion metamethod, and it will no longer return the error's message. Let's explicitly extract the error's message to retain the old behaviour. Needed for tarantool/tarantool#9105 --- luatest/server.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/luatest/server.lua b/luatest/server.lua index 5506871..8e59dc8 100644 --- a/luatest/server.lua +++ b/luatest/server.lua @@ -611,7 +611,9 @@ end local function exec_tail(ok, ...) if not ok then - local _ok, res = pcall(json.decode, tostring(...)) + local err = ... + -- net.box's eval method always throws a `box.error`. + local _ok, res = pcall(json.decode, err.message) error(_ok and res or ..., 0) else return ...