cpp-mirai-client  v2.6.1
cpp client for mirai-api-http
Exceptions.hpp
浏览该文件的文档.
1// Copyright (C) 2022 Numendacil and contributors
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#ifndef MIRAI_EXCEPTIONS_HPP_
17#define MIRAI_EXCEPTIONS_HPP_
18
19#include <exception>
20#include <stdexcept>
21#include <string>
22namespace Mirai
23{
24
25/**
26 * @brief mirai-api-http相关的异常
27 *
28 */
29class MiraiApiHttpException : public std::runtime_error
30{
31public:
32 /// 错误码
33 int code;
34 /// 错误信息
35 std::string message;
37 : std::runtime_error("mirai-api-http error: " + message + " <" + std::to_string(code) + ">")
38 , code(code)
39 , message(std::move(message))
40 {
41 }
42};
43
44#define REGISTER_STATUS_CODE(name, code) \
45 class name : public MiraiApiHttpException \
46 { \
47 public: \
48 name(std::string& message) : MiraiApiHttpException(code, message) {} \
49 };
50
51/// MAH异常: 验证密钥错误
53/// MAH异常: 不存在该Bot账号
55/// MAH异常: 无效Session
57/// MAH异常: Session未绑定账号
59/// MAH异常: 指定对象不存在
61/// MAH异常: 不支持该操作
63/// MAH异常: 文件不存在
65/// MAH异常: 缺少相关权限
67/// MAH异常: Bot被禁言
69/// MAH异常: 消息过长
71// REGISTER_STATUS_CODE(InvalidParameter, 400)
72
73#undef REGISTER_STATUS_CODE
74
75
76/**
77 * @brief 网络通信错误
78 *
79 */
80class NetworkException : public std::runtime_error
81{
82public:
83 /// 错误码
84 int code;
85 /// 错误信息
86 std::string message;
87 NetworkException(int code, std::string message)
88 : std::runtime_error("Network error: " + message + " <" + std::to_string(code) + ">")
89 , code(code)
90 , message(std::move(message))
91 {
92 }
93};
94
95/**
96 * @brief 消息解析错误
97 *
98 */
99class ParseError : public std::runtime_error
100{
101public:
102 /// 原被解析消息
103 std::string message;
104 /// 错误消息
105 std::string error;
106 ParseError(std::string error, std::string message)
107 : std::runtime_error("Unable to parse \"" + message + "\": " + error), message(std::move(message)), error(std::move(error))
108 {
109 }
110};
111
112/**
113 * @brief 类型匹配错误
114 *
115 */
116class TypeDismatchError : public std::runtime_error
117{
118public:
119 /// 目标类型
120 std::string expected_type;
121 /// 实际类型
122 std::string received_type;
123
124 TypeDismatchError(std::string expected, std::string received)
125 : std::runtime_error("Expecting type " + expected + ", but get " + received + " instead")
126 , expected_type(std::move(expected))
127 , received_type(std::move(received))
128 {
129 }
130};
131
132/**
133 * @brief 未实现方法
134 *
135 */
136class NotImplementedError : public std::runtime_error
137{
138public:
139 using std::runtime_error::runtime_error;
140};
141
142} // namespace Mirai
143
144
145#endif
#define REGISTER_STATUS_CODE(name, code)
Definition: Exceptions.hpp:44
MAH异常: 验证密钥错误
Definition: Exceptions.hpp:52
MAH异常: Bot被禁言
Definition: Exceptions.hpp:68
MAH异常: 无效Session
Definition: Exceptions.hpp:56
MAH异常: 消息过长
Definition: Exceptions.hpp:70
mirai-api-http相关的异常
Definition: Exceptions.hpp:30
std::string message
错误信息
Definition: Exceptions.hpp:35
MiraiApiHttpException(int code, std::string message)
Definition: Exceptions.hpp:36
网络通信错误
Definition: Exceptions.hpp:81
std::string message
错误信息
Definition: Exceptions.hpp:86
NetworkException(int code, std::string message)
Definition: Exceptions.hpp:87
MAH异常: 不存在该Bot账号
Definition: Exceptions.hpp:54
MAH异常: 指定对象不存在
Definition: Exceptions.hpp:60
MAH异常: 不支持该操作
Definition: Exceptions.hpp:62
MAH异常: 文件不存在
Definition: Exceptions.hpp:64
MAH异常: Session未绑定账号
Definition: Exceptions.hpp:58
消息解析错误
Definition: Exceptions.hpp:100
ParseError(std::string error, std::string message)
Definition: Exceptions.hpp:106
std::string error
错误消息
Definition: Exceptions.hpp:105
std::string message
原被解析消息
Definition: Exceptions.hpp:103
MAH异常: 缺少相关权限
Definition: Exceptions.hpp:66
类型匹配错误
Definition: Exceptions.hpp:117
std::string received_type
实际类型
Definition: Exceptions.hpp:122
std::string expected_type
目标类型
Definition: Exceptions.hpp:120
TypeDismatchError(std::string expected, std::string received)
Definition: Exceptions.hpp:124
所有mirai相关的对象的命名空间
std::string to_string(EventTypes type)
STL namespace