diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-06-01 20:34:33 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-06-01 20:34:33 -0400 |
commit | 442f1ee071152be04c4184473ddfee5040795b76 (patch) | |
tree | 297cffbf4a3728039519541429f68a670d70b98c /src/client.cpp | |
parent | aceb3cc33ee78cce1077252aff8a8808a3195ff1 (diff) | |
download | libtwittercpp-442f1ee071152be04c4184473ddfee5040795b76.tar.gz libtwittercpp-442f1ee071152be04c4184473ddfee5040795b76.tar.bz2 libtwittercpp-442f1ee071152be04c4184473ddfee5040795b76.zip |
Wrapped JSON parsing in exception handling
Diffstat (limited to 'src/client.cpp')
-rw-r--r-- | src/client.cpp | 88 |
1 files changed, 77 insertions, 11 deletions
diff --git a/src/client.cpp b/src/client.cpp index cf2b5c4..1b32b8b 100644 --- a/src/client.cpp +++ b/src/client.cpp | |||
@@ -105,7 +105,12 @@ namespace twitter { | |||
105 | std::string response_data; | 105 | std::string response_data; |
106 | if (performGet(url, response_code, response_data) && (response_code == 200)) | 106 | if (performGet(url, response_code, response_data) && (response_code == 200)) |
107 | { | 107 | { |
108 | _current_user = user(response_data); | 108 | try { |
109 | _current_user = user(response_data); | ||
110 | } catch (std::invalid_argument e) | ||
111 | { | ||
112 | // Ignore | ||
113 | } | ||
109 | } | 114 | } |
110 | } | 115 | } |
111 | 116 | ||
@@ -145,8 +150,13 @@ namespace twitter { | |||
145 | 150 | ||
146 | if (response_code == 200) | 151 | if (response_code == 200) |
147 | { | 152 | { |
148 | result = tweet(response_data); | 153 | try { |
149 | return response::ok; | 154 | result = tweet(response_data); |
155 | return response::ok; | ||
156 | } catch (std::invalid_argument e) | ||
157 | { | ||
158 | return response::invalid_response; | ||
159 | } | ||
150 | } else { | 160 | } else { |
151 | return codeForError(response_code, response_data); | 161 | return codeForError(response_code, response_data); |
152 | } | 162 | } |
@@ -186,7 +196,14 @@ namespace twitter { | |||
186 | return codeForError(response_code, response_data); | 196 | return codeForError(response_code, response_data); |
187 | } | 197 | } |
188 | 198 | ||
189 | auto response_json = json::parse(response_data); | 199 | json response_json; |
200 | try { | ||
201 | response_json = json::parse(response_data); | ||
202 | } catch (std::invalid_argument e) | ||
203 | { | ||
204 | return response::invalid_response; | ||
205 | } | ||
206 | |||
190 | media_id = response_json["media_id"].get<long>(); | 207 | media_id = response_json["media_id"].get<long>(); |
191 | 208 | ||
192 | curl_httppost* append_form_post = nullptr; | 209 | curl_httppost* append_form_post = nullptr; |
@@ -226,7 +243,13 @@ namespace twitter { | |||
226 | return codeForError(response_code, response_data); | 243 | return codeForError(response_code, response_data); |
227 | } | 244 | } |
228 | 245 | ||
229 | response_json = json::parse(response_data); | 246 | try { |
247 | response_json = json::parse(response_data); | ||
248 | } catch (std::invalid_argument e) | ||
249 | { | ||
250 | return response::invalid_response; | ||
251 | } | ||
252 | |||
230 | if (response_json.find("processing_info") != response_json.end()) | 253 | if (response_json.find("processing_info") != response_json.end()) |
231 | { | 254 | { |
232 | std::stringstream datastr; | 255 | std::stringstream datastr; |
@@ -244,7 +267,13 @@ namespace twitter { | |||
244 | return codeForError(response_code, response_data); | 267 | return codeForError(response_code, response_data); |
245 | } | 268 | } |
246 | 269 | ||
247 | response_json = json::parse(response_data); | 270 | try { |
271 | response_json = json::parse(response_data); | ||
272 | } catch (std::invalid_argument e) | ||
273 | { | ||
274 | return response::invalid_response; | ||
275 | } | ||
276 | |||
248 | if (response_json["processing_info"]["state"] == "succeeded") | 277 | if (response_json["processing_info"]["state"] == "succeeded") |
249 | { | 278 | { |
250 | break; | 279 | break; |
@@ -316,9 +345,26 @@ namespace twitter { | |||
316 | return unfollow(toUnfollow.getID()); | 345 | return unfollow(toUnfollow.getID()); |
317 | } | 346 | } |
318 | 347 | ||
319 | const user& client::getUser() const | 348 | response client::getUser(user& result) |
320 | { | 349 | { |
321 | return _current_user; | 350 | if (!_current_user) |
351 | { | ||
352 | std::string url = "https://api.twitter.com/1.1/account/verify_credentials.json"; | ||
353 | long response_code; | ||
354 | std::string response_data; | ||
355 | if (performGet(url, response_code, response_data) && (response_code == 200)) | ||
356 | { | ||
357 | try { | ||
358 | _current_user = user(response_data); | ||
359 | } catch (std::invalid_argument e) | ||
360 | { | ||
361 | return response::invalid_response; | ||
362 | } | ||
363 | } | ||
364 | } | ||
365 | |||
366 | result = _current_user; | ||
367 | return response::ok; | ||
322 | } | 368 | } |
323 | 369 | ||
324 | configuration client::getConfiguration() | 370 | configuration client::getConfiguration() |
@@ -366,7 +412,14 @@ namespace twitter { | |||
366 | 412 | ||
367 | if (response_code == 200) | 413 | if (response_code == 200) |
368 | { | 414 | { |
369 | json rjs = json::parse(response_data); | 415 | json rjs; |
416 | try { | ||
417 | rjs = json::parse(response_data); | ||
418 | } catch (std::invalid_argument e) | ||
419 | { | ||
420 | return response::invalid_response; | ||
421 | } | ||
422 | |||
370 | cursor = rjs.at("next_cursor"); | 423 | cursor = rjs.at("next_cursor"); |
371 | result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); | 424 | result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); |
372 | } else { | 425 | } else { |
@@ -408,7 +461,14 @@ namespace twitter { | |||
408 | 461 | ||
409 | if (response_code == 200) | 462 | if (response_code == 200) |
410 | { | 463 | { |
411 | json rjs = json::parse(response_data); | 464 | json rjs; |
465 | try { | ||
466 | rjs = json::parse(response_data); | ||
467 | } catch (std::invalid_argument e) | ||
468 | { | ||
469 | return response::invalid_response; | ||
470 | } | ||
471 | |||
412 | cursor = rjs.at("next_cursor"); | 472 | cursor = rjs.at("next_cursor"); |
413 | result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); | 473 | result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); |
414 | } else { | 474 | } else { |
@@ -560,7 +620,13 @@ namespace twitter { | |||
560 | 620 | ||
561 | response client::codeForError(int response_code, std::string response_data) const | 621 | response client::codeForError(int response_code, std::string response_data) const |
562 | { | 622 | { |
563 | auto response_json = json::parse(response_data); | 623 | json response_json; |
624 | try { | ||
625 | response_json = json::parse(response_data); | ||
626 | } catch (std::invalid_argument e) | ||
627 | { | ||
628 | return response::invalid_response; | ||
629 | } | ||
564 | 630 | ||
565 | std::set<int> error_codes; | 631 | std::set<int> error_codes; |
566 | if (response_json.find("errors") != response_json.end()) | 632 | if (response_json.find("errors") != response_json.end()) |