Skip to content

Simplify exception handlers #1319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions benchmarks/command_packer_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ def send_packed_command(self, command):
self.connect()
try:
self._sock.sendall(command)
except socket.error:
e = sys.exc_info()[1]
except socket.error as e:
self.disconnect()
if len(e.args) == 1:
_errno, errmsg = 'UNKNOWN', e.args[0]
else:
_errno, errmsg = e.args
raise ConnectionError("Error %s while writing to socket. %s." %
(_errno, errmsg))
except Exception as e:
except Exception:
self.disconnect()
raise e
raise

def pack_command(self, *args):
"Pack a series of arguments into a value Redis command"
Expand All @@ -46,18 +45,17 @@ def send_packed_command(self, command):
command = [command]
for item in command:
self._sock.sendall(item)
except socket.error:
e = sys.exc_info()[1]
except socket.error as e:
self.disconnect()
if len(e.args) == 1:
_errno, errmsg = 'UNKNOWN', e.args[0]
else:
_errno, errmsg = e.args
raise ConnectionError("Error %s while writing to socket. %s." %
(_errno, errmsg))
except Exception as e:
except Exception:
self.disconnect()
raise e
raise

def pack_command(self, *args):
output = []
Expand Down
17 changes: 8 additions & 9 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3887,8 +3887,8 @@ def _execute_transaction(self, connection, commands, raise_on_error):
# the socket
try:
self.parse_response(connection, '_')
except ResponseError:
errors.append((0, sys.exc_info()[1]))
except ResponseError as e:
errors.append((0, e))

# and all the other commands
for i, command in enumerate(commands):
Expand All @@ -3897,18 +3897,17 @@ def _execute_transaction(self, connection, commands, raise_on_error):
else:
try:
self.parse_response(connection, '_')
except ResponseError:
ex = sys.exc_info()[1]
self.annotate_exception(ex, i + 1, command[0])
errors.append((i, ex))
except ResponseError as e:
self.annotate_exception(e, i + 1, command[0])
errors.append((i, e))

# parse the EXEC.
try:
response = self.parse_response(connection, '_')
except ExecAbortError:
if errors:
raise errors[0][1]
raise sys.exc_info()[1]
raise

# EXEC clears any watched keys
self.watching = False
Expand Down Expand Up @@ -3950,8 +3949,8 @@ def _execute_pipeline(self, connection, commands, raise_on_error):
try:
response.append(
self.parse_response(connection, args[0], **options))
except ResponseError:
response.append(sys.exc_info()[1])
except ResponseError as e:
response.append(e)

if raise_on_error:
self.raise_first_error(commands, response)
Expand Down
17 changes: 7 additions & 10 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,7 @@ def connect(self):
sock = self._connect()
except socket.timeout:
raise TimeoutError("Timeout connecting to server")
except socket.error:
e = sys.exc_info()[1]
except socket.error as e:
raise ConnectionError(self._error_message(e))

self._sock = sock
Expand Down Expand Up @@ -701,8 +700,7 @@ def send_packed_command(self, command, check_health=True):
except socket.timeout:
self.disconnect()
raise TimeoutError("Timeout writing to socket")
except socket.error:
e = sys.exc_info()[1]
except socket.error as e:
self.disconnect()
if len(e.args) == 1:
errno, errmsg = 'UNKNOWN', e.args[0]
Expand All @@ -711,7 +709,7 @@ def send_packed_command(self, command, check_health=True):
errmsg = e.args[1]
raise ConnectionError("Error %s while writing to socket. %s." %
(errno, errmsg))
except: # noqa: E722
except BaseException:
self.disconnect()
raise

Expand All @@ -736,12 +734,11 @@ def read_response(self):
self.disconnect()
raise TimeoutError("Timeout reading from %s:%s" %
(self.host, self.port))
except socket.error:
except socket.error as e:
self.disconnect()
e = sys.exc_info()[1]
raise ConnectionError("Error while reading from %s:%s : %s" %
(self.host, self.port, e.args))
except: # noqa: E722
except BaseException:
self.disconnect()
raise

Expand Down Expand Up @@ -1197,7 +1194,7 @@ def get_connection(self, command_name, *keys, **options):
connection.connect()
if connection.can_read():
raise ConnectionError('Connection not ready')
except: # noqa: E722
except BaseException:
# release the connection back to the pool so that we don't
# leak it
self.release(connection)
Expand Down Expand Up @@ -1359,7 +1356,7 @@ def get_connection(self, command_name, *keys, **options):
connection.connect()
if connection.can_read():
raise ConnectionError('Connection not ready')
except: # noqa: E722
except BaseException:
# release the connection back to the pool so that we don't leak it
self.release(connection)
raise
Expand Down