Skip to content

Commit 0bd8365

Browse files
committed
Fix implementation and move to utils
1 parent 533e99e commit 0bd8365

File tree

5 files changed

+14
-25
lines changed

5 files changed

+14
-25
lines changed

redis/asyncio/client.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
get_lib_version,
7878
safe_str,
7979
str_if_bytes,
80+
truncate_command_for_exception,
8081
)
8182

8283
PubSubHandler = Callable[[Dict[str, str]], Awaitable[None]]
@@ -1509,16 +1510,11 @@ def annotate_exception(
15091510
) -> None:
15101511
cmd = " ".join(map(safe_str, command))
15111512
msg = (
1512-
f"Command # {number} ({self._truncate_command(cmd)}) "
1513+
f"Command # {number} ({truncate_command_for_exception(cmd)}) "
15131514
"of pipeline caused error: {exception.args}"
15141515
)
15151516
exception.args = (msg,) + exception.args[1:]
15161517

1517-
def _truncate_command(self, command, max_length=100):
1518-
if len(command) > max_length:
1519-
return command[: max_length - 3] + "..."
1520-
return command
1521-
15221518
async def parse_response(
15231519
self, connection: Connection, command_name: Union[str, bytes], **options
15241520
):

redis/asyncio/cluster.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
TryAgainError,
6565
)
6666
from redis.typing import AnyKeyT, EncodableT, KeyT
67-
from redis.utils import deprecated_function, get_lib_version, safe_str, str_if_bytes
67+
from redis.utils import deprecated_function, get_lib_version, safe_str, str_if_bytes, truncate_command_for_exception
6868

6969
TargetNodesT = TypeVar(
7070
"TargetNodesT", str, "ClusterNode", List["ClusterNode"], Dict[Any, "ClusterNode"]
@@ -1601,7 +1601,7 @@ async def _execute(
16011601
command = " ".join(map(safe_str, cmd.args))
16021602
msg = (
16031603
f"Command # {cmd.position + 1} "
1604-
f"({self._truncate_command(command)}) "
1604+
f"({truncate_command_for_exception(command)}) "
16051605
f"of pipeline caused error: {result.args}"
16061606
)
16071607
result.args = (msg,) + result.args[1:]
@@ -1651,11 +1651,6 @@ def mset_nonatomic(
16511651

16521652
return self
16531653

1654-
def _truncate_command(self, command, max_length=100):
1655-
if len(command) > max_length:
1656-
return command[: max_length - 3] + "..."
1657-
return command
1658-
16591654

16601655
for command in PIPELINE_BLOCKED_COMMANDS:
16611656
command = command.replace(" ", "_").lower()

redis/client.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
get_lib_version,
6262
safe_str,
6363
str_if_bytes,
64+
truncate_command_for_exception,
6465
)
6566

6667
if TYPE_CHECKING:
@@ -1521,16 +1522,11 @@ def raise_first_error(self, commands, response):
15211522
def annotate_exception(self, exception, number, command):
15221523
cmd = " ".join(map(safe_str, command))
15231524
msg = (
1524-
f"Command # {number} ({self._truncate_command(cmd)}) of pipeline "
1525+
f"Command # {number} ({truncate_command_for_exception(cmd)}) of pipeline "
15251526
f"caused error: {exception.args[0]}"
15261527
)
15271528
exception.args = (msg,) + exception.args[1:]
15281529

1529-
def _truncate_command(self, command, max_length=100):
1530-
if len(command) > max_length:
1531-
return command[: max_length - 3] + "..."
1532-
return command
1533-
15341530
def parse_response(self, connection, command_name, **options):
15351531
result = Redis.parse_response(self, connection, command_name, **options)
15361532
if command_name in self.UNWATCH_COMMANDS:

redis/cluster.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
merge_result,
4747
safe_str,
4848
str_if_bytes,
49+
truncate_command_for_exception,
4950
)
5051

5152

@@ -2054,16 +2055,11 @@ def annotate_exception(self, exception, number, command):
20542055
"""
20552056
cmd = " ".join(map(safe_str, command))
20562057
msg = (
2057-
f"Command # {number} ({self._truncate_command(cmd)}) of pipeline "
2058+
f"Command # {number} ({truncate_command_for_exception(cmd)}) of pipeline "
20582059
f"caused error: {exception.args[0]}"
20592060
)
20602061
exception.args = (msg,) + exception.args[1:]
20612062

2062-
def _truncate_command(self, command, max_length=100):
2063-
if len(command) > max_length:
2064-
return command[: max_length - 3] + "x.."
2065-
return command
2066-
20672063
def execute(self, raise_on_error: bool = True) -> List[Any]:
20682064
"""
20692065
Execute all the commands in the current pipeline

redis/utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,9 @@ def ensure_string(key):
257257
return key
258258
else:
259259
raise TypeError("Key must be either a string or bytes")
260+
261+
262+
def truncate_command_for_exception(self, command, max_length=100):
263+
if len(command) > max_length:
264+
return command[: max_length - 3] + "..."
265+
return command

0 commit comments

Comments
 (0)