From a89b532e752e6c5ad09b0df0b541ffc1024b5de0 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Fri, 13 Nov 2015 13:57:41 +0100
Subject: [PATCH] Fix errors due to bad connection quality

---
 src/cutecoin/core/net/node.py             | 25 +++++++++++++++++++++--
 src/cutecoin/gui/process_cfg_community.py | 11 ++++++----
 src/cutecoin/main.py                      |  3 ++-
 3 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/src/cutecoin/core/net/node.py b/src/cutecoin/core/net/node.py
index 359d6bf2..e57e96cc 100644
--- a/src/cutecoin/core/net/node.py
+++ b/src/cutecoin/core/net/node.py
@@ -11,7 +11,7 @@ from ucoinpy.api import bma as bma
 from ucoinpy.api.bma import ConnectionHandler
 
 import asyncio
-from aiohttp.errors import ClientError
+from aiohttp.errors import ClientError, DisconnectedError
 import logging
 import time
 import jsonschema
@@ -21,7 +21,7 @@ from PyQt5.QtCore import QObject, pyqtSignal
 
 class Node(QObject):
     """
-    A node is a peer seend from the client point of view.
+    A node is a peer send from the client point of view.
     This node can have multiple states :
     - ONLINE : The node is available for requests
     - OFFLINE: The node is disconnected
@@ -317,6 +317,9 @@ class Node(QObject):
                 except ClientError:
                     logging.debug("Client error : {0}".format(self.pubkey))
                     self.state = Node.OFFLINE
+                except DisconnectedError:
+                    logging.debug("Disconnected error : {0}".format(self.pubkey))
+                    self.state = Node.OFFLINE
                 except asyncio.TimeoutError:
                     logging.debug("Timeout error : {0}".format(self.pubkey))
                     self.state = Node.OFFLINE
@@ -338,6 +341,9 @@ class Node(QObject):
         except ClientError:
             logging.debug("Client error : {0}".format(self.pubkey))
             self.state = Node.OFFLINE
+        except DisconnectedError:
+            logging.debug("Disconnected error : {0}".format(self.pubkey))
+            self.state = Node.OFFLINE
         except asyncio.TimeoutError:
             logging.debug("Timeout error : {0}".format(self.pubkey))
             self.state = Node.OFFLINE
@@ -375,6 +381,9 @@ class Node(QObject):
         except asyncio.TimeoutError:
             logging.debug("Timeout error : {0}".format(self.pubkey))
             self.state = Node.OFFLINE
+        except DisconnectedError:
+            logging.debug("Disconnected error : {0}".format(self.pubkey))
+            self.state = Node.OFFLINE
         except jsonschema.ValidationError:
             logging.debug("Validation error : {0}".format(self.pubkey))
             self.state = Node.CORRUPTED
@@ -405,6 +414,9 @@ class Node(QObject):
         except asyncio.TimeoutError:
             logging.debug("Timeout error : {0}".format(self.pubkey))
             self.state = Node.OFFLINE
+        except DisconnectedError:
+            logging.debug("Disconnected error : {0}".format(self.pubkey))
+            self.state = Node.OFFLINE
         except jsonschema.ValidationError:
             logging.debug("Validation error : {0}".format(self.pubkey))
             self.state = Node.CORRUPTED
@@ -443,6 +455,9 @@ class Node(QObject):
         except asyncio.TimeoutError:
             logging.debug("Timeout error : {0}".format(self.pubkey))
             self.state = Node.OFFLINE
+        except DisconnectedError:
+            logging.debug("Disconnected error : {0}".format(self.pubkey))
+            self.state = Node.OFFLINE
         except jsonschema.ValidationError:
             logging.debug("Validation error : {0}".format(self.pubkey))
             self.state = Node.CORRUPTED
@@ -480,6 +495,9 @@ class Node(QObject):
                     except asyncio.TimeoutError:
                         logging.debug("Timeout error : {0}".format(self.pubkey))
                         self.state = Node.OFFLINE
+                    except DisconnectedError:
+                        logging.debug("Disconnected error : {0}".format(self.pubkey))
+                        self.state = Node.OFFLINE
                     except jsonschema.ValidationError:
                         logging.debug("Validation error : {0}".format(self.pubkey))
                         self.state = Node.CORRUPTED
@@ -494,6 +512,9 @@ class Node(QObject):
         except asyncio.TimeoutError:
             logging.debug("Timeout error : {0}".format(self.pubkey))
             self.state = Node.OFFLINE
+        except DisconnectedError:
+            logging.debug("Disconnected error : {0}".format(self.pubkey))
+            self.state = Node.OFFLINE
         except jsonschema.ValidationError:
             logging.debug("Validation error : {0}".format(self.pubkey))
             self.state = Node.CORRUPTED
diff --git a/src/cutecoin/gui/process_cfg_community.py b/src/cutecoin/gui/process_cfg_community.py
index 6e925d0c..0fac2659 100644
--- a/src/cutecoin/gui/process_cfg_community.py
+++ b/src/cutecoin/gui/process_cfg_community.py
@@ -6,6 +6,7 @@ Created on 8 mars 2014
 
 import logging
 import asyncio
+import aiohttp
 
 from PyQt5.QtWidgets import QDialog, QMenu, QMessageBox, QApplication
 from PyQt5.QtGui import QCursor
@@ -88,8 +89,8 @@ Yours : {0}, the network : {1}""".format(registered[1], registered[2])))
         server = self.config_dialog.lineedit_server.text()
         port = self.config_dialog.spinbox_port.value()
         logging.debug("Is valid ? ")
-        self.node = yield from Node.from_address(None, server, port)
-        if self.node:
+        try:
+            self.node = yield from Node.from_address(None, server, port)
             community = Community.create(self.node)
             self.config_dialog.button_connect.setEnabled(False)
             self.config_dialog.button_register.setEnabled(False)
@@ -119,8 +120,10 @@ Yours : {0}, the network : {1}""".format(registered[1], registered[2])))
 Yours : {0}, the network : {1}""".format(registered[1], registered[2])))
             else:
                 self.config_dialog.label_error.setText(self.tr("Your account already exists on the network"))
-        else:
-            self.config_dialog.label_error.setText(self.tr("Could not connect."))
+        except aiohttp.errors.DisconnectedError as e:
+            self.config_dialog.label_error.setText(str(e))
+        except aiohttp.errors.ClientError as e:
+            self.config_dialog.label_error.setText(str(e))
 
     @pyqtSlot()
     def check_register(self):
diff --git a/src/cutecoin/main.py b/src/cutecoin/main.py
index 5145e7bd..962f95cb 100755
--- a/src/cutecoin/main.py
+++ b/src/cutecoin/main.py
@@ -41,7 +41,8 @@ def async_exception_handler(loop, context):
         log_lines.append('{}: {!r}'.format(key, context[key]))
 
     logging.error('\n'.join(log_lines), exc_info=exc_info)
-    if "Unclosed" not in message:
+    if "Unclosed" not in message and \
+        "gaierror(-2" not in message:
         os._exit(1)
 
 
-- 
GitLab