1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 # errors.py - Arvados-specific exceptions.
9 from apiclient import errors as apiclient_errors
10 from collections import OrderedDict
12 class ApiError(apiclient_errors.HttpError):
13 def _get_reason(self):
15 return '; '.join(json.loads(self.content.decode('utf-8'))['errors'])
16 except (KeyError, TypeError, ValueError):
17 return super(ApiError, self)._get_reason()
20 class KeepRequestError(Exception):
21 """Base class for errors accessing Keep services."""
22 def __init__(self, message='', request_errors=(), label=""):
23 """KeepRequestError(message='', request_errors=(), label="")
26 A human-readable message describing what Keep operation
30 An iterable that yields 2-tuples of keys (where the key refers to
31 some operation that was attempted) to the error encountered when
32 talking to it--either an exception, or an HTTP response object.
33 These will be packed into an OrderedDict, available through the
34 request_errors() method.
37 A label indicating the type of value in the 'key' position of request_errors.
41 self._request_errors = OrderedDict(request_errors)
42 if self._request_errors:
43 exc_reports = [self._format_error(*err_pair)
44 for err_pair in self._request_errors.items()]
45 base_msg = "{}: {}".format(message, "; ".join(exc_reports))
48 super(KeepRequestError, self).__init__(base_msg)
49 self.message = message
51 def _format_error(self, key, error):
52 if isinstance(error, HttpError):
53 err_fmt = "{} {} responded with {e.status_code} {e.reason}"
55 err_fmt = "{} {} raised {e.__class__.__name__} ({e})"
56 return err_fmt.format(self.label, key, e=error)
58 def request_errors(self):
59 """request_errors() -> OrderedDict
61 The keys of the dictionary are described by `self.label`
62 The corresponding value is the exception raised when sending the
64 return self._request_errors
67 class HttpError(Exception):
68 def __init__(self, status_code, reason):
69 self.status_code = status_code
73 class ArgumentError(Exception):
75 class SyntaxError(Exception):
77 class AssertionError(Exception):
79 class CommandFailedError(Exception):
81 class KeepReadError(KeepRequestError):
83 class KeepWriteError(KeepRequestError):
85 class NotFoundError(KeepReadError):
87 class NotImplementedError(Exception):
89 class NoKeepServersError(Exception):
91 class StaleWriterStateError(Exception):
93 class FeatureNotEnabledError(Exception):