-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.py
357 lines (298 loc) · 11.2 KB
/
exceptions.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
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# exceptions.py - Alpaca Exception Classes
#
# Part of the AlpycaDevice Alpaca skeleton/template device driver
#
# Author: Robert B. Denny <[email protected]> (rbd)
#
# Python Compatibility: Requires Python 3.7 or later
# GitHub: https://github.com/ASCOMInitiative/AlpycaDevice
#
# -----------------------------------------------------------------------------
# MIT License
#
# Copyright (c) 2022 Bob Denny
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# -----------------------------------------------------------------------------
# Edit History:
# 17-Dec-2022 rbd 0.1 Initial edit for Alpaca sample/template
# 18-Dec-2022 rbd 0.1 Refactor to support optional overriding
# error message, and support DriverException
# with variable error number.
# 26-Dev-2022 rbd 0.1 Logging, including Python low level exceptions
# 27-Dec-2022 rbd 0.1 MIT License and module header
# 13-Jan-2023 rbd 0.1 Fix DriverException's recovery from bad error number
# 16-Jan-2023 rbd 0.1 Docstrings for other exception classes
#
import traceback
from config import Config
from logging import Logger
global logger
#logger: Logger = None
logger = None # Safe on Python 3.7 but no intellisense in VSCode etc.
class Success:
"""Default err input to response classes, indicates success"""
def __init__(self):
"""Initialize the Success object
Args:
number (int): 0
message (str): ''
"""
self.number: int = 0
self.message: str = ''
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message
class ActionNotImplementedException:
"""Requested ``Action()`` is not implemented"""
def __init__(
self,
message: str = 'The requested action is not implemented in this driver.'
):
"""Initialize the ``ActionNotImplementedException`` object
Args:
number (int): 0x040C (1036)
message (str): 'The requested action is not implemented in this driver.'
* Logs ``ActionNotImplementedException: {message}``
"""
self.number = 0x40C
self.message = message
cname = self.__class__.__name__
logger.error(f'{cname}: {message}')
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message
# The device chooses a number between 0x500 and 0xFFF, and
# provides a helpful/specific error message. Asserts the
# error number within range.
#
# args:
class DriverException:
"""
**Exception Class for Driver Internal Errors**
This exception is used for device errors and other internal exceptions.
It can be instantiated with a captured exception object, and if so format
the Alpaca error message to include line number/module or optionally a
complete traceback of the exception (a config option).
"""
def __init__(
self,
number: int = 0x500,
message: str = 'Internal driver error - this should be more specific.',
exc = None # Python exception info
):
"""Initialize the DeviceException object
Args:
number (int): Alpaca error number between 0x500 and 0xFFF, your choice
defaults to 0x500 (1280)
message (str): Specific error message or generic if left blank. Defaults
to 'Internal driver error - this should be more specific.'
exc: Contents 'ex' of 'except Exception as ex:' If not included
then only message is included. If supplied, then a detailed
error message with traceback is created (see full parameter)
Notes:
* Checks error number within legal range and if not, logs this error and substitutes
0x500 number.
* If the Python exception object is included as the 3rd argument, it constructs
a message containing the name of the underlying Python exception and its basic
context. If :py:attr:`~config.Config.verbose_driver_exceptions` is ``true``, a complete
Python traceback is included.
* Logs the constructed ``DriverException`` message
"""
if number <= 0x500 and number >= 0xFFF:
logger.error(f'Programmer error, bad DriverException number {hex(number)}, substituting 0x500')
number = 0x500
self.number = number
cname = self.__class__.__name__
if not exc is None:
if Config.verbose_driver_exceptions:
self.fullmsg = f'{cname}: {message}\n{traceback.format_exc()}' # TODO Safe if not explicitly using exc?
else:
self.fullmsg = f'{cname}: {message}\n{type(exc).__name__}: {str(exc)}'
else:
self.fullmsg = f'{cname}: {message}'
logger.error(self.fullmsg)
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.fullmsg
class InvalidOperationException:
"""The client asked for something that can't be done"""
def __init__(
self,
message: str = 'The requested operation cannot be undertaken at this time.'
):
"""Initialize the ``InvalidOperationException`` object
Args:
number (int): 0x040B (1035)
message (str): 'The requested operation cannot be undertaken at this time.'
* Logs ``InvalidOperationException: {message}``
"""
self.number = 0x40B
self.message = message
cname = self.__class__.__name__
logger.error(f'{cname}: {message}')
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message
class InvalidValueException:
"""A value given is invalid or out of range"""
def __init__(
self,
message: str = 'Invalid value given.'
):
"""Initialize the ``InvalidValueException`` object
Args:
number (int): 0x401 (1025)
message (str): 'Invalid value given.'
* Logs ``InvalidValueException: {message}``
"""
self.number = 0x401
self.message = message
cname = self.__class__.__name__
logger.error(f'{cname}: {message}')
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message
class NotConnectedException:
"""The device must be connected and is not at this time"""
def __init__(
self,
message: str = 'The device is not connected.'
):
"""Initialize the ``NotConnectedException`` object
Args:
number (int): 0x407 (1031)
message (str): 'The device is not connected.'
* Logs ``NotConnectedException: {message}``
"""
self.number = 0x407
self.message = message
cname = self.__class__.__name__
logger.error(f'{cname}: {message}')
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message
class NotImplementedException:
"""The requested property or method is not implemented"""
def __init__(
self,
message: str = 'Property or method not implemented.'
):
"""Initialize the ``NotImplementedException`` object
Args:
number (int): 0x400 (1024)
message (str): 'Property or method not implemented.'
* Logs ``NotImplementedException: {message}``
"""
self.number = 0x400
self.message = message
cname = self.__class__.__name__
logger.error(f'{cname}: {message}')
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message
class ParkedException:
"""Cannot do this while the device is parked"""
def __init__(
self,
message: str = 'Illegal operation while parked.'
):
"""Initialize the ``ParkedException`` object
Args:
number (int): 0x408 (1032)
message (str): 'Illegal operation while parked.'
* Logs ``ParkedException: {message}``
"""
self.number = 0x408
self.message = message
cname = self.__class__.__name__
logger.error(f'{cname}: {message}')
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message
class SlavedException:
"""Cannot do this while the device is slaved"""
def __init__(
self,
message: str = 'Illegal operation while slaved.'
):
"""Initialize the ``SlavedException`` object
Args:
number (int): 0x409 (1033)
message (str): 'Illegal operation while slaved.'
* Logs ``SlavedException: {message}``
"""
self.number = 0x409
self.message = message
cname = self.__class__.__name__
logger.error(f'{cname}: {message}')
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message
class ValueNotSetException:
"""The requested vzalue has not yet een set"""
def __init__(
self,
message: str = 'The value has not yet been set.'
):
"""Initialize the ``ValueNotSetException`` object
Args:
number (int): 0x402 (1026)
message (str): 'The value has not yet been set.'
* Logs ``ValueNotSetException: {message}``
"""
self.number = 0x402
self.message = message
cname = self.__class__.__name__
logger.error(f'{cname}: {message}')
@property
def Number(self) -> int:
return self.number
@property
def Message(self) -> str:
return self.message