]> git.tdb.fi Git - libs/net.git/commitdiff
Improve error handling
authorMikko Rasa <tdb@tdb.fi>
Thu, 26 Sep 2019 20:55:01 +0000 (23:55 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 27 Sep 2019 00:13:06 +0000 (03:13 +0300)
Particularly of asynchronous operations

source/http/client.cpp
source/http/server.cpp
source/net/resolve.cpp

index da6b25ebc17ef4c6fc0234704e4b80ad73f40b84..e09057684957cac17acdeb17adeaf272efdc1952 100644 (file)
@@ -141,23 +141,39 @@ void Client::resolve_failed(unsigned tag, const exception &err)
                return;
        resolve_tag = 0;
 
-       signal_socket_error.emit(err);
-
        delete request;
        request = 0;
+
+       if(signal_socket_error.empty())
+               throw err;
+       signal_socket_error.emit(err);
 }
 
 void Client::connect_finished(const exception *err)
 {
        if(err)
        {
-               signal_socket_error.emit(*err);
-
                delete request;
                request = 0;
+
+               if(signal_socket_error.empty())
+                       throw *err;
+               signal_socket_error.emit(*err);
        }
        else
-               sock->write(request->str());
+       {
+               try
+               {
+                       sock->write(request->str());
+               }
+               catch(const exception &e)
+               {
+                       if(signal_socket_error.empty())
+                               throw;
+                       signal_socket_error.emit(e);
+                       return;
+               }
+       }
 }
 
 void Client::data_available()
@@ -170,6 +186,8 @@ void Client::data_available()
        }
        catch(const exception &e)
        {
+               if(signal_socket_error.empty())
+                       throw;
                signal_socket_error.emit(e);
                return;
        }
index 7caa0f5d025b6eb0e55db15f55cbf896aadf40ba..f985bf994526bf871844b97cfd263f2ee56ee2ef 100644 (file)
@@ -92,11 +92,19 @@ void Server::client_data_available(Client &cl)
                        break;
                }
 
-       char rbuf[4096];
-       unsigned len = cl.sock->read(rbuf, sizeof(rbuf));
-       if(cl.stale)
+       try
+       {
+               char rbuf[4096];
+               unsigned len = cl.sock->read(rbuf, sizeof(rbuf));
+               if(cl.stale)
+                       return;
+               cl.in_buf.append(rbuf, len);
+       }
+       catch(const exception &)
+       {
+               cl.stale = true;
                return;
-       cl.in_buf.append(rbuf, len);
+       }
 
        RefPtr<Response> response;
        if(!cl.request)
@@ -133,7 +141,7 @@ void Server::client_data_available(Client &cl)
        }
        else
        {
-               len = cl.request->parse_content(cl.in_buf);
+               unsigned len = cl.request->parse_content(cl.in_buf);
                cl.in_buf.erase(0, len);
        }
 
@@ -180,7 +188,17 @@ void Server::send_response(Client &cl, Response &resp)
 {
        if(cl.keepalive)
                resp.set_header("Connection", "keep-alive");
-       cl.sock->write(resp.str());
+
+       try
+       {
+               cl.sock->write(resp.str());
+       }
+       catch(const exception &)
+       {
+               cl.stale = true;
+               return;
+       }
+
        cl.async = false;
        if(cl.keepalive)
        {
index 10e8c656797fedfdd685b2edb48ad5f20a217996..006f210d777eb2aa38d7e5df91ecc1c25ca20c0d 100644 (file)
@@ -132,7 +132,16 @@ void Resolver::task_done()
                if(task->addr)
                        signal_address_resolved.emit(task->tag, *task->addr);
                else if(task->error)
+               {
+                       if(signal_resolve_failed.empty())
+                       {
+                               RefPtr<runtime_error> err = task->error;
+                               task->error = 0;
+                               thread.pop_complete_task();
+                               throw *err;
+                       }
                        signal_resolve_failed.emit(task->tag, *task->error);
+               }
                thread.pop_complete_task();
        }
 }