1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-12-23 21:25:43 +00:00

Pull out the error reason guessing code into a free function.

This commit is contained in:
King_DuckZ 2017-06-23 20:50:48 +01:00
parent 6589ddd86f
commit 3dc644c91a
5 changed files with 72 additions and 23 deletions

View file

@ -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}

View file

@ -19,6 +19,7 @@
#include "cgi_env.hpp"
#include "settings_bag.hpp"
#include "error_reasons.hpp"
#include "redis_to_error_reason.hpp"
#include <cassert>
#include <ciso646>
#include <utility>
@ -47,7 +48,7 @@ namespace kamokan {
storage().retrieve_pastie(token, settings().as<uint32_t>("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

View file

@ -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;
};

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "error_reasons.hpp"
#include "incredis/incredis.hpp"
#include <string>
namespace kamokan {
tawashi::ErrorReasons redis_to_error_reason (const redis::ErrorString& parError);
tawashi::ErrorReasons redis_to_error_reason (const std::string& parError);
} //namespace kamokan