1 | #include "mtx/errors.hpp" |
---|---|
2 | #include "mtxclient/http/errors.hpp" |
3 | |
4 | #include <coeurl/errors.hpp> |
5 | |
6 | #include <nlohmann/json.hpp> |
7 | |
8 | namespace mtx::errors { |
9 | std::string |
10 | to_string(ErrorCode code) |
11 | { |
12 | switch (code) { |
13 | case ErrorCode::M_UNRECOGNIZED: |
14 | return "M_UNRECOGNIZED"; |
15 | case ErrorCode::M_FORBIDDEN: |
16 | return "M_FORBIDDEN"; |
17 | case ErrorCode::M_UNKNOWN_TOKEN: |
18 | return "M_UNKNOWN_TOKEN"; |
19 | case ErrorCode::M_BAD_JSON: |
20 | return "M_BAD_JSON"; |
21 | case ErrorCode::M_NOT_JSON: |
22 | return "M_NOT_JSON"; |
23 | case ErrorCode::M_NOT_FOUND: |
24 | return "M_NOT_FOUND"; |
25 | case ErrorCode::M_LIMIT_EXCEEDED: |
26 | return "M_LIMIT_EXCEEDED"; |
27 | case ErrorCode::M_USER_IN_USE: |
28 | return "M_USER_IN_USE"; |
29 | case ErrorCode::M_INVALID_USERNAME: |
30 | return "M_INVALID_USERNAME"; |
31 | case ErrorCode::M_ROOM_IN_USE: |
32 | return "M_ROOM_IN_USE"; |
33 | case ErrorCode::M_INVALID_ROOM_STATE: |
34 | return "M_INVALID_ROOM_STATE"; |
35 | case ErrorCode::M_BAD_PAGINATION: |
36 | return "M_BAD_PAGINATION"; |
37 | case ErrorCode::M_THREEPID_IN_USE: |
38 | return "M_THREEPID_IN_USE"; |
39 | case ErrorCode::M_THREEPID_NOT_FOUND: |
40 | return "M_THREEPID_NOT_FOUND"; |
41 | case ErrorCode::M_SERVER_NOT_TRUSTED: |
42 | return "M_SERVER_NOT_TRUSTED"; |
43 | case ErrorCode::M_MISSING_TOKEN: |
44 | return "M_MISSING_TOKEN"; |
45 | case ErrorCode::M_INVALID_SIGNATURE: |
46 | return "M_INVALID_SIGNATURE"; |
47 | case ErrorCode::M_EXCLUSIVE: |
48 | return "M_EXCLUSIVE"; |
49 | case ErrorCode::M_UNKNOWN: |
50 | return "M_UNKNOWN"; |
51 | } |
52 | |
53 | return ""; |
54 | } |
55 | |
56 | ErrorCode |
57 | from_string(const std::string &code) |
58 | { |
59 | if (code == "M_FORBIDDEN") |
60 | return ErrorCode::M_FORBIDDEN; |
61 | else if (code == "M_UNKNOWN_TOKEN") |
62 | return ErrorCode::M_UNKNOWN_TOKEN; |
63 | else if (code == "M_BAD_JSON") |
64 | return ErrorCode::M_BAD_JSON; |
65 | else if (code == "M_NOT_JSON") |
66 | return ErrorCode::M_NOT_JSON; |
67 | else if (code == "M_NOT_FOUND") |
68 | return ErrorCode::M_NOT_FOUND; |
69 | else if (code == "M_LIMIT_EXCEEDED") |
70 | return ErrorCode::M_LIMIT_EXCEEDED; |
71 | else if (code == "M_USER_IN_USE") |
72 | return ErrorCode::M_USER_IN_USE; |
73 | else if (code == "M_INVALID_USERNAME") |
74 | return ErrorCode::M_INVALID_USERNAME; |
75 | else if (code == "M_ROOM_IN_USE") |
76 | return ErrorCode::M_ROOM_IN_USE; |
77 | else if (code == "M_INVALID_ROOM_STATE") |
78 | return ErrorCode::M_INVALID_ROOM_STATE; |
79 | else if (code == "M_BAD_PAGINATION") |
80 | return ErrorCode::M_BAD_PAGINATION; |
81 | else if (code == "M_THREEPID_IN_USE") |
82 | return ErrorCode::M_THREEPID_IN_USE; |
83 | else if (code == "M_THREEPID_NOT_FOUND") |
84 | return ErrorCode::M_THREEPID_NOT_FOUND; |
85 | else if (code == "M_SERVER_NOT_TRUSTED") |
86 | return ErrorCode::M_SERVER_NOT_TRUSTED; |
87 | else if (code == "M_MISSING_TOKEN") |
88 | return ErrorCode::M_MISSING_TOKEN; |
89 | else if (code == "M_INVALID_SIGNATURE") |
90 | return ErrorCode::M_INVALID_SIGNATURE; |
91 | else if (code == "M_EXCLUSIVE") |
92 | return ErrorCode::M_EXCLUSIVE; |
93 | else if (code == "M_UNKNOWN") |
94 | return ErrorCode::M_UNKNOWN; |
95 | else // if (code == "M_UNRECOGNIZED") |
96 | return ErrorCode::M_UNRECOGNIZED; |
97 | } |
98 | |
99 | void |
100 | from_json(const nlohmann::json &obj, LightweightError &error) |
101 | { |
102 | error.errcode = from_string(code: obj.value(key: "errcode", default_value: "")); |
103 | error.error = obj.value(key: "error", default_value: ""); |
104 | } |
105 | |
106 | void |
107 | from_json(const nlohmann::json &obj, Error &error) |
108 | { |
109 | error.errcode = from_string(code: obj.value(key: "errcode", default_value: "")); |
110 | error.error = obj.value(key: "error", default_value: ""); |
111 | |
112 | if (obj.contains(key: "flows")) |
113 | error.unauthorized = obj.get<user_interactive::Unauthorized>(); |
114 | |
115 | if (obj.contains(key: "retry_after_ms")) |
116 | error.retry_after = |
117 | std::chrono::milliseconds(obj.value(key: "retry_after_ms", default_value: std::uint64_t{0})); |
118 | } |
119 | } |
120 | |
121 | const char * |
122 | mtx::http::ClientError::error_code_string() const |
123 | { |
124 | return coeurl::to_string(c: static_cast<CURLcode>(error_code)); |
125 | } |
126 |