1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-12-27 21:35:41 +00:00

Organize exceptions so it's easy to log errors locally.

This commit is contained in:
King_DuckZ 2017-05-16 18:54:00 +01:00
parent 32eadfc64d
commit 10da16051c
7 changed files with 97 additions and 16 deletions

View file

@ -25,6 +25,7 @@ add_library(${PROJECT_NAME} STATIC
sanitized_utf8.cpp
tiger.c
error_response.cpp
tawashi_exception.cpp
)
target_include_directories(${PROJECT_NAME}

View file

@ -25,6 +25,7 @@ namespace tawashi {
PastieNotSaved,
UserFlooding,
UnkownReason,
RedisDisconnected
RedisDisconnected,
MissingPostVariable
)
} //namespace tawashi

View file

@ -49,7 +49,8 @@ namespace tawashi {
"Submitted pastie couldn't be saved.",
"The pastie was not saved because the client is submitting too many pasties too quickly. Please wait a bit longer and try again.",
"An unknown error was raised.",
"Unable to connect to Redis."
"Unable to connect to Redis.",
"Request is missing a POST variable."
};
constexpr const auto lengths = string_lengths(err_descs);
static_assert(err_descs.static_size == lengths.static_size, "Mismatching array sizes between strings and their lengths");

View file

@ -24,9 +24,8 @@
#include "duckhandy/compatibility.h"
#include "duckhandy/lexical_cast.hpp"
#include "duckhandy/int_to_string_ary.hpp"
#include "tawashi_exception.hpp"
#include <ciso646>
#include <sstream>
#include <stdexcept>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <cstdint>
@ -40,9 +39,14 @@ namespace tawashi {
const char g_language_key[] = "lang";
const char g_duration_key[] = "ttl";
class MissingPostVarError : public std::runtime_error {
class MissingPostVarError : public TawashiException {
public:
MissingPostVarError (const std::string& parMsg) : std::runtime_error(parMsg) {}
explicit MissingPostVarError(const boost::string_ref& parKey) :
TawashiException(
ErrorReasons::MissingPostVariable,
"Error retrieving POST variable \"" + std::string(parKey.begin(), parKey.end()) + "\""
)
{}
};
template <std::size_t N>
@ -57,11 +61,8 @@ namespace tawashi {
boost::string_ref get_value_from_post (const cgi::PostMapType& parPost, boost::string_ref parKey) {
std::string key(parKey.data(), parKey.size());
auto post_data_it = parPost.find(key);
if (parPost.end() == post_data_it) {
std::ostringstream oss;
oss << "can't find POST data field \"" << parKey << '"';
throw MissingPostVarError(oss.str());
}
if (parPost.end() == post_data_it)
throw MissingPostVarError(parKey);
return post_data_it->second;
}
@ -104,18 +105,24 @@ namespace tawashi {
boost::string_ref pastie;
boost::string_ref lang;
boost::string_ref duration;
auto statuslog = spdlog::get("statuslog");
assert(statuslog);
try {
pastie = get_value_from_post(post, make_string_ref(g_post_key));
}
catch (const MissingPostVarError& e) {
m_error_message = e.what();
catch (const TawashiException& e) {
statuslog->error(e.what());
error_redirect(500, e.reason());
return;
}
try {
lang = get_value_from_post(post, make_string_ref(g_language_key));
duration = get_value_from_post(post, make_string_ref(g_duration_key));
}
catch (const MissingPostVarError&) {
catch (const MissingPostVarError& e) {
statuslog->info(e.what());
}
const SettingsBag& settings = this->settings();
@ -145,6 +152,7 @@ namespace tawashi {
this->change_type(Response::Location, oss.str());
}
else {
error_redirect(500, ErrorReasons::PastieNotSaved);
return;
}
}

View file

@ -39,7 +39,5 @@ namespace tawashi {
virtual void on_process() override;
boost::optional<std::string> submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang);
void error_redirect (int parCode, ErrorReasons parReason);
std::string m_error_message;
};
} //namespace tawashi

View file

@ -0,0 +1,37 @@
/* Copyright 2017, Michele Santullo
* This file is part of "tawashi".
*
* "tawashi" 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.
*
* "tawashi" 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 "tawashi". If not, see <http://www.gnu.org/licenses/>.
*/
#include "tawashi_exception.hpp"
#include <sstream>
namespace tawashi {
namespace {
std::string compose_err_message (ErrorReasons parReason, const boost::string_ref& parMessage) {
std::ostringstream oss;
oss << "Exception with reason " << parReason << ": " << parMessage;
return oss.str();
}
} //unnamed namespace
TawashiException::TawashiException (ErrorReasons parReason, const boost::string_ref& parMessage) :
std::runtime_error(compose_err_message(parReason, parMessage)),
m_reason(parReason)
{
}
TawashiException::~TawashiException() noexcept = default;
} //namespace tawashi

View file

@ -0,0 +1,35 @@
/* Copyright 2017, Michele Santullo
* This file is part of "tawashi".
*
* "tawashi" 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.
*
* "tawashi" 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 "tawashi". If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "error_reasons.hpp"
#include <stdexcept>
#include <string>
#include <boost/utility/string_ref.hpp>
namespace tawashi {
class TawashiException : public std::runtime_error {
public:
TawashiException (ErrorReasons parReason, const boost::string_ref& parMessage);
~TawashiException() noexcept;
ErrorReasons reason() const { return m_reason; }
private:
ErrorReasons m_reason;
};
} //namespace tawashi