mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2024-12-27 21:35:41 +00:00
Add http status codes and save dummy error into the mstch context.
This commit is contained in:
parent
4d31323bb1
commit
ec80734625
4 changed files with 29 additions and 9 deletions
|
@ -17,10 +17,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "enum.h"
|
||||
|
||||
namespace tawashi {
|
||||
enum ErrorReasons : int {
|
||||
BETTER_ENUM(ErrorReasons, int,
|
||||
PostLengthNotInRange,
|
||||
PastieNotSaved,
|
||||
UserFlooding
|
||||
};
|
||||
UserFlooding,
|
||||
UnkownReason,
|
||||
RedisDisconnected
|
||||
)
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -16,7 +16,12 @@
|
|||
*/
|
||||
|
||||
#include "error_response.hpp"
|
||||
#include "error_reasons.hpp"
|
||||
#include "cgi_env.hpp"
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <ciso646>
|
||||
#include <string>
|
||||
|
||||
namespace tawashi {
|
||||
ErrorResponse::ErrorResponse (
|
||||
|
@ -29,6 +34,15 @@ namespace tawashi {
|
|||
}
|
||||
|
||||
void ErrorResponse::on_mustache_prepare (mstch::map& parContext) {
|
||||
auto get = cgi_env().query_string_split();
|
||||
auto err_code = boost::lexical_cast<int>(get["code"]);
|
||||
const int reason_int = boost::lexical_cast<int>(get["reason"]);
|
||||
ErrorReasons reason_code(ErrorReasons::UnkownReason);
|
||||
if (reason_int >= 0 and reason_int < ErrorReasons::_size())
|
||||
reason_code = ErrorReasons::_from_integral(reason_int);
|
||||
|
||||
parContext["error_message"] = "No error";
|
||||
parContext["error_code"] = std::to_string(err_code);
|
||||
parContext["error_id"] = std::to_string(reason_code);
|
||||
}
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -43,9 +43,9 @@ namespace tawashi {
|
|||
}
|
||||
|
||||
void PastieResponse::on_process() {
|
||||
auto env = cgi_env().query_string_split();
|
||||
auto get = cgi_env().query_string_split();
|
||||
const std::string& query_str(cgi_env().query_string());
|
||||
if (env["m"] == "plain" or query_str.empty()) {
|
||||
if (get["m"] == "plain" or query_str.empty()) {
|
||||
this->change_type(Response::ContentType, "text/plain; charset=utf-8");
|
||||
m_plain_text = true;
|
||||
}
|
||||
|
|
|
@ -127,7 +127,7 @@ namespace tawashi {
|
|||
pastie = pastie.substr(0, max_sz);
|
||||
}
|
||||
else {
|
||||
error_redirect(1, ErrorReasons::PostLengthNotInRange);
|
||||
error_redirect(431, ErrorReasons::PostLengthNotInRange);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -151,13 +151,15 @@ namespace tawashi {
|
|||
|
||||
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) {
|
||||
auto& redis = this->redis();
|
||||
if (not redis.is_connected())
|
||||
if (not redis.is_connected()) {
|
||||
error_redirect(503, ErrorReasons::RedisDisconnected);
|
||||
return boost::optional<std::string>();
|
||||
}
|
||||
|
||||
std::string ip_hash = hashed_ip(cgi_env().remote_addr());
|
||||
if (redis.get(ip_hash)) {
|
||||
//please wait and submit again
|
||||
error_redirect(1, ErrorReasons::UserFlooding);
|
||||
error_redirect(429, ErrorReasons::UserFlooding);
|
||||
return boost::optional<std::string>();
|
||||
}
|
||||
|
||||
|
@ -175,7 +177,7 @@ namespace tawashi {
|
|||
return boost::make_optional(token);
|
||||
}
|
||||
|
||||
error_redirect(1, ErrorReasons::PastieNotSaved);
|
||||
error_redirect(500, ErrorReasons::PastieNotSaved);
|
||||
return boost::optional<std::string>();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue