From d20870655aeca0614d9fc315fe5f9893ebdc7aa3 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 26 Sep 2019 23:55:01 +0300 Subject: [PATCH] Improve error handling Particularly of asynchronous operations --- source/http/client.cpp | 28 +++++++++++++++++++++++----- source/http/server.cpp | 30 ++++++++++++++++++++++++------ source/net/resolve.cpp | 9 +++++++++ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/source/http/client.cpp b/source/http/client.cpp index da6b25e..e090576 100644 --- a/source/http/client.cpp +++ b/source/http/client.cpp @@ -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; } diff --git a/source/http/server.cpp b/source/http/server.cpp index 7caa0f5..f985bf9 100644 --- a/source/http/server.cpp +++ b/source/http/server.cpp @@ -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; 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) { diff --git a/source/net/resolve.cpp b/source/net/resolve.cpp index 10e8c65..006f210 100644 --- a/source/net/resolve.cpp +++ b/source/net/resolve.cpp @@ -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 err = task->error; + task->error = 0; + thread.pop_complete_task(); + throw *err; + } signal_resolve_failed.emit(task->tag, *task->error); + } thread.pop_complete_task(); } } -- 2.45.2