From: Oleg Babin <olegrok@tarantool.org> To: tarantool-patches@dev.tarantool.org Cc: Oleg Babin <babinoleg@mail.ru> Subject: [Tarantool-patches] [PATCH v2] error: Add __concat method to error object Date: Mon, 11 Nov 2019 17:50:09 +0300 [thread overview] Message-ID: <20191111145009.91425-1-olegrok@tarantool.org> (raw) From: Oleg Babin <babinoleg@mail.ru> 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. Closes #4489 --- Changes in v2: - Added tests --- src/lua/error.lua | 8 +++++++ test/box/misc.result | 47 +++++++++++++++++++++++++++++++++++++++++- test/box/misc.test.lua | 17 ++++++++++++++- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/lua/error.lua b/src/lua/error.lua index 28fc0377d..b44fab7b6 100644 --- a/src/lua/error.lua +++ b/src/lua/error.lua @@ -150,9 +150,17 @@ local function error_index(err, key) return error_methods[key] end +local function error_concat(lhs, rhs) + if lhs == nil or rhs == nil then + error("attempt to concatenate struct error and nil") + end + return tostring(lhs) .. tostring(rhs) +end + local error_mt = { __index = error_index; __tostring = error_message; + __concat = error_concat; }; ffi.metatype('struct error', error_mt); diff --git a/test/box/misc.result b/test/box/misc.result index b2930515b..db184c037 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -196,6 +196,51 @@ 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 struct error and nil' +... +nil .. e +--- +- error: 'builtin/error.lua: attempt to concatenate struct error and nil' +... +e .. box.NULL +--- +- error: 'builtin/error.lua: attempt to concatenate struct error and nil' +... +box.NULL .. e +--- +- error: 'builtin/error.lua: attempt to concatenate struct error and nil' +... +123 .. e +--- +- 123Unknown error +... +e .. 123 +--- +- Unknown error123 +... +e = nil +--- +... ---------------- -- # box.stat ---------------- @@ -1037,7 +1082,7 @@ error() --- - error: null ... --- A test case for bitwise operations +-- A test case for bitwise operations bit.lshift(1, 32) --- - 1 diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua index cc223b2ef..6104b935b 100644 --- a/test/box/misc.test.lua +++ b/test/box/misc.test.lua @@ -59,6 +59,21 @@ 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 = nil + ---------------- -- # box.stat ---------------- @@ -271,7 +286,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) -- 2.23.0
next reply other threads:[~2019-11-11 14:50 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-11-11 14:50 Oleg Babin [this message] 2020-01-10 12:32 ` Alexander Turenko 2020-01-10 15:51 ` Oleg Babin 2020-01-10 14:51 ` Sergey Ostanevich
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20191111145009.91425-1-olegrok@tarantool.org \ --to=olegrok@tarantool.org \ --cc=babinoleg@mail.ru \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v2] error: Add __concat method to error object' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox