Skip to content

Commit cdb7c7e

Browse files
committed
add error callbacks
1 parent e05af2c commit cdb7c7e

File tree

3 files changed

+109
-14
lines changed

3 files changed

+109
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
55
## [Unreleased]
66
- No unreleased changes so far
77

8+
### Added
9+
- Possibility to register for error callbacks
10+
811
## [0.20.15] - 2021-09-28
912
### Fixed
1013
- Fixed badges

weconnect/elements/vehicle.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,14 @@ def updateStatus(self, updateCapabilities: bool = True, force: bool = False): #
195195
try:
196196
statusResponse: Response = self.weConnect.session.get(url, allow_redirects=False)
197197
except exceptions.ConnectionError as connectionError:
198+
self.notifyError(self, WeConnect.ErrorEventType.CONNECTION, 'connection', 'Could not fetch vehicle status due to connection problem')
198199
raise RetrievalError from connectionError
199200
except exceptions.ReadTimeout as timeoutError:
201+
self.notifyError(self, WeConnect.ErrorEventType.TIMEOUT, 'timeout', 'Could not fetch vehicle status due to timeout')
200202
raise RetrievalError from timeoutError
201203
except exceptions.RetryError as retryError:
204+
self.notifyError(self, WeConnect.ErrorEventType.HTTP, str(retryError.response.status_code),
205+
'Could not fetch vehicle status due to server error')
202206
raise RetrievalError from retryError
203207
if statusResponse.status_code in (codes['ok'], codes['multiple_status']):
204208
data = statusResponse.json()
@@ -210,10 +214,14 @@ def updateStatus(self, updateCapabilities: bool = True, force: bool = False): #
210214
try:
211215
statusResponse = self.weConnect.session.get(url, allow_redirects=False)
212216
except exceptions.ConnectionError as connectionError:
217+
self.notifyError(self, WeConnect.ErrorEventType.CONNECTION, 'connection', 'Could not fetch vehicle status due to connection problem')
213218
raise RetrievalError from connectionError
214219
except exceptions.ReadTimeout as timeoutError:
220+
self.notifyError(self, WeConnect.ErrorEventType.TIMEOUT, 'timeout', 'Could not fetch vehicle status due to timeout')
215221
raise RetrievalError from timeoutError
216222
except exceptions.RetryError as retryError:
223+
self.notifyError(self, WeConnect.ErrorEventType.HTTP, str(retryError.response.status_code),
224+
'Could not fetch vehicle status due to server error')
217225
raise RetrievalError from retryError
218226
if statusResponse.status_code == codes['ok']:
219227
data = statusResponse.json()
@@ -322,10 +330,14 @@ def updateStatus(self, updateCapabilities: bool = True, force: bool = False): #
322330
try:
323331
statusResponse = self.weConnect.session.get(url, allow_redirects=False)
324332
except exceptions.ConnectionError as connectionError:
333+
self.notifyError(self, WeConnect.ErrorEventType.CONNECTION, 'connection', 'Could not fetch parking position due to connection problem')
325334
raise RetrievalError from connectionError
326335
except exceptions.ReadTimeout as timeoutError:
336+
self.notifyError(self, WeConnect.ErrorEventType.TIMEOUT, 'timeout', 'Could not fetch parking position due to timeout')
327337
raise RetrievalError from timeoutError
328338
except exceptions.RetryError as retryError:
339+
self.notifyError(self, WeConnect.ErrorEventType.HTTP, str(retryError.response.status_code),
340+
'Could not fetch parking position due to server error')
329341
raise RetrievalError from retryError
330342
if statusResponse.status_code == codes['ok']:
331343
data = statusResponse.json()
@@ -337,10 +349,14 @@ def updateStatus(self, updateCapabilities: bool = True, force: bool = False): #
337349
try:
338350
statusResponse = self.weConnect.session.get(url, allow_redirects=False)
339351
except exceptions.ConnectionError as connectionError:
352+
self.notifyError(self, WeConnect.ErrorEventType.CONNECTION, 'connection', 'Could not fetch parking position due to connection problem')
340353
raise RetrievalError from connectionError
341354
except exceptions.ReadTimeout as timeoutError:
355+
self.notifyError(self, WeConnect.ErrorEventType.TIMEOUT, 'timeout', 'Could not fetch parking position due to timeout')
342356
raise RetrievalError from timeoutError
343357
except exceptions.RetryError as retryError:
358+
self.notifyError(self, WeConnect.ErrorEventType.HTTP, str(retryError.response.status_code),
359+
'Could not fetch parking position due to server error')
344360
raise RetrievalError from retryError
345361
if statusResponse.status_code == codes['ok']:
346362
data = statusResponse.json()
@@ -406,10 +422,14 @@ def updatePictures(self) -> None: # noqa: C901
406422
try:
407423
imageResponse: Response = self.weConnect.session.get(url, allow_redirects=False)
408424
except exceptions.ConnectionError as connectionError:
425+
self.notifyError(self, WeConnect.ErrorEventType.CONNECTION, 'connection', 'Could not fetch vehicle image list due to connection problem')
409426
raise RetrievalError from connectionError
410427
except exceptions.ReadTimeout as timeoutError:
428+
self.notifyError(self, WeConnect.ErrorEventType.TIMEOUT, 'timeout', 'Could not fetch vehicle image list due to timeout')
411429
raise RetrievalError from timeoutError
412430
except exceptions.RetryError as retryError:
431+
self.notifyError(self, WeConnect.ErrorEventType.HTTP, str(retryError.response.status_code),
432+
'Could not fetch vehicle image list due to server error')
413433
raise RetrievalError from retryError
414434
if imageResponse.status_code == codes['ok']:
415435
data = imageResponse.json()
@@ -421,10 +441,14 @@ def updatePictures(self) -> None: # noqa: C901
421441
try:
422442
imageResponse = self.weConnect.session.get(url, allow_redirects=False)
423443
except exceptions.ConnectionError as connectionError:
444+
self.notifyError(self, WeConnect.ErrorEventType.CONNECTION, 'connection', 'Could not fetch vehicle image list due to connection problem')
424445
raise RetrievalError from connectionError
425446
except exceptions.ReadTimeout as timeoutError:
447+
self.notifyError(self, WeConnect.ErrorEventType.TIMEOUT, 'timeout', 'Could not fetch vehicle image list due to timeout')
426448
raise RetrievalError from timeoutError
427449
except exceptions.RetryError as retryError:
450+
self.notifyError(self, WeConnect.ErrorEventType.HTTP, str(retryError.response.status_code),
451+
'Could not fetch vehicle image list due to server error')
428452
raise RetrievalError from retryError
429453
if imageResponse.status_code == codes['ok']:
430454
data = imageResponse.json()
@@ -449,10 +473,14 @@ def updatePictures(self) -> None: # noqa: C901
449473
try:
450474
imageDownloadResponse = self.weConnect.session.get(imageurl, stream=True)
451475
except exceptions.ConnectionError as connectionError:
476+
self.notifyError(self, WeConnect.ErrorEventType.CONNECTION, 'connection', 'Could not fetch vehicle image due to connection problem')
452477
raise RetrievalError from connectionError
453478
except exceptions.ReadTimeout as timeoutError:
479+
self.notifyError(self, WeConnect.ErrorEventType.TIMEOUT, 'timeout', 'Could not fetch vehicle image due to timeout')
454480
raise RetrievalError from timeoutError
455481
except exceptions.RetryError as retryError:
482+
self.notifyError(self, WeConnect.ErrorEventType.HTTP, str(retryError.response.status_code),
483+
'Could not fetch vehicle image due to server error')
456484
raise RetrievalError from retryError
457485
if imageDownloadResponse.status_code == codes['ok']:
458486
img = Image.open(imageDownloadResponse.raw)
@@ -467,10 +495,14 @@ def updatePictures(self) -> None: # noqa: C901
467495
try:
468496
imageDownloadResponse = self.weConnect.session.get(imageurl, stream=True)
469497
except exceptions.ConnectionError as connectionError:
498+
self.notifyError(self, WeConnect.ErrorEventType.CONNECTION, 'connection', 'Could not fetch vehicle image due to connection problem')
470499
raise RetrievalError from connectionError
471500
except exceptions.ReadTimeout as timeoutError:
501+
self.notifyError(self, WeConnect.ErrorEventType.TIMEOUT, 'timeout', 'Could not fetch vehicle image due to timeout')
472502
raise RetrievalError from timeoutError
473503
except exceptions.RetryError as retryError:
504+
self.notifyError(self, WeConnect.ErrorEventType.HTTP, str(retryError.response.status_code),
505+
'Could not fetch vehicle image due to server error')
474506
raise RetrievalError from retryError
475507
if imageDownloadResponse.status_code == codes['ok']:
476508
img = Image.open(imageDownloadResponse.raw)

0 commit comments

Comments
 (0)