tgbotxx 1.2.9.2
Telegram Bot C++ Library
Loading...
Searching...
No Matches
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
6namespace 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
48
53 INTERNAL = cpr::status::HTTP_INTERNAL_SERVER_ERROR
54 };
55 static std::ostream& operator<<(std::ostream& os, const ErrorCode& errorCode) noexcept {
56 switch (errorCode) {
57 case ErrorCode::OTHER: return os << "OTHER";
58 case ErrorCode::SEE_OTHER: return os << "SEE_OTHER";
59 case ErrorCode::BAD_REQUEST: return os << "BAD_REQUEST";
60 case ErrorCode::UNAUTHORIZED: return os << "UNAUTHORIZED";
61 case ErrorCode::FORBIDDEN: return os << "FORBIDDEN";
62 case ErrorCode::NOT_FOUND: return os << "NOT_FOUND";
63 case ErrorCode::NOT_ACCEPTABLE: return os << "NOT_ACCEPTABLE";
64 case ErrorCode::FLOOD: return os << "FLOOD";
65 case ErrorCode::CONFLICT: return os << "CONFLICT";
66 case ErrorCode::TOO_MANY_REQUESTS: return os << "TOO_MANY_REQUESTS";
67 case ErrorCode::BAD_GATEWAY: return os << "BAD_GATEWAY";
68 case ErrorCode::REQUEST_CANCELLED: return os << "REQUEST_CANCELLED";
69 case ErrorCode::INTERNAL: return os << "INTERNAL";
70 default: return os << "Unknown ErrorCode (" << static_cast<std::int32_t>(errorCode) << ')';
71 }
72 }
73
74 static bool isErrorCode(const std::int32_t c) noexcept {
75 switch (c) {
76 case static_cast<std::int32_t>(ErrorCode::OTHER):
77 case static_cast<std::int32_t>(ErrorCode::SEE_OTHER):
78 case static_cast<std::int32_t>(ErrorCode::BAD_REQUEST):
79 case static_cast<std::int32_t>(ErrorCode::UNAUTHORIZED):
80 case static_cast<std::int32_t>(ErrorCode::FORBIDDEN):
81 case static_cast<std::int32_t>(ErrorCode::NOT_FOUND):
82 case static_cast<std::int32_t>(ErrorCode::NOT_ACCEPTABLE):
83 case static_cast<std::int32_t>(ErrorCode::FLOOD):
84 case static_cast<std::int32_t>(ErrorCode::CONFLICT):
85 case static_cast<std::int32_t>(ErrorCode::TOO_MANY_REQUESTS):
86 case static_cast<std::int32_t>(ErrorCode::BAD_GATEWAY):
87 case static_cast<std::int32_t>(ErrorCode::REQUEST_CANCELLED):
88 case static_cast<std::int32_t>(ErrorCode::INTERNAL):
89 return true;
90 default:
91 return false;
92 }
93 }
94
96 class Exception : public std::runtime_error {
97 const ErrorCode m_errorCode;
98
99 public:
100 explicit Exception(const std::string& errMessage) : std::runtime_error(errMessage), m_errorCode(ErrorCode::OTHER) {}
101 explicit Exception(const std::string& errMessage, ErrorCode errCode) : std::runtime_error(errMessage), m_errorCode(errCode) {}
102
104 return m_errorCode;
105 }
106 };
107}
tgbotxx::Exception
Definition Exception.hpp:96
Exception(const std::string &errMessage)
Exception(const std::string &errMessage, ErrorCode errCode)
ErrorCode errorCode() const noexcept
static bool isErrorCode(const std::int32_t c) noexcept
Definition Exception.hpp:74
std::shared_ptr< T > Ptr
Definition Ptr.hpp:6
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...
@ REQUEST_CANCELLED
Request is cancelled by the sender (e.g. Bot::stop() cancelled long polling request)
@ 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:55