@@ -164,12 +164,30 @@ local function pcall_check_trace(level, fn, ...)
164
164
return ok , err
165
165
end
166
166
167
+ --
168
+ -- Wrapper around pcall that:
169
+ -- * checks the stack trace for box errors;
170
+ -- * unwraps the error if it was wrapped by luatest (e.g. by Server:exec());
171
+ -- * re-raises the error if it was raised by a luatest assertion failure.
172
+ --
173
+ local function pcall_wrapper (level , fn , ...)
174
+ local ok , err = pcall_check_trace (level + 1 , fn , ... )
175
+ if not ok and utils .is_luatest_error (err ) then
176
+ if err .status == ' error' then
177
+ err = err .message
178
+ else
179
+ error (err )
180
+ end
181
+ end
182
+ return ok , err
183
+ end
184
+
167
185
--- Check that calling fn raises an error.
168
186
--
169
187
-- @func fn
170
188
-- @param ... arguments for function
171
189
function M .assert_error (fn , ...)
172
- local ok , err = pcall_check_trace (2 , fn , ... )
190
+ local ok , err = pcall_wrapper (2 , fn , ... )
173
191
if ok then
174
192
failure (" Expected an error when calling function but no error generated" , nil , 2 )
175
193
end
@@ -565,7 +583,7 @@ function M.assert_str_matches(value, pattern, start, final, message)
565
583
end
566
584
567
585
local function _assert_error_msg_equals (stripFileAndLine , expectedMsg , func , ...)
568
- local no_error , error_msg = pcall_check_trace (3 , func , ... )
586
+ local no_error , error_msg = pcall_wrapper (3 , func , ... )
569
587
if no_error then
570
588
local failure_message = string.format (
571
589
' Function successfully returned: %s\n Expected error: %s' ,
631
649
-- @func fn
632
650
-- @param ... arguments for function
633
651
function M .assert_error_msg_contains (expected_partial , fn , ...)
634
- local no_error , error_msg = pcall_check_trace (2 , fn , ... )
652
+ local no_error , error_msg = pcall_wrapper (2 , fn , ... )
635
653
log .info (' Assert error message %s contains %s' , error_msg , expected_partial )
636
654
if no_error then
637
655
local failure_message = string.format (
654
672
-- @func fn
655
673
-- @param ... arguments for function
656
674
function M .assert_error_msg_matches (pattern , fn , ...)
657
- local no_error , error_msg = pcall_check_trace (2 , fn , ... )
675
+ local no_error , error_msg = pcall_wrapper (2 , fn , ... )
658
676
if no_error then
659
677
local failure_message = string.format (
660
678
' Function successfully returned: %s\n Expected error matching: %s' ,
694
712
-- @func fn
695
713
-- @param ... arguments for function
696
714
function M .assert_error_covers (expected , fn , ...)
697
- local ok , actual = pcall_check_trace (2 , fn , ... )
715
+ local ok , actual = pcall_wrapper (2 , fn , ... )
698
716
if ok then
699
717
fail_fmt (2 , nil ,
700
718
' Function successfully returned: %s\n Expected error: %s' ,
0 commit comments