Skip to content

Commit

Permalink
Fix HBPVIS#224: Handle exceptions in server handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Eilemann authored and Stefan Eilemann committed Jul 28, 2017
1 parent 92178a5 commit 9b7a94f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
28 changes: 28 additions & 0 deletions tests/http/server.cpp
Expand Up @@ -79,6 +79,10 @@ const Response response204{ServerReponse::no_content, ""};
const Response error400{ServerReponse::bad_request, ""};
const Response error404{ServerReponse::not_found, ""};
const Response error405{ServerReponse::method_not_allowed, ""};
Response error500(const std::string& body)
{
return Response(ServerReponse::internal_server_error, body);
}

const std::string jsonGet("Not JSON, just want to see that the is data a-ok");
const std::string jsonPut("See what my stepbrother jsonGet says");
Expand Down Expand Up @@ -977,6 +981,30 @@ BOOST_AUTO_TEST_CASE(issue157)
thread.join();
}

BOOST_AUTO_TEST_CASE(issue224) // robustness when handlers throw
{
bool running = true;
zeroeq::http::Server server;

server.handleGET("test/foo", [&]() {
throw std::runtime_error("I've had enough!");
return jsonGet;
});

std::thread thread([&]() {
while (running)
server.receive(TIMEOUT);
});

Client client(server.getURI());
client.checkGET("/test/foo",
error500("Request handler exception: I've had enough!"),
__LINE__);

running = false;
thread.join();
}

BOOST_AUTO_TEST_CASE(urlcasesensitivity)
{
bool running = true;
Expand Down
18 changes: 17 additions & 1 deletion zeroeq/http/server.cpp
Expand Up @@ -659,7 +659,23 @@ bool Server::process(detail::Socket&)
}
else
{
message->response = respondTo(message->request);
try
{
message->response = respondTo(message->request);
}
catch (const std::exception& e)
{
message->response =
make_ready_response(Code::INTERNAL_SERVER_ERROR,
std::string("Request handler exception: ") +
e.what());
}
catch (...)
{
message->response =
make_ready_response(Code::INTERNAL_SERVER_ERROR,
"An unknown exception occured");
}

// When a client makes a CORS request (by setting an 'Origin' header) it
// expects an 'Access-Control-Allow-Origin' response header. See:
Expand Down

0 comments on commit 9b7a94f

Please sign in to comment.