diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-18 14:48:12 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-18 14:48:12 +0500 |
| commit | d1793df1eda463b10107d41785ad1d7f055ed476 (patch) | |
| tree | fd3e41c3b4a05924748ae4b762e1ae55a0bc815c /src/commands/response_error.hpp | |
| parent | d7a2de17e9b7931f68b5b4079b1c36866a19d343 (diff) | |
upd: moved the bot part to a relative subfolder
Diffstat (limited to 'src/commands/response_error.hpp')
| -rw-r--r-- | src/commands/response_error.hpp | 222 |
1 files changed, 0 insertions, 222 deletions
diff --git a/src/commands/response_error.hpp b/src/commands/response_error.hpp deleted file mode 100644 index ae2c3ee..0000000 --- a/src/commands/response_error.hpp +++ /dev/null @@ -1,222 +0,0 @@ -#pragma once - -#include <exception> -#include <optional> -#include <string> -#include <type_traits> -#include <vector> - -#include "command.hpp" -#include "request.hpp" - -namespace bot { - enum ResponseError { - NOT_ENOUGH_ARGUMENTS, - INCORRECT_ARGUMENT, - - INCOMPATIBLE_NAME, - NAMESAKE_CREATION, - NOT_FOUND, - - SOMETHING_WENT_WRONG, - - EXTERNAL_API_ERROR, - INSUFFICIENT_RIGHTS, - - ILLEGAL_COMMAND - }; - - template <ResponseError T, class Enable = void> - class ResponseException; - - template <ResponseError T> - class ResponseException< - T, typename std::enable_if< - T == INCORRECT_ARGUMENT || T == INCOMPATIBLE_NAME || - T == NAMESAKE_CREATION || T == NOT_FOUND>::type> - : public std::exception { - public: - ResponseException(const command::Request &request, - const loc::Localization &localizator, - const std::string &message) - : request(request), - localizator(localizator), - message(message), - error(T) { - loc::LineId line_id; - - switch (this->error) { - case INCORRECT_ARGUMENT: - line_id = loc::LineId::ErrorIncorrectArgument; - break; - case INCOMPATIBLE_NAME: - line_id = loc::LineId::ErrorIncompatibleName; - break; - case NAMESAKE_CREATION: - line_id = loc::LineId::ErrorNamesakeCreation; - break; - case NOT_FOUND: - line_id = loc::LineId::ErrorNotFound; - break; - default: - line_id = loc::LineId::ErrorSomethingWentWrong; - break; - }; - - this->line = - this->localizator - .get_formatted_line(this->request, line_id, {this->message}) - .value(); - } - ~ResponseException() = default; - - const char *what() const noexcept override { return this->line.c_str(); } - - private: - const command::Request &request; - const loc::Localization &localizator; - std::string message, line; - ResponseError error; - }; - - template <ResponseError T> - class ResponseException<T, - typename std::enable_if<T == SOMETHING_WENT_WRONG || - T == INSUFFICIENT_RIGHTS || - T == ILLEGAL_COMMAND>::type> - : public std::exception { - public: - ResponseException(const command::Request &request, - const loc::Localization &localizator) - : request(request), localizator(localizator), error(T) { - loc::LineId line_id; - - switch (this->error) { - case INSUFFICIENT_RIGHTS: - line_id = loc::LineId::ErrorInsufficientRights; - break; - case ILLEGAL_COMMAND: - line_id = loc::LineId::ErrorIllegalCommand; - break; - default: - line_id = loc::LineId::ErrorSomethingWentWrong; - break; - } - - this->line = - this->localizator.get_formatted_line(this->request, line_id, {}) - .value(); - } - ~ResponseException() = default; - - const char *what() const noexcept override { return this->line.c_str(); } - - private: - const command::Request &request; - const loc::Localization &localizator; - std::string line; - ResponseError error; - }; - - template <ResponseError T> - class ResponseException< - T, typename std::enable_if<T == EXTERNAL_API_ERROR>::type> - : public std::exception { - public: - ResponseException( - const command::Request &request, const loc::Localization &localizator, - const int &code, - const std::optional<std::string> &message = std::nullopt) - : request(request), - localizator(localizator), - code(code), - message(message), - error(T) { - loc::LineId line_id = loc::LineId::ErrorExternalAPIError; - std::vector<std::string> args = {std::to_string(this->code)}; - - if (this->message.has_value()) { - args.push_back(" " + this->message.value()); - } - - this->line = - this->localizator.get_formatted_line(this->request, line_id, args) - .value(); - } - ~ResponseException() = default; - - const char *what() const noexcept override { return this->line.c_str(); } - - private: - const command::Request &request; - const loc::Localization &localizator; - int code; - std::optional<std::string> message; - std::string line; - ResponseError error; - }; - - template <ResponseError T> - class ResponseException< - T, typename std::enable_if<T == NOT_ENOUGH_ARGUMENTS>::type> - : public std::exception { - public: - ResponseException(const command::Request &request, - const loc::Localization &localizator, - command::CommandArgument argument) - : request(request), - localizator(localizator), - argument(argument), - error(T) { - loc::LineId line_id = loc::LineId::ErrorNotEnoughArguments; - loc::LineId arg_id; - - switch (this->argument) { - case command::SUBCOMMAND: - arg_id = loc::LineId::ArgumentSubcommand; - break; - case command::MESSAGE: - arg_id = loc::LineId::ArgumentMessage; - break; - case command::INTERVAL: - arg_id = loc::LineId::ArgumentInterval; - break; - case command::NAME: - arg_id = loc::LineId::ArgumentName; - break; - case command::TARGET: - arg_id = loc::LineId::ArgumentTarget; - break; - case command::VALUE: - arg_id = loc::LineId::ArgumentValue; - break; - case command::AMOUNT: - arg_id = loc::LineId::ArgumentAmount; - break; - default: - break; - } - - auto arg = - this->localizator - .get_localized_line( - this->request.channel_preferences.get_locale(), arg_id) - .value(); - - this->line = - this->localizator.get_formatted_line(this->request, line_id, {arg}) - .value(); - } - ~ResponseException() = default; - - const char *what() const noexcept override { return this->line.c_str(); } - - private: - const command::Request &request; - const loc::Localization &localizator; - command::CommandArgument argument; - ResponseError error; - std::string line; - }; - -} |
