THRIFT-1243 TAsyncChannel callbacks
improved exception handling
Patch: Alexandre Parenteau

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1167679 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/async/TEvhttpServer.cpp b/lib/cpp/src/async/TEvhttpServer.cpp
index 701f8bd..b92422c 100755
--- a/lib/cpp/src/async/TEvhttpServer.cpp
+++ b/lib/cpp/src/async/TEvhttpServer.cpp
@@ -22,6 +22,8 @@
 #include "transport/TBufferTransports.h"
 #include <evhttp.h>
 
+#include <iostream>
+
 #ifndef HTTP_INTERNAL // libevent < 2
 #define HTTP_INTERNAL 500
 #endif
@@ -55,12 +57,12 @@
   // Create event_base and evhttp.
   eb_ = event_base_new();
   if (eb_ == NULL) {
-    abort();  // XXX
+    throw TException("event_base_new failed");
   }
   eh_ = evhttp_new(eb_);
   if (eh_ == NULL) {
     event_base_free(eb_);
-    abort();  // XXX
+    throw TException("evhttp_new failed");
   }
 
   // Bind to port.
@@ -68,6 +70,7 @@
   if (ret < 0) {
     evhttp_free(eh_);
     event_base_free(eb_);
+	throw TException("evhttp_bind_socket failed");
   }
 
   // Register a handler.  If you use the other constructor,
@@ -89,7 +92,7 @@
 
 int TEvhttpServer::serve() {
   if (eb_ == NULL) {
-    abort();  // XXX
+    throw TException("Unexpected call to TEvhttpServer::serve");
   }
   return event_base_dispatch(eb_);
 }
@@ -127,17 +130,19 @@
   (void) success;
   std::auto_ptr<RequestContext> ptr(ctx);
 
-  int code = 200;
-  const char* reason = "OK";
+  int code = success ? 200 : 400;
+  const char* reason = success ? "OK" : "Bad Request";
 
   int rv = evhttp_add_header(ctx->req->output_headers, "Content-Type", "application/x-thrift");
   if (rv != 0) {
     // TODO: Log an error.
+    std::cerr << "evhttp_add_header failed " << __FILE__ << ":" << __LINE__ << std::endl;
   }
 
   struct evbuffer* buf = evbuffer_new();
   if (buf == NULL) {
     // TODO: Log an error.
+      std::cerr << "evbuffer_new failed " << __FILE__ << ":" <<  __LINE__ << std::endl;
   } else {
     uint8_t* obuf;
     uint32_t sz;
@@ -145,6 +150,7 @@
     int ret = evbuffer_add(buf, obuf, sz);
     if (ret != 0) {
       // TODO: Log an error.
+      std::cerr << "evhttp_add failed with " << ret << " " << __FILE__ << ":" <<  __LINE__ << std::endl;
     }
   }