Make httpget handle socket errors
authorMikko Rasa <tdb@tdb.fi>
Wed, 10 Aug 2011 19:31:26 +0000 (22:31 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 10 Aug 2011 19:31:26 +0000 (22:31 +0300)
examples/httpget.cpp

index 70942b7647ffb0bcbdff535e902af0305eadb2ba..e0c051c946925b91bd7e0984e3d038aaf368bdc8 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/core/application.h>
 #include <msp/core/getopt.h>
+#include <msp/debug/demangle.h>
 #include <msp/http/client.h>
 #include <msp/http/request.h>
 #include <msp/http/response.h>
@@ -18,6 +19,8 @@ public:
        HttpGet(int, char **);
 
        virtual int main();
+private:
+       void socket_error(const exception &);
 };
 
 HttpGet::HttpGet(int argc, char **argv):
@@ -37,6 +40,7 @@ HttpGet::HttpGet(int argc, char **argv):
 int HttpGet::main()
 {
        Http::Client client;
+       client.signal_socket_error.connect(sigc::mem_fun(this, &HttpGet::socket_error));
 
        if(verbose)
        {
@@ -45,8 +49,11 @@ int HttpGet::main()
                IO::print(client.get_request()->str());
                client.wait_response();
                const Http::Response *response = client.get_response();
-               IO::print("=== Got response ===\n");
-               IO::print(response->str());
+               if(response)
+               {
+                       IO::print("=== Got response ===\n");
+                       IO::print(response->str());
+               }
        }
        else
        {
@@ -56,3 +63,10 @@ int HttpGet::main()
 
        return 1;
 }
+
+void HttpGet::socket_error(const exception &e)
+{
+       IO::print("=== Error ===\n");
+       IO::print("%s\n", Debug::demangle(typeid(e).name()));
+       IO::print("%s\n", e.what());
+}