aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
error.hpp
1 //
2 // error.hpp
3 // *********
4 //
5 // Copyright (c) 2019 Sharon W (sharon at aegis dot gg)
6 //
7 // Distributed under the MIT License. (See accompanying file LICENSE)
8 //
9 
10 #pragma once
11 
12 #include "aegis/config.hpp"
13 #include <exception>
14 #include <string>
15 #include <utility>
16 #include <system_error>
17 
18 namespace aegis
19 {
20 using err_str_pair = std::pair<std::error_code, std::string>;
21 
22 using error_code = std::error_code;
23 
26 enum error
27 {
29  general = 1,
30 
32  invalid_token,
33 
35  invalid_state,
36 
38  get_gateway,
39 
41  no_gateway,
42 
44  no_permission,
45 
47  not_implemented,
48 
50  member_not_found,
51 
53  channel_not_found,
54 
56  guild_not_found,
57 
59  rate_limited,
60 
62  global_rate_limited,
63 
65  member_error,
66 
68  channel_error,
69 
71  guild_error,
72 
74  shard_error,
75 
77  bad_request,
78 
80  bad_redis_request,
81 
83  bulk_delete_out_of_range,
84 
86  invalid_intents,
87 
89  disallowed_intents,
90 
92  max_errors
93 };
94 
97 class category : public std::error_category
98 {
99 public:
100  category() = default;
101 
102  char const * name() const noexcept override
103  {
104  return "aegis";
105  }
106 
107  std::string message(int val) const override
108  {
109  switch (val)
110  {
111  case error::general:
112  return "Generic error";
113  case error::invalid_token:
114  return "Token invalid";
115  case error::invalid_state:
116  return "Invalid state";
117  case error::get_gateway:
118  return "Unable to retrieve gateway data";
119  case error::no_gateway:
120  return "No gateway url set";
121  case error::no_permission:
122  return "No permission for this action";
123  case error::not_implemented:
124  return "Feature not yet implemented";
125  case error::member_not_found:
126  return "Member not found";
127  case error::channel_not_found:
128  return "Channel not found";
129  case error::guild_not_found:
130  return "Guild not found";
131  case error::rate_limited:
132  return "Rate limited";
133  case error::global_rate_limited:
134  return "Globally rate limited";
135  case error::member_error:
136  return "Member related error";
137  case error::channel_error:
138  return "Channel related error";
139  case error::guild_error:
140  return "Guild related error";
141  case error::shard_error:
142  return "Shard related error";
143  case error::bad_request:
144  return "Bad request";
145  case error::bad_redis_request:
146  return "Bad Redis request";
147  case error::bulk_delete_out_of_range:
148  return "Bulk delete invalid message amount";
149  case error::invalid_intents:
150  return "Invalid intents";
151  case error::disallowed_intents:
152  return "Disallowed intents";
153  default:
154  return "Unknown";
155  }
156  }
157 };
158 
161 inline const std::error_category & get_category()
162 {
163  static category instance;
164  return instance;
165 }
166 
169 inline std::error_code make_error_code(error e)
170 {
171  return std::error_code(static_cast<int>(e), get_category());
172 }
173 
176 class exception : public std::exception
177 {
178 public:
179  exception(std::string const & msg, std::error_code ec = make_error_code(error::general))
180  : _msg(msg.empty() ? ec.message() : msg)
181  , _code(ec)
182  {
183  }
184 
185  explicit exception(std::error_code ec)
186  : _msg(ec.message())
187  , _code(ec)
188  {
189  }
190 
191  ~exception() = default;
192 
193  virtual char const * what() const noexcept override
194  {
195  return _msg.c_str();
196  }
197 
198  std::error_code code() const noexcept
199  {
200  return _code;
201  }
202 
203  const std::string _msg;
204  std::error_code _code;
205 };
206 
207 }
208 
210 namespace std
211 {
212 
213 template<> struct is_error_code_enum<aegis::error>
214 {
215  static const bool value = true;
216 };
217 
218 }
Definition: error.hpp:97
Definition: error.hpp:176