Operator
C++ library for plugging into matrix.org
error.hh
Go to the documentation of this file.
1 
20 #ifndef __OPERATOR_ERROR_HH_
21 #define __OPERATOR_ERROR_HH_
22 
23 #include <json.hpp>
24 
25 #include <functional>
26 #include <map>
27 #include <string>
28 
29 namespace Operator {
32  namespace Error {
37  class Error {
38  protected:
39  Error() {};
40  public:
41  virtual ~Error() {};
42  virtual std::string to_string() const = 0;
43  };
44 
49  class InvalidData : public Error {
50  public:
51  InvalidData(const std::string &_description)
52  : description(_description)
53  {}
54  std::string to_string() const { return description; };
55  private:
56  std::string description;
57  };
58 
62  class MatrixError : public Error {
63  public:
64  virtual const std::string &getErrorCode() const = 0;
65  const std::string &getError() const { return error; }
66  unsigned short getStatusCode() const { return status_code; }
67  const std::map<std::string,nlohmann::json> &getData() const { return other_data; }
68 
69  static Error * newError(unsigned short status_code, const nlohmann::json &data) throw ();
70 
71  std::string to_string() const { return getErrorCode() + " (" + std::to_string(getStatusCode()) + "): " + getError(); }
72 
73  static const char M_FORBIDDEN[];
74  static const char M_UNKNOWN_TOKEN[];
75  static const char M_BAD_JSON[];
76  static const char M_NOT_JSON[];
77  static const char M_NOT_FOUND[];
78  static const char M_LIMIT_EXCEEDED[];
79  static const char M_USER_IN_USE[];
80  static const char M_INVALID_USERNAME[];
81  static const char M_ROOM_IN_USE[];
82  static const char M_BAD_PAGINATION[];
83  static const char M_THREEPID_IN_USE[];
84  static const char M_THREEPID_NOT_FOUND[];
85  static const char M_SERVER_NOT_TRUSTED[];
86  static const char M_WEAK_PASSWORD[];
87  static const char M_UNKNOWN[];
88  static const char M_EXCLUSIVE[];
89  static const char M_MISSING_PARAM[];
90 
91  typedef std::function<MatrixError *(unsigned short, const std::string &, const std::string &, std::map<std::string,nlohmann::json> &)> error_builder_t;
92  static void registerErrorClass(const std::string &error_code, error_builder_t);
93  template<typename T> static MatrixError *_new_helper(unsigned short _status_code, const std::string &, const std::string &_error, std::map<std::string,nlohmann::json> &_other_data) {
94  return new T(_status_code, _error, _other_data);
95  }
96  private:
97  unsigned short status_code;
98  std::string error;
99  std::map<std::string,nlohmann::json> other_data;
100  protected:
101  MatrixError(unsigned short _status_code, const std::string &_error, std::map<std::string,nlohmann::json> &_other_data)
102  : status_code(_status_code),
103  error(_error),
104  other_data(std::move(_other_data))
105  {}
106  };
107 
108  template<const char *c> class _MatrixErr : public MatrixError {
109  public:
110  _MatrixErr(unsigned short _status_code, std::string _error, std::map<std::string,nlohmann::json> &_other_data)
111  : MatrixError(_status_code, _error, _other_data) {}
112 
113  const std::string &getErrorCode() const { return ERROR_CODE; }
114  static const std::string ERROR_CODE;
115  };
116 
202 
205  class OtherMatrixError : public MatrixError {
206  public:
207  OtherMatrixError(unsigned short _status_code, const std::string &_error_code, const std::string &_error, std::map<std::string,nlohmann::json> &_other_data)
208  : MatrixError(_status_code, _error, _other_data),
209  error_code(_error_code)
210  {}
211  const std::string &getErrorCode() const { return error_code; }
212  private:
213  const std::string error_code;
214  };
215  }
216 }
217 
218 #endif // __OPERATOR_ERROR_HH_
Unknown (possibly custom) Matrix error.
Definition: error.hh:205
The access token specified was not recognised.
Encountered when trying to register a user ID which is not valid.
The password is too weak.
Request contained valid JSON, but it was malformed in some way, e.g. missing required keys...
Definition: client.hh:32
The desired user ID is in the exclusive namespace claimed by an application service.
Encountered when trying to register a user ID which has been taken.
A parameter is missing.
Sent when a threepid given to an API cannot be used because no record matching the threepid was found...
No resource was found for this request.
The client&#39;s request used a third party server, eg. ID server, that this server does not trust...
Sent when a threepid given to an API cannot be used because the same threepid is already in use...
Definition: error.hh:108
Request did not contain valid JSON.
Encountered when trying to create a room which has been taken.
base error class.
Definition: error.hh:37
base error class for Matrix protocol errors.
Definition: error.hh:62
Encountered when specifying bad pagination query parameters.
invalid data error.
Definition: error.hh:49
Forbidden access, e.g. joining a room without permission, failed login.
Too many requests have been sent in a short period of time. Wait a while then try again...