Skip to content

Commit dc5f9ba

Browse files
authored
Better error handling on client (yhirose#601)
1 parent cf084e1 commit dc5f9ba

File tree

5 files changed

+747
-677
lines changed

5 files changed

+747
-677
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,13 @@ int main(void)
286286
{
287287
httplib::Client cli("localhost", 1234);
288288
289-
auto res = cli.Get("/hi");
290-
if (res && res->status == 200) {
291-
std::cout << res->body << std::endl;
289+
if (auto res = cli.Get("/hi")) {
290+
if (res->status == 200) {
291+
std::cout << res->body << std::endl;
292+
}
293+
} else {
294+
auto err = res.error();
295+
...
292296
}
293297
}
294298
```
@@ -434,13 +438,12 @@ auto res = cli_.Post(
434438
httplib::Client client(url, port);
435439
436440
// prints: 0 / 000 bytes => 50% complete
437-
std::shared_ptr<httplib::Response> res =
438-
cli.Get("/", [](uint64_t len, uint64_t total) {
439-
printf("%lld / %lld bytes => %d%% complete\n",
440-
len, total,
441-
(int)(len*100/total));
442-
return true; // return 'false' if you want to cancel the request.
443-
}
441+
auto res = cli.Get("/", [](uint64_t len, uint64_t total) {
442+
printf("%lld / %lld bytes => %d%% complete\n",
443+
len, total,
444+
(int)(len*100/total));
445+
return true; // return 'false' if you want to cancel the request.
446+
}
444447
);
445448
```
446449

example/client.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ int main(void) {
2323
httplib::Client cli("localhost", 8080);
2424
#endif
2525

26-
auto res = cli.Get("/hi");
27-
if (res) {
26+
if (auto res = cli.Get("/hi")) {
2827
cout << res->status << endl;
2928
cout << res->get_header_value("Content-Type") << endl;
3029
cout << res->body << endl;
3130
} else {
31+
cout << "error code: " << res.error() << std::endl;
3232
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3333
auto result = cli.get_openssl_verify_result();
3434
if (result) {

example/simplecli.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ int main(void) {
1717
auto scheme_host_port = "http://localhost:8080";
1818
#endif
1919

20-
auto res = httplib::Client(scheme_host_port).Get("/hi");
21-
22-
if (res) {
20+
if (auto res = httplib::Client(scheme_host_port).Get("/hi")) {
2321
cout << res->status << endl;
2422
cout << res->get_header_value("Content-Type") << endl;
2523
cout << res->body << endl;
24+
} else {
25+
cout << res.error() << endl;
2626
}
2727

2828
return 0;

0 commit comments

Comments
 (0)