Fix error handling in server examples

This commit is contained in:
Evgeniy authored and Vinnie Falco committed 2016-09-29 13:03:33 -04:00
1 parent 4c7065a80a
commit 2a5b116530
2 files changed
+30 -23

No files matched your search

+14 -11
View File
@@ -249,20 +249,23 @@ private:
asio::placeholders::error));
return;
}
resp_type res;
res.status = 200;
res.reason = "OK";
res.version = req_.version;
res.headers.insert("Server", "http_async_server");
res.headers.insert("Content-Type", mime_type(path));
res.body = path;
try
{
resp_type res;
res.status = 200;
res.reason = "OK";
res.version = req_.version;
res.headers.insert("Server", "http_async_server");
res.headers.insert("Content-Type", mime_type(path));
res.body = path;
prepare(res);
async_write(sock_, std::move(res),
std::bind(&peer::on_write, shared_from_this(),
asio::placeholders::error));
}
catch(std::exception const& e)
{
res = {};
response_v1<string_body> res;
res.status = 500;
res.reason = "Internal Error";
res.version = req_.version;
@@ -271,10 +274,10 @@ private:
res.body =
std::string{"An internal error occurred"} + e.what();
prepare(res);
async_write(sock_, std::move(res),
std::bind(&peer::on_write, shared_from_this(),
asio::placeholders::error));
}
async_write(sock_, std::move(res),
std::bind(&peer::on_write, shared_from_this(),
asio::placeholders::error));
}
void on_write(error_code ec)
+16 -12
View File
@@ -172,33 +172,37 @@ private:
write(sock, res, ec);
if(ec)
break;
return;
}
resp_type res;
res.status = 200;
res.reason = "OK";
res.version = req.version;
res.headers.insert("Server", "http_sync_server");
res.headers.insert("Content-Type", mime_type(path));
res.body = path;
try
{
resp_type res;
res.status = 200;
res.reason = "OK";
res.version = req.version;
res.headers.insert("Server", "http_sync_server");
res.headers.insert("Content-Type", mime_type(path));
res.body = path;
prepare(res);
write(sock, res, ec);
if(ec)
break;
}
catch(std::exception const& e)
{
res = {};
response_v1<string_body> res;
res.status = 500;
res.reason = "Internal Error";
res.version = req.version;
res.headers.insert("Server", "http_sync_server");
res.headers.insert("Content-Type", "text/html");
res.body =
std::string{"An internal error occurred"} + e.what();
std::string{"An internal error occurred: "} + e.what();
prepare(res);
write(sock, res, ec);
if(ec)
break;
}
write(sock, res, ec);
if(ec)
break;
}
fail(id, ec);
}