From ecb44e725a5f5ec85ef9b718dc171ee9de1eeba6 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 23 Jun 2017 18:58:27 +0100 Subject: [PATCH] Report errors from lua into the response. --- src/kamokan_impl/general_pastie_response.cpp | 23 ++++++++++++++++++++ src/kamokan_impl/general_pastie_response.hpp | 2 ++ src/kamokan_impl/retrieve_pastie.lua | 4 ++++ src/kamokan_impl/storage.cpp | 9 ++++---- src/kamokan_impl/storage.hpp | 1 + 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/kamokan_impl/general_pastie_response.cpp b/src/kamokan_impl/general_pastie_response.cpp index b6204f8..9791117 100644 --- a/src/kamokan_impl/general_pastie_response.cpp +++ b/src/kamokan_impl/general_pastie_response.cpp @@ -46,6 +46,9 @@ namespace kamokan { m_pastie_info = storage().retrieve_pastie(token, settings().as("max_token_length")); + if (m_pastie_info.error) + return make_error_redirect(m_pastie_info); + if (this->token_invalid()) { assert(this->pastie_not_found()); return make_error_redirect(ErrorReasons::InvalidToken); @@ -89,4 +92,24 @@ namespace kamokan { else return std::string(); } + + tawashi::HttpHeader GeneralPastieResponse::make_error_redirect (const Storage::RetrievedPastie& parRetrievedInfo) { + using tawashi::ErrorReasons; + + assert(parRetrievedInfo.error); + const auto& err = *parRetrievedInfo.error; + + try { + return make_error_redirect(ErrorReasons::_from_string(err.c_str())); + } + catch (const std::runtime_error& e) { + auto statuslog = spdlog::get("statuslog"); + statuslog->error( + "Retrieved info generated an unexpected error: \"{}\"; exception raised: \"{}\"", + err, + e.what() + ); + return make_error_redirect(ErrorReasons::UnknownReason); + } + } } //namespace kamokan diff --git a/src/kamokan_impl/general_pastie_response.hpp b/src/kamokan_impl/general_pastie_response.hpp index 2c1577a..f0bd1e3 100644 --- a/src/kamokan_impl/general_pastie_response.hpp +++ b/src/kamokan_impl/general_pastie_response.hpp @@ -39,6 +39,8 @@ namespace kamokan { virtual tawashi::HttpHeader on_general_pastie_process() = 0; virtual void on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) = 0; std::string default_pastie_lang() override; + using Response::make_error_redirect; + tawashi::HttpHeader make_error_redirect (const Storage::RetrievedPastie& parRetrievedInfo); Storage::RetrievedPastie m_pastie_info; }; diff --git a/src/kamokan_impl/retrieve_pastie.lua b/src/kamokan_impl/retrieve_pastie.lua index de9ff4f..4d325fd 100644 --- a/src/kamokan_impl/retrieve_pastie.lua +++ b/src/kamokan_impl/retrieve_pastie.lua @@ -1,5 +1,9 @@ local token = KEYS[1] local result = redis.call("HMGET", token, "pastie", "selfdes", "lang") +if false == result[1] then + return redis.error_reply("PastieNotFound") +end + local selfdes = 0 local deleted = 0 if result[2] == 1 then diff --git a/src/kamokan_impl/storage.cpp b/src/kamokan_impl/storage.cpp index 19bf212..b557c32 100644 --- a/src/kamokan_impl/storage.cpp +++ b/src/kamokan_impl/storage.cpp @@ -170,14 +170,11 @@ namespace kamokan { } auto Storage::retrieve_pastie (const boost::string_view& parToken, uint32_t parMaxTokenLen) const -> RetrievedPastie { - using opt_string_list = redis::IncRedis::opt_string_list; - RetrievedPastie retval; retval.valid_token = is_valid_token(parToken, parMaxTokenLen); if (not retval.valid_token) return retval; - redis::Script retrieve = m_redis->command().make_script(boost::string_view(g_load_script, g_load_script_size - 1)); auto batch = m_redis->command().make_batch(); retrieve.run(batch, std::make_tuple(parToken), std::make_tuple()); @@ -185,7 +182,11 @@ namespace kamokan { if (raw_replies.empty()) return retval; - assert(not raw_replies.front().is_error()); + if (raw_replies.front().is_error()) { + retval.error = + boost::make_optional(get_error_string(raw_replies.front()).message()); + return retval; + } auto pastie_reply = get_array(raw_replies.front()); retval.pastie = get_string(pastie_reply[0]); diff --git a/src/kamokan_impl/storage.hpp b/src/kamokan_impl/storage.hpp index cd1ee2c..dadf905 100644 --- a/src/kamokan_impl/storage.hpp +++ b/src/kamokan_impl/storage.hpp @@ -46,6 +46,7 @@ namespace kamokan { boost::optional pastie; boost::optional lang; + boost::optional error; bool self_destructed; bool valid_token; };