mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-08-07 12:59:45 +00:00
Organize exceptions so it's easy to log errors locally.
This commit is contained in:
parent
32eadfc64d
commit
10da16051c
7 changed files with 97 additions and 16 deletions
|
@ -25,6 +25,7 @@ add_library(${PROJECT_NAME} STATIC
|
||||||
sanitized_utf8.cpp
|
sanitized_utf8.cpp
|
||||||
tiger.c
|
tiger.c
|
||||||
error_response.cpp
|
error_response.cpp
|
||||||
|
tawashi_exception.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME}
|
target_include_directories(${PROJECT_NAME}
|
||||||
|
|
|
@ -25,6 +25,7 @@ namespace tawashi {
|
||||||
PastieNotSaved,
|
PastieNotSaved,
|
||||||
UserFlooding,
|
UserFlooding,
|
||||||
UnkownReason,
|
UnkownReason,
|
||||||
RedisDisconnected
|
RedisDisconnected,
|
||||||
|
MissingPostVariable
|
||||||
)
|
)
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -49,7 +49,8 @@ namespace tawashi {
|
||||||
"Submitted pastie couldn't be saved.",
|
"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.",
|
"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.",
|
"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);
|
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");
|
static_assert(err_descs.static_size == lengths.static_size, "Mismatching array sizes between strings and their lengths");
|
||||||
|
|
|
@ -24,9 +24,8 @@
|
||||||
#include "duckhandy/compatibility.h"
|
#include "duckhandy/compatibility.h"
|
||||||
#include "duckhandy/lexical_cast.hpp"
|
#include "duckhandy/lexical_cast.hpp"
|
||||||
#include "duckhandy/int_to_string_ary.hpp"
|
#include "duckhandy/int_to_string_ary.hpp"
|
||||||
|
#include "tawashi_exception.hpp"
|
||||||
#include <ciso646>
|
#include <ciso646>
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
@ -40,9 +39,14 @@ namespace tawashi {
|
||||||
const char g_language_key[] = "lang";
|
const char g_language_key[] = "lang";
|
||||||
const char g_duration_key[] = "ttl";
|
const char g_duration_key[] = "ttl";
|
||||||
|
|
||||||
class MissingPostVarError : public std::runtime_error {
|
class MissingPostVarError : public TawashiException {
|
||||||
public:
|
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>
|
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) {
|
boost::string_ref get_value_from_post (const cgi::PostMapType& parPost, boost::string_ref parKey) {
|
||||||
std::string key(parKey.data(), parKey.size());
|
std::string key(parKey.data(), parKey.size());
|
||||||
auto post_data_it = parPost.find(key);
|
auto post_data_it = parPost.find(key);
|
||||||
if (parPost.end() == post_data_it) {
|
if (parPost.end() == post_data_it)
|
||||||
std::ostringstream oss;
|
throw MissingPostVarError(parKey);
|
||||||
oss << "can't find POST data field \"" << parKey << '"';
|
|
||||||
throw MissingPostVarError(oss.str());
|
|
||||||
}
|
|
||||||
return post_data_it->second;
|
return post_data_it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,18 +105,24 @@ namespace tawashi {
|
||||||
boost::string_ref pastie;
|
boost::string_ref pastie;
|
||||||
boost::string_ref lang;
|
boost::string_ref lang;
|
||||||
boost::string_ref duration;
|
boost::string_ref duration;
|
||||||
|
|
||||||
|
auto statuslog = spdlog::get("statuslog");
|
||||||
|
assert(statuslog);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
pastie = get_value_from_post(post, make_string_ref(g_post_key));
|
pastie = get_value_from_post(post, make_string_ref(g_post_key));
|
||||||
}
|
}
|
||||||
catch (const MissingPostVarError& e) {
|
catch (const TawashiException& e) {
|
||||||
m_error_message = e.what();
|
statuslog->error(e.what());
|
||||||
|
error_redirect(500, e.reason());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
lang = get_value_from_post(post, make_string_ref(g_language_key));
|
lang = get_value_from_post(post, make_string_ref(g_language_key));
|
||||||
duration = get_value_from_post(post, make_string_ref(g_duration_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();
|
const SettingsBag& settings = this->settings();
|
||||||
|
@ -145,6 +152,7 @@ namespace tawashi {
|
||||||
this->change_type(Response::Location, oss.str());
|
this->change_type(Response::Location, oss.str());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
error_redirect(500, ErrorReasons::PastieNotSaved);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,5 @@ namespace tawashi {
|
||||||
virtual void on_process() override;
|
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);
|
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);
|
void error_redirect (int parCode, ErrorReasons parReason);
|
||||||
|
|
||||||
std::string m_error_message;
|
|
||||||
};
|
};
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
37
src/tawashi_implem/tawashi_exception.cpp
Normal file
37
src/tawashi_implem/tawashi_exception.cpp
Normal 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
|
35
src/tawashi_implem/tawashi_exception.hpp
Normal file
35
src/tawashi_implem/tawashi_exception.hpp
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue