tgbotxx  1.1.6.9
Telegram Bot C++ Library
Exception.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <cpr/status_codes.h>
3 #include <stdexcept>
4 #include <string>
5 
6 namespace tgbotxx {
8  enum class ErrorCode : std::int32_t {
11  OTHER = 0,
12 
14  SEE_OTHER = cpr::status::HTTP_SEE_OTHER,
15 
18  BAD_REQUEST = cpr::status::HTTP_BAD_REQUEST,
19 
21  UNAUTHORIZED = cpr::status::HTTP_UNAUTHORIZED,
22 
24  FORBIDDEN = cpr::status::HTTP_FORBIDDEN,
25 
27  NOT_FOUND = cpr::status::HTTP_NOT_FOUND,
28 
30  NOT_ACCEPTABLE = cpr::status::HTTP_NOT_ACCEPTABLE,
31 
35  FLOOD = 420,
36 
38  CONFLICT = cpr::status::HTTP_CONFLICT,
39 
41  TOO_MANY_REQUESTS = cpr::status::HTTP_TOO_MANY_REQUESTS,
42 
44  BAD_GATEWAY = cpr::status::HTTP_BAD_GATEWAY,
45 
50  INTERNAL = cpr::status::HTTP_INTERNAL_SERVER_ERROR
51  };
52  static std::ostream& operator<<(std::ostream& os, const ErrorCode& errorCode) noexcept {
53  switch (errorCode) {
54  case ErrorCode::OTHER: return os << "OTHER";
55  case ErrorCode::SEE_OTHER: return os << "SEE_OTHER";
56  case ErrorCode::BAD_REQUEST: return os << "BAD_REQUEST";
57  case ErrorCode::UNAUTHORIZED: return os << "UNAUTHORIZED";
58  case ErrorCode::FORBIDDEN: return os << "FORBIDDEN";
59  case ErrorCode::NOT_FOUND: return os << "NOT_FOUND";
60  case ErrorCode::NOT_ACCEPTABLE: return os << "NOT_ACCEPTABLE";
61  case ErrorCode::FLOOD: return os << "FLOOD";
62  case ErrorCode::CONFLICT: return os << "CONFLICT";
63  case ErrorCode::TOO_MANY_REQUESTS: return os << "TOO_MANY_REQUESTS";
64  case ErrorCode::BAD_GATEWAY: return os << "BAD_GATEWAY";
65  case ErrorCode::INTERNAL: return os << "INTERNAL";
66  default: return os << "Unknown ErrorCode (" << (std::int32_t)errorCode << ')';
67  }
68  }
69 
70  static bool isErrorCode(std::int32_t c) noexcept {
71  switch (c) {
72  case static_cast<std::int32_t>(ErrorCode::OTHER):
73  case static_cast<std::int32_t>(ErrorCode::SEE_OTHER):
74  case static_cast<std::int32_t>(ErrorCode::BAD_REQUEST):
75  case static_cast<std::int32_t>(ErrorCode::UNAUTHORIZED):
76  case static_cast<std::int32_t>(ErrorCode::FORBIDDEN):
77  case static_cast<std::int32_t>(ErrorCode::NOT_FOUND):
78  case static_cast<std::int32_t>(ErrorCode::NOT_ACCEPTABLE):
79  case static_cast<std::int32_t>(ErrorCode::FLOOD):
80  case static_cast<std::int32_t>(ErrorCode::CONFLICT):
81  case static_cast<std::int32_t>(ErrorCode::TOO_MANY_REQUESTS):
82  case static_cast<std::int32_t>(ErrorCode::BAD_GATEWAY):
83  case static_cast<std::int32_t>(ErrorCode::INTERNAL):
84  return true;
85  default:
86  return false;
87  }
88  }
89 
91  class Exception : public std::runtime_error {
92  const ErrorCode m_errorCode;
93 
94  public:
95  explicit Exception(const std::string& errMessage) : std::runtime_error(errMessage), m_errorCode(ErrorCode::OTHER) {}
96  explicit Exception(const std::string& errMessage, ErrorCode errCode) : std::runtime_error(errMessage), m_errorCode(errCode) {}
97 
98  [[nodiscard]] ErrorCode errorCode() const noexcept {
99  return m_errorCode;
100  }
101  };
102 }
tgbotxx::Exception
Definition: Exception.hpp:91
Exception(const std::string &errMessage)
Definition: Exception.hpp:95
Exception(const std::string &errMessage, ErrorCode errCode)
Definition: Exception.hpp:96
ErrorCode errorCode() const noexcept
Definition: Exception.hpp:98
Definition: Api.hpp:14
ErrorCode
https://core.telegram.org/api/errors
Definition: Exception.hpp:8
@ UNAUTHORIZED
There was an unauthorized attempt to use functionality available only to authorized users.
@ BAD_GATEWAY
Bad Gateway.
@ SEE_OTHER
The request must be repeated, but directed to a different data center.
@ TOO_MANY_REQUESTS
Too Many Requests: retry after X.
@ FORBIDDEN
Privacy violation. For example, an attempt to write a message to someone who has blacklisted the curr...
@ NOT_FOUND
An attempt to invoke a non-existent object, such as a method.
@ NOT_ACCEPTABLE
Similar to 400 BAD_REQUEST, but the app must display the error to the user a bit differently.
@ CONFLICT
Conflict: terminated by other long poll or webhook.
static std::ostream & operator<<(std::ostream &os, const ErrorCode &errorCode) noexcept
Definition: Exception.hpp:52
static bool isErrorCode(std::int32_t c) noexcept
Definition: Exception.hpp:70