-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcamera_error_handler.py
40 lines (32 loc) · 1.13 KB
/
camera_error_handler.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
from json import dumps
from traceback import format_exc
from mhp.topics import Camera
class CameraErrorHandler:
"""
Exception handler for the Raspicam
Use alongside the 'with' magic Python word.
eg. with CameraException(client, camera, backend_name, bg_path, configs)
"""
def __init__(
self, client, camera: str, backend: str, bg_path: str, configs: dict
):
self.client = client
self.camera = camera
self.backend = backend
self.bg_path = bg_path
self.configs = configs
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
""" Handle any exception thrown, if an error occured. """
if exc_type is Exception:
message = {
"camera": self.camera,
"backend": self.backend,
"bg_path": self.bg_path,
"configs": self.configs,
"traceback": format_exc(),
"message": str(exc_value),
}
status_topic = str(Camera.errors)
self.client.publish(status_topic, dumps(message))