From 0a63a8488d7e2ca3c190d8a02728635981829d27 Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Thu, 13 Oct 2022 14:13:26 +0300
Subject: [PATCH] fix(tlua): returning Err from rust callback now throws a lua
 error

---
 CHANGELOG.md                      |  2 ++
 tests/src/tlua/functions_write.rs | 10 ++--------
 tlua/src/functions_write.rs       |  3 +--
 3 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 041816ef..8c75dddc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@
 - `Error::Decode` now contains expected rust type and actual incorrect msgpack
     contents.
 - `sql::Statement::execute` now returns `Error::DecodeRmpValue`.
+- Returning `Result::Err(msg)` from a rust callback now throws a lua error
+    instead of returning `nil, msg` as it was before.
 
 # [0.6.4] Oct ?? 2022
 
diff --git a/tests/src/tlua/functions_write.rs b/tests/src/tlua/functions_write.rs
index ed71a166..0410d508 100644
--- a/tests/src/tlua/functions_write.rs
+++ b/tests/src/tlua/functions_write.rs
@@ -75,14 +75,8 @@ pub fn return_result() {
     let f: function![() -> Result<i32, &'static str>] = function0(always_fails);
     lua.set("always_fails", &f);
 
-    match lua.exec(r#"
-        local res, err = always_fails();
-        assert(res == nil);
-        assert(err == "oops, problem");
-    "#) {
-        Ok(()) => {}
-        Err(e) => panic!("{:?}", e),
-    }
+    let msg = lua.exec("always_fails()").unwrap_err().to_string();
+    assert_eq!(msg, r#"Execution error: [string "chunk"]:1: oops, problem"#);
 }
 
 pub fn closures() {
diff --git a/tlua/src/functions_write.rs b/tlua/src/functions_write.rs
index b83736b0..52e15358 100644
--- a/tlua/src/functions_write.rs
+++ b/tlua/src/functions_write.rs
@@ -2,7 +2,6 @@ use crate::{
     ffi,
     AsLua,
     error,
-    Nil,
     LuaError,
     LuaRead,
     LuaState,
@@ -344,7 +343,7 @@ where
     {
         match self {
             Ok(val) => val.push_into_lua(lua),
-            Err(val) => Ok(lua.push(&(Nil, val.to_string()))),
+            Err(err) => error!(lua, "{}", err),
         }
     }
 }
-- 
GitLab