From 1e9875878aee4218fbbc8fbc768e86b25c8d9ef5 Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Thu, 25 Aug 2011 17:33:03 +0000 Subject: [PATCH] THRIFT-1293. cpp: improve handling of exceptions thrown by process() Patch: Adam Simpkins git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1161660 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/src/server/TNonblockingServer.cpp | 25 +++++++++++++---------- lib/cpp/src/server/TSimpleServer.cpp | 8 ++++---- lib/cpp/src/server/TThreadPoolServer.cpp | 11 ++++------ lib/cpp/src/server/TThreadedServer.cpp | 8 ++++---- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/cpp/src/server/TNonblockingServer.cpp b/lib/cpp/src/server/TNonblockingServer.cpp index 681cbdb5..a0810770 100644 --- a/lib/cpp/src/server/TNonblockingServer.cpp +++ b/lib/cpp/src/server/TNonblockingServer.cpp @@ -78,15 +78,16 @@ class TConnection::Task: public Runnable { break; } } - } catch (TTransportException& ttx) { - cerr << "TNonblockingServer client died: " << ttx.what() << endl; - } catch (TException& x) { - cerr << "TNonblockingServer exception: " << x.what() << endl; - } catch (bad_alloc&) { - cerr << "TNonblockingServer caught bad_alloc exception."; + } catch (const TTransportException& ttx) { + GlobalOutput.printf("TNonblockingServer client died: %s", ttx.what()); + } catch (const bad_alloc&) { + GlobalOutput("TNonblockingServer caught bad_alloc exception."); exit(-1); + } catch (const std::exception& x) { + GlobalOutput.printf("TNonblockingServer process() exception: %s: %s", + typeid(x).name(), x.what()); } catch (...) { - cerr << "TNonblockingServer uncaught exception." << endl; + GlobalOutput("TNonblockingServer uncaught exception."); } // Signal completion back to the libevent thread via a pipe @@ -320,13 +321,15 @@ void TConnection::transition() { try { // Invoke the processor server_->getProcessor()->process(inputProtocol_, outputProtocol_, NULL); - } catch (TTransportException &ttx) { - GlobalOutput.printf("TTransportException: Server::process() %s", ttx.what()); + } catch (const TTransportException &ttx) { + GlobalOutput.printf("TNonblockingServer transport error in " + "process(): %s", ttx.what()); server_->decrementActiveProcessors(); close(); return; - } catch (TException &x) { - GlobalOutput.printf("TException: Server::process() %s", x.what()); + } catch (const std::exception &x) { + GlobalOutput.printf("Server::process() uncaught exception: %s: %s", + typeid(x).name(), x.what()); server_->decrementActiveProcessors(); close(); return; diff --git a/lib/cpp/src/server/TSimpleServer.cpp b/lib/cpp/src/server/TSimpleServer.cpp index c0fd67c0..0e5a967e 100644 --- a/lib/cpp/src/server/TSimpleServer.cpp +++ b/lib/cpp/src/server/TSimpleServer.cpp @@ -102,12 +102,12 @@ void TSimpleServer::serve() { break; } } - } catch (TTransportException& ttx) { + } catch (const TTransportException& ttx) { string errStr = string("TSimpleServer client died: ") + ttx.what(); GlobalOutput(errStr.c_str()); - } catch (TException& tx) { - string errStr = string("TSimpleServer exception: ") + tx.what(); - GlobalOutput(errStr.c_str()); + } catch (const std::exception& x) { + GlobalOutput.printf("TSimpleServer exception: %s: %s", + typeid(x).name(), x.what()); } catch (...) { GlobalOutput("TSimpleServer uncaught exception."); } diff --git a/lib/cpp/src/server/TThreadPoolServer.cpp b/lib/cpp/src/server/TThreadPoolServer.cpp index 840b8359..22040764 100644 --- a/lib/cpp/src/server/TThreadPoolServer.cpp +++ b/lib/cpp/src/server/TThreadPoolServer.cpp @@ -68,17 +68,14 @@ public: break; } } - } catch (TTransportException& ttx) { + } catch (const TTransportException& ttx) { // This is reasonably expected, client didn't send a full request so just // ignore him // string errStr = string("TThreadPoolServer client died: ") + ttx.what(); // GlobalOutput(errStr.c_str()); - } catch (TException& x) { - string errStr = string("TThreadPoolServer exception: ") + x.what(); - GlobalOutput(errStr.c_str()); - } catch (std::exception &x) { - string errStr = string("TThreadPoolServer, std::exception: ") + x.what(); - GlobalOutput(errStr.c_str()); + } catch (const std::exception& x) { + GlobalOutput.printf("TThreadPoolServer exception %s: %s", + typeid(x).name(), x.what()); } catch (...) { GlobalOutput("TThreadPoolServer, unexpected exception in " "TThreadPoolServer::Task::run()"); diff --git a/lib/cpp/src/server/TThreadedServer.cpp b/lib/cpp/src/server/TThreadedServer.cpp index b1ef1e02..feeb0e94 100644 --- a/lib/cpp/src/server/TThreadedServer.cpp +++ b/lib/cpp/src/server/TThreadedServer.cpp @@ -70,14 +70,14 @@ public: break; } } - } catch (TTransportException& ttx) { + } catch (const TTransportException& ttx) { if (ttx.getType() != TTransportException::END_OF_FILE) { string errStr = string("TThreadedServer client died: ") + ttx.what(); GlobalOutput(errStr.c_str()); } - } catch (TException& x) { - string errStr = string("TThreadedServer exception: ") + x.what(); - GlobalOutput(errStr.c_str()); + } catch (const std::exception &x) { + GlobalOutput.printf("TThreadedServer exception: %s: %s", + typeid(x).name(), x.what()); } catch (...) { GlobalOutput("TThreadedServer uncaught exception."); } -- 2.17.1