From ec807346255ec56cf5caf9bf8082e4e31ee9b005 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 14 May 2017 03:38:41 +0100 Subject: [PATCH] Add http status codes and save dummy error into the mstch context. --- src/tawashi_implem/error_reasons.hpp | 10 +++++++--- src/tawashi_implem/error_response.cpp | 14 ++++++++++++++ src/tawashi_implem/pastie_response.cpp | 4 ++-- src/tawashi_implem/submit_paste_response.cpp | 10 ++++++---- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/tawashi_implem/error_reasons.hpp b/src/tawashi_implem/error_reasons.hpp index 5320e8c..98fbb24 100644 --- a/src/tawashi_implem/error_reasons.hpp +++ b/src/tawashi_implem/error_reasons.hpp @@ -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 diff --git a/src/tawashi_implem/error_response.cpp b/src/tawashi_implem/error_response.cpp index b762aa9..f742281 100644 --- a/src/tawashi_implem/error_response.cpp +++ b/src/tawashi_implem/error_response.cpp @@ -16,7 +16,12 @@ */ #include "error_response.hpp" +#include "error_reasons.hpp" +#include "cgi_env.hpp" #include +#include +#include +#include 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(get["code"]); + const int reason_int = boost::lexical_cast(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 diff --git a/src/tawashi_implem/pastie_response.cpp b/src/tawashi_implem/pastie_response.cpp index 18df2b2..6af8a1a 100644 --- a/src/tawashi_implem/pastie_response.cpp +++ b/src/tawashi_implem/pastie_response.cpp @@ -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; } diff --git a/src/tawashi_implem/submit_paste_response.cpp b/src/tawashi_implem/submit_paste_response.cpp index 2d168cf..9b33397 100644 --- a/src/tawashi_implem/submit_paste_response.cpp +++ b/src/tawashi_implem/submit_paste_response.cpp @@ -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 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 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(); } @@ -175,7 +177,7 @@ namespace tawashi { return boost::make_optional(token); } - error_redirect(1, ErrorReasons::PastieNotSaved); + error_redirect(500, ErrorReasons::PastieNotSaved); return boost::optional(); }