Skip to content
Snippets Groups Projects
Unverified Commit ae1bf8a4 authored by Oleg Babin's avatar Oleg Babin Committed by Alexander Turenko
Browse files

error: add __concat method to error object

Usually functions return pair `nil, err` and expected that err is string.
Let's make the behaviour of error object closer to string
and define __concat metamethod.

The case of error "error_mt.__concat(): neither of args is an error"
is not covered by tests because of #4723

Closes #4489

(cherry picked from commit 935db173)
parent b89c62bd
No related branches found
No related tags found
No related merge requests found
......@@ -150,9 +150,20 @@ local function error_index(err, key)
return error_methods[key]
end
local function error_concat(lhs, rhs)
if ffi.istype('struct error', lhs) then
return tostring(lhs) .. rhs
elseif ffi.istype('struct error', rhs) then
return lhs .. tostring(rhs)
else
error('error_mt.__concat(): neither of args is an error')
end
end
local error_mt = {
__index = error_index;
__tostring = error_message;
__concat = error_concat;
};
ffi.metatype('struct error', error_mt);
......
......@@ -196,6 +196,79 @@ box.error.new()
---
- error: 'Usage: box.error.new(code, args)'
...
--
-- gh-4489: box.error has __concat metamethod
--
test_run:cmd("push filter '(.builtin/.*.lua):[0-9]+' to '\\1'")
---
- true
...
e = box.error.new(box.error.UNKNOWN)
---
...
'left side: ' .. e
---
- 'left side: Unknown error'
...
e .. ': right side'
---
- 'Unknown error: right side'
...
e .. nil
---
- error: 'builtin/error.lua: attempt to concatenate local ''rhs'' (a nil value)'
...
nil .. e
---
- error: 'builtin/error.lua: attempt to concatenate local ''lhs'' (a nil value)'
...
e .. box.NULL
---
- error: 'builtin/error.lua: attempt to concatenate ''string'' and ''void *'''
...
box.NULL .. e
---
- error: 'builtin/error.lua: attempt to concatenate ''void *'' and ''string'''
...
123 .. e
---
- 123Unknown error
...
e .. 123
---
- Unknown error123
...
e .. e
---
- Unknown errorUnknown error
...
e .. {}
---
- error: 'builtin/error.lua: attempt to concatenate local ''rhs'' (a table value)'
...
{} .. e
---
- error: 'builtin/error.lua: attempt to concatenate local ''lhs'' (a table value)'
...
-1ULL .. e
---
- error: 'builtin/error.lua: attempt to concatenate ''uint64_t'' and ''string'''
...
e .. -1ULL
---
- error: 'builtin/error.lua: attempt to concatenate ''string'' and ''uint64_t'''
...
1LL .. e
---
- error: 'builtin/error.lua: attempt to concatenate ''int64_t'' and ''string'''
...
e .. 1LL
---
- error: 'builtin/error.lua: attempt to concatenate ''string'' and ''int64_t'''
...
e = nil
---
...
----------------
-- # box.stat
----------------
......@@ -1032,7 +1105,7 @@ error()
---
- error: null
...
-- A test case for bitwise operations
-- A test case for bitwise operations
bit.lshift(1, 32)
---
- 1
......
......@@ -59,6 +59,28 @@ e = box.error.new(box.error.CREATE_SPACE, "space", "error")
e
box.error.new()
--
-- gh-4489: box.error has __concat metamethod
--
test_run:cmd("push filter '(.builtin/.*.lua):[0-9]+' to '\\1'")
e = box.error.new(box.error.UNKNOWN)
'left side: ' .. e
e .. ': right side'
e .. nil
nil .. e
e .. box.NULL
box.NULL .. e
123 .. e
e .. 123
e .. e
e .. {}
{} .. e
-1ULL .. e
e .. -1ULL
1LL .. e
e .. 1LL
e = nil
----------------
-- # box.stat
----------------
......@@ -271,7 +293,7 @@ dostring('return abc')
dostring('return ...', 1, 2, 3)
-- A test case for Bug#1043804 lua error() -> server crash
error()
-- A test case for bitwise operations
-- A test case for bitwise operations
bit.lshift(1, 32)
bit.band(1, 3)
bit.bor(1, 2)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment