From 1a9422af7d4f5a9302cb314c0efa49d0aa539336 Mon Sep 17 00:00:00 2001 From: Rahix Date: Tue, 16 Apr 2019 22:49:20 +0200 Subject: [PATCH] Better errors and return codes Signed-off-by: Rahix --- README.md | 7 ++++++- bc.lua | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1aa60759c230..25ca0d788412 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,9 @@ print("Value: ", bc:get("some_noun")) ### `BaseControl:call(verb, ...)` Call a verb **asynchroneously**. All parameters following `verb` will be given to the remote function. There is no guarantee that the verb was actually run. +`call` will return `true` if an attempt was made to run the verb and `false` +otherwise. `call` specifically does **not** error, because no guarantees +could be made anyway. _Note_: For local verbs, the call will still be synchroneous. ```lua @@ -146,8 +149,10 @@ bc:call("some_verb", "param_a_value", "param_b_value") ### `BaseControl:call_sync(verb, timeout, ...)` Call a verb **synchroneously**. All parameters following `verb` will be given to the remote function. `call_sync` will return the remote function's return -value. If timeout is not `nil` or `0`, `call_sync` will return early upon +value. If timeout is not `nil` or `0`, `call_sync` will error upon the timeout expiring. + +_Note_: For local verbs, the timeout can not be adhered to. ```lua local bc = require("bc") bc:register("stupid_add", function(a, b) diff --git a/bc.lua b/bc.lua index 357b1fa69976..180b9a5a40a8 100644 --- a/bc.lua +++ b/bc.lua @@ -245,7 +245,7 @@ function BaseControl:get(noun, timeout) error("timeout") end else - return error("unknown") + return error("unknown noun \""..noun.."\"") end end @@ -289,12 +289,16 @@ end function BaseControl:call(verb, ...) if self.local_verbs[verb] ~= nil then self.local_verbs[verb](...) + return true elseif self.remote_verbs[verb] ~= nil then self.network:send(self.remote_verbs[verb], { ty=Message.VerbRequest, verb=verb, param={...}, }) + return true + else + return false end end @@ -320,7 +324,7 @@ function BaseControl:call_sync(verb, ...) error("timeout") end else - error("unknown") + error("unknown verb \""..verb.."\"") end end -- }}}