[tarantool-patches] [PATCH] error: Add __concat method to error object

  • From: Oleg Babin <olegrok@xxxxxxxxxxxxx>
  • To: tarantool-patches@xxxxxxxxxxxxx
  • Date: Tue, 22 Oct 2019 16:14:29 +0300

From: Oleg Babin <babinoleg@xxxxxxx>

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 tarantool/tarantool#4489
---
 src/lua/error.lua | 8 ++++++++
 1 file changed, 8 insertions(+)

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);
-- 
2.22.0


Other related posts: