From 3dc644c91a0387a8d979bd56de761f27588f8ff7 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 23 Jun 2017 20:50:48 +0100 Subject: [PATCH] Pull out the error reason guessing code into a free function. --- src/kamokan_impl/CMakeLists.txt | 1 + src/kamokan_impl/general_pastie_response.cpp | 23 +---------- src/kamokan_impl/general_pastie_response.hpp | 2 - src/kamokan_impl/redis_to_error_reason.cpp | 42 ++++++++++++++++++++ src/kamokan_impl/redis_to_error_reason.hpp | 27 +++++++++++++ 5 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 src/kamokan_impl/redis_to_error_reason.cpp create mode 100644 src/kamokan_impl/redis_to_error_reason.hpp diff --git a/src/kamokan_impl/CMakeLists.txt b/src/kamokan_impl/CMakeLists.txt index adc743b..441052d 100644 --- a/src/kamokan_impl/CMakeLists.txt +++ b/src/kamokan_impl/CMakeLists.txt @@ -23,6 +23,7 @@ add_library(${PROJECT_NAME} STATIC edit_response.cpp general_pastie_response.cpp ${CMAKE_CURRENT_BINARY_DIR}/include/lua_scripts_for_redis.cpp + redis_to_error_reason.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/src/kamokan_impl/general_pastie_response.cpp b/src/kamokan_impl/general_pastie_response.cpp index 9791117..ed2cc57 100644 --- a/src/kamokan_impl/general_pastie_response.cpp +++ b/src/kamokan_impl/general_pastie_response.cpp @@ -19,6 +19,7 @@ #include "cgi_env.hpp" #include "settings_bag.hpp" #include "error_reasons.hpp" +#include "redis_to_error_reason.hpp" #include #include #include @@ -47,7 +48,7 @@ namespace kamokan { storage().retrieve_pastie(token, settings().as("max_token_length")); if (m_pastie_info.error) - return make_error_redirect(m_pastie_info); + return make_error_redirect(redis_to_error_reason(*m_pastie_info.error)); if (this->token_invalid()) { assert(this->pastie_not_found()); @@ -92,24 +93,4 @@ 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 f0bd1e3..2c1577a 100644 --- a/src/kamokan_impl/general_pastie_response.hpp +++ b/src/kamokan_impl/general_pastie_response.hpp @@ -39,8 +39,6 @@ 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/redis_to_error_reason.cpp b/src/kamokan_impl/redis_to_error_reason.cpp new file mode 100644 index 0000000..2614b6a --- /dev/null +++ b/src/kamokan_impl/redis_to_error_reason.cpp @@ -0,0 +1,42 @@ +/* Copyright 2017, Michele Santullo + * This file is part of "kamokan". + * + * "kamokan" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "kamokan" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "kamokan". If not, see . + */ + +#include "redis_to_error_reason.hpp" +#include "spdlog.hpp" + +namespace kamokan { + tawashi::ErrorReasons redis_to_error_reason (const redis::ErrorString& parError) { + return redis_to_error_reason(parError.message()); + } + + tawashi::ErrorReasons redis_to_error_reason (const std::string& parError) { + using tawashi::ErrorReasons; + + try { + return ErrorReasons::_from_string(parError.c_str()); + } + catch (const std::runtime_error& e) { + auto statuslog = spdlog::get("statuslog"); + statuslog->error( + "Unable to deduce error reason from the received string: \"{}\"; exception raised: \"{}\"", + parError, + e.what() + ); + return ErrorReasons::UnknownReason; + } + } +} //namespace kamokan diff --git a/src/kamokan_impl/redis_to_error_reason.hpp b/src/kamokan_impl/redis_to_error_reason.hpp new file mode 100644 index 0000000..2b39e0f --- /dev/null +++ b/src/kamokan_impl/redis_to_error_reason.hpp @@ -0,0 +1,27 @@ +/* Copyright 2017, Michele Santullo + * This file is part of "kamokan". + * + * "kamokan" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "kamokan" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "kamokan". If not, see . + */ + +#pragma once + +#include "error_reasons.hpp" +#include "incredis/incredis.hpp" +#include + +namespace kamokan { + tawashi::ErrorReasons redis_to_error_reason (const redis::ErrorString& parError); + tawashi::ErrorReasons redis_to_error_reason (const std::string& parError); +} //namespace kamokan