feature: add exception handler for http server

This commit is contained in:
Jørgen Lien Sellæg 2016-09-01 17:06:19 +02:00
commit d490c3ff4e

View file

@ -80,6 +80,7 @@ namespace SimpleWeb {
std::string remote_endpoint_address; std::string remote_endpoint_address;
unsigned short remote_endpoint_port; unsigned short remote_endpoint_port;
std::function<void(const std::exception&)> exception_handler;
private: private:
Request(): content(streambuf) {} Request(): content(streambuf) {}
@ -90,7 +91,10 @@ namespace SimpleWeb {
remote_endpoint_address=socket.lowest_layer().remote_endpoint().address().to_string(); remote_endpoint_address=socket.lowest_layer().remote_endpoint().address().to_string();
remote_endpoint_port=socket.lowest_layer().remote_endpoint().port(); remote_endpoint_port=socket.lowest_layer().remote_endpoint().port();
} }
catch(const std::exception&) {} catch(const std::exception&e) {
if(exception_handler)
exception_handler(e);
}
} }
}; };
@ -186,6 +190,7 @@ namespace SimpleWeb {
callback(ec); callback(ec);
}); });
} }
std::function<void(const std::exception&)> exception_handler;
protected: protected:
boost::asio::io_service io_service; boost::asio::io_service io_service;
@ -250,7 +255,9 @@ namespace SimpleWeb {
try { try {
content_length=stoull(it->second); content_length=stoull(it->second);
} }
catch(const std::exception &) { catch(const std::exception &e) {
if(exception_handler)
exception_handler(e);
return; return;
} }
if(content_length>num_additional_bytes) { if(content_length>num_additional_bytes) {
@ -356,7 +363,9 @@ namespace SimpleWeb {
try { try {
http_version=stof(request->http_version); http_version=stof(request->http_version);
} }
catch(const std::exception &) { catch(const std::exception &e){
if(exception_handler)
exception_handler(e);
return; return;
} }
@ -374,7 +383,9 @@ namespace SimpleWeb {
try { try {
resource_function(response, request); resource_function(response, request);
} }
catch(const std::exception&) { catch(const std::exception&e) {
if(exception_handler)
exception_handler(e);
return; return;
} }
} }