| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 1 | class JujuError(Exception): |
| Adam Israel | 1a15d1c | 2017-10-23 12:00:49 -0400 | [diff] [blame] | 2 | def __init__(self, *args, **kwargs): |
| 3 | self.message = '' |
| 4 | self.errors = [] |
| 5 | if args: |
| 6 | self.message = str(args[0]) |
| 7 | if isinstance(args[0], (list, tuple)): |
| 8 | self.errors = args[0] |
| 9 | elif len(args) > 1: |
| 10 | self.errors = list(args) |
| 11 | else: |
| 12 | self.errors = [self.message] |
| 13 | super().__init__(*args, **kwargs) |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 14 | |
| 15 | |
| 16 | class JujuAPIError(JujuError): |
| 17 | def __init__(self, result): |
| 18 | self.result = result |
| 19 | self.message = result['error'] |
| 20 | self.error_code = result.get('error-code') |
| 21 | self.response = result['response'] |
| 22 | self.request_id = result['request-id'] |
| 23 | super().__init__(self.message) |
| 24 | |
| 25 | |
| 26 | class JujuConnectionError(ConnectionError, JujuError): |
| 27 | pass |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class JujuAuthError(JujuConnectionError): |
| 31 | pass |
| 32 | |
| 33 | |
| 34 | class JujuRedirectException(Exception): |
| 35 | """Exception indicating that a redirection was requested""" |
| 36 | def __init__(self, redirect_info): |
| 37 | self.redirect_info = redirect_info |
| 38 | |
| 39 | @property |
| 40 | def ca_cert(self): |
| 41 | return self.redirect_info['ca-cert'] |
| 42 | |
| 43 | @property |
| 44 | def endpoints(self): |
| 45 | return [ |
| 46 | ('{value}:{port}'.format(**s), self.ca_cert) |
| 47 | for servers in self.redirect_info['servers'] |
| 48 | for s in servers if s['scope'] == 'public' |
| 49 | ] |