summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-05-05 03:59:08 +0500
committerilotterytea <iltsu@alright.party>2024-05-05 03:59:08 +0500
commita150df50e31d8cb6145a4ffe7cc7e3a3c623ca54 (patch)
treef682292efa5abfc34f4017437f9ad116803b2232 /src
parent832fab6c6e4d5ff051a33282b11ce5bcaeca7002 (diff)
feat: exception class for commands
Diffstat (limited to 'src')
-rw-r--r--src/commands/command.hpp10
-rw-r--r--src/commands/response_error.hpp93
2 files changed, 103 insertions, 0 deletions
diff --git a/src/commands/command.hpp b/src/commands/command.hpp
index 345b821..40ec114 100644
--- a/src/commands/command.hpp
+++ b/src/commands/command.hpp
@@ -11,6 +11,16 @@
namespace bot {
namespace command {
+ enum CommandArgument {
+ SUBCOMMAND,
+ MESSAGE,
+ INTERVAL,
+ NAME,
+ TARGET,
+ VALUE,
+ AMOUNT,
+ };
+
class Command {
public:
virtual std::string get_name() const = 0;
diff --git a/src/commands/response_error.hpp b/src/commands/response_error.hpp
new file mode 100644
index 0000000..deb142e
--- /dev/null
+++ b/src/commands/response_error.hpp
@@ -0,0 +1,93 @@
+#pragma once
+
+#include <optional>
+#include <string>
+#include <type_traits>
+
+#include "command.hpp"
+
+namespace bot::command {
+ enum ResponseErrorType {
+ NOT_ENOUGH_ARGUMENTS,
+ INCORRECT_ARGUMENT,
+
+ INCOMPATIBLE_NAME,
+ NAMESAKE_CREATION,
+ NOT_FOUND,
+
+ SOMETHING_WENT_WRONG,
+
+ EXTERNAL_API_ERROR,
+ INSUFFICIENT_RIGHTS
+ };
+
+ template <ResponseErrorType T, class Enable = void>
+ class ResponseError;
+
+ template <ResponseErrorType T>
+ class ResponseError<T,
+ typename std::enable_if<
+ T == INCORRECT_ARGUMENT || T == INCOMPATIBLE_NAME ||
+ T == NAMESAKE_CREATION || T == NOT_FOUND>::type> {
+ public:
+ ResponseError(const std::string &message) : message(message), m_type(T){};
+ ~ResponseError() = default;
+
+ const std::string &what() const noexcept { return this->message; }
+
+ const ResponseErrorType &type() const noexcept { return this->m_type; }
+
+ private:
+ std::string message;
+ ResponseErrorType m_type;
+ };
+
+ template <ResponseErrorType T>
+ class ResponseError<T, typename std::enable_if<T == SOMETHING_WENT_WRONG ||
+ T == INSUFFICIENT_RIGHTS>> {
+ public:
+ ResponseError() : m_type(T){};
+ ~ResponseError() = default;
+
+ const ResponseErrorType &type() const noexcept { return this->m_type; }
+
+ private:
+ ResponseErrorType m_type;
+ };
+
+ template <ResponseErrorType T>
+ class ResponseError<T, typename std::enable_if<T == EXTERNAL_API_ERROR>> {
+ public:
+ ResponseError(const int &code, const std::optional<std::string> &message)
+ : m_code(code), message(message), m_type(T){};
+ ~ResponseError() = default;
+
+ const std::optional<std::string> &what() const noexcept {
+ return this->message;
+ }
+ const int &code() const noexcept { return this->code; }
+ const ResponseErrorType &type() const noexcept { return this->m_type; }
+
+ private:
+ int m_code;
+ std::optional<std::string> message;
+ ResponseErrorType m_type;
+ };
+
+ template <ResponseErrorType T>
+ class ResponseError<T, typename std::enable_if<T == NOT_ENOUGH_ARGUMENTS>> {
+ public:
+ ResponseError(const CommandArgument &argument)
+ : m_argument(argument), m_type(T){};
+ ~ResponseError() = default;
+
+ const CommandArgument &argument() const noexcept {
+ return this->m_argument;
+ }
+ const ResponseErrorType &type() const noexcept { return this->m_type; }
+
+ private:
+ CommandArgument m_argument;
+ ResponseErrorType m_type;
+ };
+}