mirror of https://github.com/Shizmob/pydle.git
Handle socket error conditions more gracefully.
This commit is contained in:
parent
37dd569f26
commit
cd55afad60
|
@ -341,8 +341,9 @@ class BasicClient:
|
||||||
message = self._parse_message()
|
message = self._parse_message()
|
||||||
self.on_raw(message)
|
self.on_raw(message)
|
||||||
|
|
||||||
def on_data_error(self):
|
def on_data_error(self, exception):
|
||||||
""" Handle error. """
|
""" Handle error. """
|
||||||
|
self.logger.error('Encountered error on socket. Reconnecting.', exc_info=(type(exception), exception, None))
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
def on_raw(self, message):
|
def on_raw(self, message):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
import os.path as path
|
import os.path as path
|
||||||
import collections
|
import collections
|
||||||
import time
|
import time
|
||||||
|
@ -276,7 +277,14 @@ class Connection:
|
||||||
## Lower-level data-related methods.
|
## Lower-level data-related methods.
|
||||||
|
|
||||||
def on(self, method, callback):
|
def on(self, method, callback):
|
||||||
""" Add callback for event. """
|
"""
|
||||||
|
Add callback for event.
|
||||||
|
|
||||||
|
Handlers are called as follows:
|
||||||
|
- read: Called with the read data.
|
||||||
|
- write: Called with the a list of the written messages.
|
||||||
|
- error: Called with the exception that occurred.
|
||||||
|
"""
|
||||||
if method not in self.handlers:
|
if method not in self.handlers:
|
||||||
raise ValueError('Given method must be one of: {}'.format(', '.join(self.handlers)))
|
raise ValueError('Given method must be one of: {}'.format(', '.join(self.handlers)))
|
||||||
self.handlers[method].append(callback)
|
self.handlers[method].append(callback)
|
||||||
|
@ -386,5 +394,13 @@ class Connection:
|
||||||
self.update_write_handler()
|
self.update_write_handler()
|
||||||
|
|
||||||
def _on_error(self, fd):
|
def _on_error(self, fd):
|
||||||
|
# Get native error and create exception from it.
|
||||||
|
errno = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
|
||||||
|
try:
|
||||||
|
message = os.strerror(errno)
|
||||||
|
except ValueError:
|
||||||
|
message = 'Unknown error'
|
||||||
|
exception = IOError(errno, message)
|
||||||
|
|
||||||
for handler in self.handlers['error']:
|
for handler in self.handlers['error']:
|
||||||
handler()
|
handler(exception)
|
||||||
|
|
Loading…
Reference in New Issue