{"action":"created","issue":{"url":"https://api.github.com/repos/chriskohlhoff/networking-ts-impl/issues/13","repository_url":"https://api.github.com/repos/chriskohlhoff/networking-ts-impl","labels_url":"https://api.github.com/repos/chriskohlhoff/networking-ts-impl/issues/13/labels{/name}","comments_url":"https://api.github.com/repos/chriskohlhoff/networking-ts-impl/issues/13/comments","events_url":"https://api.github.com/repos/chriskohlhoff/networking-ts-impl/issues/13/events","html_url":"https://github.com/chriskohlhoff/networking-ts-impl/issues/13","id":288379834,"node_id":"MDU6SXNzdWUyODgzNzk4MzQ=","number":13,"title":"Please consider making cross platform exceptions easy to catch","user":{"login":"sldr","id":9607160,"node_id":"MDQ6VXNlcjk2MDcxNjA=","avatar_url":"https://avatars1.githubusercontent.com/u/9607160?v=4","gravatar_id":"","url":"https://api.github.com/users/sldr","html_url":"https://github.com/sldr","followers_url":"https://api.github.com/users/sldr/followers","following_url":"https://api.github.com/users/sldr/following{/other_user}","gists_url":"https://api.github.com/users/sldr/gists{/gist_id}","starred_url":"https://api.github.com/users/sldr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sldr/subscriptions","organizations_url":"https://api.github.com/users/sldr/orgs","repos_url":"https://api.github.com/users/sldr/repos","events_url":"https://api.github.com/users/sldr/events{/privacy}","received_events_url":"https://api.github.com/users/sldr/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-01-14T03:39:26Z","updated_at":"2018-08-06T12:33:34Z","closed_at":null,"author_association":"NONE","body":"In the following code, reading the content requires catching std::system_error then checking for std::net::error::eof in the code() call. It would be nice if most (if not all) errors could be caught, but at least the most common cross platform errors. Note that switch (e.code()) will not work and switch (e.code().value()) doesn't work because the category will need to be checked too.\r\n\r\n```c++\r\n#include <string>\r\n#include <iostream>\r\n#include <experimental/net>\r\n#include <experimental/buffer>\r\n#include <experimental/internet>\r\n\r\nnamespace std {\r\n\tnamespace net = std::experimental::net; // Put std::experimental::net into std::net namespace (I assume this is where it will go when approved)\r\n}\r\nusing namespace std::string_literals; // Give me \"SLDR\"s -> std::string\r\n\r\nint main()\r\n{\r\n\tstd::net::io_context myiocontext;\r\n\tstd::net::ip::tcp::resolver myresolver = std::net::ip::tcp::resolver(myiocontext);\r\n\tstd::net::ip::tcp::socket mysocket = std::net::ip::tcp::socket(myiocontext);\r\n\tstd::net::connect(mysocket, myresolver.resolve(\"httpbin.org\", \"http\"));\r\n\tfor (auto r : {\r\n\t\t\t\"GET /get?sldr=22 HTTP/1.0\\r\\n\"s,\r\n\t\t\t\"Host: httpbin.org\\r\\n\"s,\r\n\t\t\t\"Accept: */*\\r\\n\"s,\r\n\t\t\t\"User-Agent: Nonya\\r\\n\"s,\r\n\t\t\t\"Connection: close\\r\\n\\r\\n\"s }) {\r\n\t\tstd::net::write(mysocket, std::net::buffer(r));\r\n\t}\r\n\t// Handle http headers\r\n\tstd::string headers;\r\n\tstd::net::read(mysocket, std::net::dynamic_buffer(headers),\r\n\t\t[&headers](auto ec, auto n) -> std::size_t {\r\n\t\tif (ec || (headers.size() > 3 && headers.compare(headers.size() - 4, 4, \"\\r\\n\\r\\n\") == 0)) {\r\n\t\t\treturn 0; // don't read anymore characters\r\n\t\t}\r\n\t\telse {\r\n\t\t\treturn 1; // read 1 more character return 2; // read 2 more characters\r\n\t\t}\r\n\t\t});\r\n\tstd::cout << headers;\r\n\t// Handle http content\r\n\tstd::string content;\r\n\ttry {\r\n\t\tbool repeat = false;\r\n\t\tint repeat_times = 3;\r\n\t\tdo {\r\n\t\t\ttry {\r\n\t\t\t\twhile (0 < std::net::read(mysocket, std::net::dynamic_buffer(content))) {\r\n\t\t\t\t\tstd::cout << content;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tcatch (std::system_error e) {\r\n\t\t\t\tif (e.code() != std::net::error::try_again) {\r\n\t\t\t\t\tthrow;\r\n\t\t\t\t}\r\n\t\t\t\tstd::cout << content;\r\n\t\t\t\trepeat = true;\r\n\t\t\t}\r\n\t\t\tcontent.clear();\r\n\t\t\trepeat_times--;\r\n\t\t} while (repeat && repeat_times > 0);\r\n\t}\r\n\tcatch (std::system_error e) {\r\n\t\tif (e.code() != std::net::error::eof && e.code() != std::net::error::connection_reset) {\r\n\t\t\tthrow;\r\n\t\t}\r\n\t\tstd::cout << content;\r\n\t}\r\n\treturn 0;\r\n}\r\n```\r\nAllowing something like the following would make the code simpler for catching errors for the users that prefer the exception method of dealing with the errors.\r\n```c++\r\n\t// Handle http content\r\n\tstd::string content;\r\n\ttry {\r\n\t\tbool repeat = false;\r\n\t\tint repeat_times = 3;\r\n\t\tdo {\r\n\t\t\ttry {\r\n\t\t\t\twhile (0 < std::net::read(mysocket, std::net::dynamic_buffer(content))) {\r\n\t\t\t\t\tstd::cout << content;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tcatch (std::net::error::try_again_error) {\r\n\t\t\t\tstd::cout << content;\r\n\t\t\t\trepeat = true;\r\n\t\t\t}\r\n\t\t\trepeat_times--;\r\n\t\t} while (repeat && repeat_times > 0);\r\n\t}\r\n\tcatch (std::net::error::eof_error) {\r\n\t\tstd::cout << content;\r\n\t}\r\n\tcatch (std::net::error::connection_reset_error) {\r\n\t\tstd::cout << content;\r\n\t}\r\n\treturn 0;\r\n```\r\nNote that the `std::net::error::eof_error` could be a nested exception with the currently thrown `std::system_error` exception being the internal exception. It could be noted that the std::system_error exception can always be thrown because unexpected platform specific errors could happen that don't map to any `std::net::error::*_error` exception. One down side is compiler venders (and/or stl coders) will have to decide what errors map to the common exceptions but the standard-ease wording could try to keep them from making to poor of a choice that would make users have to resort to writing code like the first example.\r\n\r\nThanks,\r\nSLDR\r\n(Stephen L. De Rudder)"},"comment":{"url":"https://api.github.com/repos/chriskohlhoff/networking-ts-impl/issues/comments/410692402","html_url":"https://github.com/chriskohlhoff/networking-ts-impl/issues/13#issuecomment-410692402","issue_url":"https://api.github.com/repos/chriskohlhoff/networking-ts-impl/issues/13","id":410692402,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMDY5MjQwMg==","user":{"login":"FlorianFranzen","id":781077,"node_id":"MDQ6VXNlcjc4MTA3Nw==","avatar_url":"https://avatars0.githubusercontent.com/u/781077?v=4","gravatar_id":"","url":"https://api.github.com/users/FlorianFranzen","html_url":"https://github.com/FlorianFranzen","followers_url":"https://api.github.com/users/FlorianFranzen/followers","following_url":"https://api.github.com/users/FlorianFranzen/following{/other_user}","gists_url":"https://api.github.com/users/FlorianFranzen/gists{/gist_id}","starred_url":"https://api.github.com/users/FlorianFranzen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/FlorianFranzen/subscriptions","organizations_url":"https://api.github.com/users/FlorianFranzen/orgs","repos_url":"https://api.github.com/users/FlorianFranzen/repos","events_url":"https://api.github.com/users/FlorianFranzen/events{/privacy}","received_events_url":"https://api.github.com/users/FlorianFranzen/received_events","type":"User","site_admin":false},"created_at":"2018-08-06T12:33:34Z","updated_at":"2018-08-06T12:33:34Z","author_association":"NONE","body":"@sldr: Sounds more like something that should be added to the standard itself (there is a email address in it): http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4734.pdf "}}
{"actor":{"display_login":"FlorianFranzen"}}