-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexception.py
646 lines (533 loc) · 23.1 KB
/
exception.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# License: MIT
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
"""Exceptions raised by an API client."""
from __future__ import annotations
from typing import Protocol
import grpc
from grpc.aio import AioRpcError
class ApiClientError(Exception):
"""There was an error in an API client.
To simplify retrying, errors are classified as
[retryable][frequenz.client.base.exception.ApiClientError.is_retryable], or not.
Retryable errors might succeed if retried, while permanent errors won't. When
uncertain, errors are assumed to be retryable.
The following sub-classes are available:
- [GrpcError][frequenz.client.base.exception.GrpcError]: A gRPC operation failed.
"""
def __init__(
self,
*,
server_url: str,
operation: str,
description: str,
retryable: bool,
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
description: A human-readable description of the error.
retryable: Whether retrying the operation might succeed.
"""
super().__init__(
f"Failed calling {operation!r} on {server_url!r}: {description}"
)
self.server_url = server_url
"""The URL of the server that returned the error."""
self.operation = operation
"""The operation that caused the error."""
self.description = description
"""The human-readable description of the error."""
self.is_retryable = retryable
"""Whether retrying the operation might succeed."""
@classmethod
def from_grpc_error(
cls,
*,
server_url: str,
operation: str,
grpc_error: AioRpcError,
) -> GrpcError:
"""Create an instance of the appropriate subclass from a gRPC error.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error to convert.
Returns:
An instance of
[GrpcError][frequenz.client.base.exception.GrpcError] if the gRPC status
is not recognized, or an appropriate subclass if it is.
"""
class Ctor(Protocol):
"""A protocol for the constructor of a subclass of `GrpcError`."""
def __call__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> GrpcError: ...
grpc_status_map: dict[grpc.StatusCode, Ctor] = {
grpc.StatusCode.CANCELLED: OperationCancelled,
grpc.StatusCode.UNKNOWN: UnknownError,
grpc.StatusCode.INVALID_ARGUMENT: InvalidArgument,
grpc.StatusCode.DEADLINE_EXCEEDED: OperationTimedOut,
grpc.StatusCode.NOT_FOUND: EntityNotFound,
grpc.StatusCode.ALREADY_EXISTS: EntityAlreadyExists,
grpc.StatusCode.PERMISSION_DENIED: PermissionDenied,
grpc.StatusCode.RESOURCE_EXHAUSTED: ResourceExhausted,
grpc.StatusCode.FAILED_PRECONDITION: OperationPreconditionFailed,
grpc.StatusCode.ABORTED: OperationAborted,
grpc.StatusCode.OUT_OF_RANGE: OperationOutOfRange,
grpc.StatusCode.UNIMPLEMENTED: OperationNotImplemented,
grpc.StatusCode.INTERNAL: InternalError,
grpc.StatusCode.UNAVAILABLE: ServiceUnavailable,
grpc.StatusCode.DATA_LOSS: DataLoss,
grpc.StatusCode.UNAUTHENTICATED: OperationUnauthenticated,
}
if ctor := grpc_status_map.get(grpc_error.code()):
return ctor(
server_url=server_url, operation=operation, grpc_error=grpc_error
)
return UnrecognizedGrpcStatus(
server_url=server_url,
operation=operation,
grpc_error=grpc_error,
)
class ClientNotConnected(ApiClientError):
"""The client is not connected to the server."""
def __init__(self, *, server_url: str, operation: str) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The client is not connected to the server",
retryable=True,
)
class GrpcError(ApiClientError):
"""The gRPC server returned an error with a status code.
These errors are specific to gRPC. If you want to use the client in
a protocol-independent way, you should avoid catching this exception. Catching
subclasses that don't have *grpc* in their name should be protocol-independent.
The following sub-classes are available:
- [DataLoss][frequenz.client.base.exception.DataLoss]: Unrecoverable data loss or
corruption.
- [EntityAlreadyExists][frequenz.client.base.exception.EntityAlreadyExists]: The
entity that we attempted to create already exists.
- [EntityNotFound][frequenz.client.base.exception.EntityNotFound]: The requested
entity was not found.
- [InternalError][frequenz.client.base.exception.InternalError]: Some invariants
expected by the underlying system have been broken.
- [InvalidArgument][frequenz.client.base.exception.InvalidArgument]: The client
specified an invalid argument.
- [OperationAborted][frequenz.client.base.exception.OperationAborted]: The
operation was aborted.
- [OperationCancelled][frequenz.client.base.exception.OperationCancelled]: The
operation was cancelled.
- [OperationNotImplemented][frequenz.client.base.exception.OperationNotImplemented]:
The operation is not implemented or not supported/enabled in this service.
- [OperationOutOfRange][frequenz.client.base.exception.OperationOutOfRange]: The
operation was attempted past the valid range.
- [OperationPreconditionFailed][frequenz.client.base.exception.OperationPreconditionFailed]:
The operation was rejected because the system is not in a required state.
- [OperationTimedOut][frequenz.client.base.exception.OperationTimedOut]: The time
limit was exceeded while waiting for the operation to complete.
- [OperationUnauthenticated][frequenz.client.base.exception.OperationUnauthenticated]:
The request does not have valid authentication credentials for the operation.
- [PermissionDenied][frequenz.client.base.exception.PermissionDenied]: The caller
does not have permission to execute the specified operation.
- [ResourceExhausted][frequenz.client.base.exception.ResourceExhausted]: Some
resource has been exhausted (for example per-user quota, disk space, etc.).
- [ServiceUnavailable][frequenz.client.base.exception.ServiceUnavailable]: The
service is currently unavailable.
- [UnknownError][frequenz.client.base.exception.UnknownError]: There was an error
that can't be described using other statuses.
- [UnrecognizedGrpcStatus][frequenz.client.base.exception.UnrecognizedGrpcStatus]:
The gRPC server returned an unrecognized status code.
References:
* [gRPC status
codes](https://github.com/grpc/grpc/blob/master/doc/statuscodes.md)
"""
def __init__( # pylint: disable=too-many-arguments
self,
*,
server_url: str,
operation: str,
description: str,
grpc_error: AioRpcError,
retryable: bool,
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
description: A human-readable description of the error.
grpc_error: The gRPC error originating this exception.
retryable: Whether retrying the operation might succeed.
"""
status_name = grpc_error.code().name
message = grpc_error.details()
details = grpc_error.debug_error_string()
message = f": {message}" if message else ""
details = f" ({details})" if details else ""
super().__init__(
server_url=server_url,
operation=operation,
description=f"{description} <status={status_name}>{message}{details}",
retryable=retryable,
)
self.description: str = description
"""The human-readable description of the error."""
self.grpc_error: AioRpcError = grpc_error
"""The original gRPC error."""
class UnrecognizedGrpcStatus(GrpcError):
"""The gRPC server returned an unrecognized status code."""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="Got an unrecognized status code",
grpc_error=grpc_error,
retryable=True, # We don't know so we assume it's retryable
)
class OperationCancelled(GrpcError):
"""The operation was cancelled."""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The operation was cancelled",
grpc_error=grpc_error,
retryable=True,
)
class UnknownError(GrpcError):
"""There was an error that can't be described using other statuses."""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="There was an error that can't be described using other statuses",
grpc_error=grpc_error,
retryable=True, # We don't know so we assume it's retryable
)
class InvalidArgument(GrpcError, ValueError):
"""The client specified an invalid argument.
Note that this error differs from
[OperationPreconditionFailed][frequenz.client.base.exception.OperationPreconditionFailed].
This error indicates arguments that are problematic regardless of the state of the
system (e.g., a malformed file name).
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The client specified an invalid argument",
grpc_error=grpc_error,
retryable=False,
)
class OperationTimedOut(GrpcError):
"""The time limit was exceeded while waiting for the operationt o complete.
For operations that change the state of the system, this error may be returned even
if the operation has completed successfully. For example, a successful response from
a server could have been delayed long.
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The time limit was exceeded while waiting for the operation "
"to complete",
grpc_error=grpc_error,
retryable=True,
)
class EntityNotFound(GrpcError):
"""The requested entity was not found.
Note that this error differs from
[PermissionDenied][frequenz.client.base.exception.PermissionDenied]. This error is
used when the requested entity is not found, regardless of the user's permissions.
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The requested entity was not found",
grpc_error=grpc_error,
retryable=True, # If the entity is added later it might succeed
)
class EntityAlreadyExists(GrpcError):
"""The entity that we attempted to create already exists."""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The entity that we attempted to create already exists",
grpc_error=grpc_error,
retryable=True, # If the entity is deleted later it might succeed
)
class PermissionDenied(GrpcError):
"""The caller does not have permission to execute the specified operation.
Note that when the operation is rejected due to other reasons, such as the resources
being exhausted or the user not being authenticated at all, different errors should
be catched instead
([ResourceExhausted][frequenz.client.base.exception.ResourceExhausted] and
[OperationUnauthenticated][frequenz.client.base.exception.OperationUnauthenticated]
respectively).
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The caller does not have permission to execute the specified "
"operation",
grpc_error=grpc_error,
retryable=True, # If the user is granted permission it might succeed
)
class ResourceExhausted(GrpcError):
"""Some resource has been exhausted (for example per-user quota, disk space, etc.)."""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="Some resource has been exhausted (for example per-user quota, "
"disk space, etc.)",
grpc_error=grpc_error,
retryable=True, # If the resource is freed it might succeed
)
class OperationPreconditionFailed(GrpcError):
"""The operation was rejected because the system is not in a required state.
For example, the directory to be deleted is non-empty, an rmdir operation is applied
to a non-directory, etc. The user should perform some corrective action before
retrying the operation.
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The operation was rejected because the system is not in a "
"required state",
grpc_error=grpc_error,
retryable=True, # If the system state changes it might succeed
)
class OperationAborted(GrpcError):
"""The operation was aborted.
Typically due to a concurrency issue or transaction abort.
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The operation was aborted",
grpc_error=grpc_error,
retryable=True,
)
class OperationOutOfRange(GrpcError):
"""The operation was attempted past the valid range.
Unlike [InvalidArgument][frequenz.client.base.exception.InvalidArgument], this error
indicates a problem that may be fixed if the system state changes.
There is a fair bit of overlap with
[OperationPreconditionFailed][frequenz.client.base.exception.OperationPreconditionFailed],
this error is just a more specific version of that error and could be the result of
an operation that doesn't even take any arguments.
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The operation was attempted past the valid range",
grpc_error=grpc_error,
retryable=True, # If the system state changes it might succeed
)
class OperationNotImplemented(GrpcError):
"""The operation is not implemented or not supported/enabled in this service."""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The operation is not implemented or not supported/enabled in "
"this service",
grpc_error=grpc_error,
retryable=False,
)
class InternalError(GrpcError):
"""Some invariants expected by the underlying system have been broken.
This error code is reserved for serious errors.
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="Some invariants expected by the underlying system have been "
"broken",
grpc_error=grpc_error,
retryable=True, # If the system state changes it might succeed
)
class ServiceUnavailable(GrpcError):
"""The service is currently unavailable.
This is most likely a transient condition, which can be corrected by retrying with
a backoff. Note that it is not always safe to retry non-idempotent operations.
"""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The service is currently unavailable",
grpc_error=grpc_error,
retryable=True, # If the service becomes available it might succeed
)
class DataLoss(GrpcError):
"""Unrecoverable data loss or corruption."""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="Unrecoverable data loss or corruption",
grpc_error=grpc_error,
retryable=False,
)
class OperationUnauthenticated(GrpcError):
"""The request does not have valid authentication credentials for the operation."""
def __init__(
self, *, server_url: str, operation: str, grpc_error: AioRpcError
) -> None:
"""Create a new instance.
Args:
server_url: The URL of the server that returned the error.
operation: The operation that caused the error.
grpc_error: The gRPC error originating this exception.
"""
super().__init__(
server_url=server_url,
operation=operation,
description="The request does not have valid authentication credentials "
"for the operation",
grpc_error=grpc_error,
retryable=False,
)