File tree Expand file tree Collapse file tree 4 files changed +41
-2
lines changed Expand file tree Collapse file tree 4 files changed +41
-2
lines changed Original file line number Diff line number Diff line change 5
5
from .exceptions import ClientError # noqa: F401
6
6
from .exceptions import ConnectionFailedError # noqa: F401
7
7
from .exceptions import ConnectionTimeoutError # noqa: F401
8
+ from .exceptions import DuplicateLeaseError # noqa: F401
8
9
from .exceptions import InternalError # noqa: F401
9
10
from .exceptions import InvalidArgumentError # noqa: F401
10
11
from .exceptions import PreconditionFailedError # noqa: F401
Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ class PreconditionFailedError(ClientError):
29
29
"""Raises on etcd server precondition errors."""
30
30
31
31
32
+ class DuplicateLeaseError (ClientError ):
33
+ """Raised on attempt to create lease with already existing id."""
34
+
35
+
32
36
class RevisionCompactedError (ClientError ):
33
37
"""Raises when requested and previous revisions were already compacted."""
34
38
@@ -68,9 +72,13 @@ def _handle_exception(error: Exception):
68
72
# Query RPC error mapping and raise one of the matched client errors
69
73
if isinstance (error , rpc .AioRpcError ):
70
74
e = _EXCEPTIONS_BY_CODE .get (error .code ())
75
+ error_details = error .details ()
76
+
71
77
if e is not None :
72
- raise e (error .details ()) from error
73
- raise ClientError (error .details ()) from error
78
+ if e is PreconditionFailedError and 'lease already exists' in error_details :
79
+ raise DuplicateLeaseError
80
+ raise e (error_details ) from error
81
+ raise ClientError (error_details ) from error
74
82
75
83
# Fallback to wrap original error with the client error
76
84
raise ClientError (error )
Original file line number Diff line number Diff line change 2
2
3
3
import pytest
4
4
5
+ import aetcd .exceptions
6
+
5
7
6
8
@pytest .mark .asyncio
7
9
async def test_lease_grant (etcd ):
@@ -44,3 +46,11 @@ async def test_lease_expire(etcd):
44
46
await asyncio .sleep ((await lease .granted_ttl ()) + 1 )
45
47
result = await etcd .get (key )
46
48
assert result is None
49
+
50
+
51
+ @pytest .mark .asyncio
52
+ async def test_lease_create_with_already_existing_id (etcd ):
53
+ await etcd .lease (10 , lease_id = 123 )
54
+
55
+ with pytest .raises (aetcd .exceptions .DuplicateLeaseError ):
56
+ await etcd .lease (15 , lease_id = 123 )
Original file line number Diff line number Diff line change @@ -67,3 +67,23 @@ def test__handle_exception_with_unknown_errors(rpc_error):
67
67
68
68
with pytest .raises (aetcd .exceptions .ClientError , match = 'unknown error' ):
69
69
aetcd .exceptions ._handle_exception (error )
70
+
71
+
72
+ def test__handle_exception_with_duplicate_lease_error (rpc_error ):
73
+ error = rpc_error (
74
+ code = aetcd .rpc .StatusCode .FAILED_PRECONDITION ,
75
+ details = 'etcdserver: lease already exists' ,
76
+ )
77
+
78
+ with pytest .raises (aetcd .exceptions .DuplicateLeaseError ):
79
+ aetcd .exceptions ._handle_exception (error )
80
+
81
+
82
+ def test__handle_exception_with_duplicate_lease_error_and_internal_rpc_error (rpc_error ):
83
+ error = rpc_error (
84
+ code = aetcd .rpc .StatusCode .INTERNAL ,
85
+ details = 'etcdserver: lease already exists' ,
86
+ )
87
+
88
+ with pytest .raises (aetcd .exceptions .InternalError ):
89
+ aetcd .exceptions ._handle_exception (error )
You can’t perform that action at this time.
0 commit comments