This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.py
285 lines (212 loc) · 8.16 KB
/
error.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
# vim:ai:et:ff=unix:fileencoding=utf-8:sw=4:ts=4:
# conveyor/src/main/python/conveyor/error.py
#
# conveyor - Printing dispatch engine for 3D objects and their friends.
# Copyright © 2012 Matthew W. Samsonoff <[email protected]>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, print_function, unicode_literals)
class Handleable(object):
def handle(self, log):
'''
Handle the error by writing it to the specified log and returning an
exit code.
'''
raise NotImplementedError
# NOTE: some exceptions end with `Error` and others with `Exception`. The name
# should have the same ending as the base class. Most of the `Error` classes
# derive from the built-in `KeyError`.
class ConfigKeyError(KeyError, Handleable):
'''
Raised when the configuration is missing a key. Since there are default
values, the configuration should always be fully populated and this
exception indicates a programming error.
'''
def __init__(self, config_path, key):
KeyError.__init__(self, key)
self.config_path = config_path
self.key = key
def handle(self, log):
log.critical('internal error', exc_info=True)
return 1
class ConfigTypeError(TypeError, Handleable):
'''
Raised when a configuration file element has an invalid type (e.g., it is a
string instead of a number).
'''
def __init__(self, config_path, key, value):
TypeError.__init__(self, value)
self.config_path = config_path
self.key = key
self.value = value
def handle(self, log):
log.critical(
'invalid type for configuration file element: %s: %s: %s',
e.config_path, e.key, e.value, exc_info=True)
return 1
class ConfigValueError(ValueError, Handleable):
'''
Raised when a configuration file element has an invalid value (e.g., the
value for the logging level parameter is not one of the valid logging
levels).
'''
def __init__(self, config_path, key, value):
ValueError.__init__(self, key, value)
self.config_path = config_path
self.key = key
self.value = value
def handle(self, log):
log.critical(
'invalid value for configuration file element: %s: %s: %s',
e.config_path, e.key, e.value, exc_info=True)
return 1
class DriverMismatchException(Exception, Handleable):
def handle(self, log):
log.critical(
'the requested driver does not match the machine\'s current driver',
exc_info=True)
return 1
class MachineStateException(Exception, Handleable):
def handle(self, log):
log.critical(
'the machine is in an invalid state for that operation',
exc_info=True)
return 1
class MissingExecutableException(Exception, Handleable):
def __init__(self, path):
Exception.__init__(self, path)
self.path = path
def handle(self, log):
log.critical('missing executable: %s', self.path, exc_info=True)
return 1
class MissingFileException(Exception, Handleable):
def __init__(self, path):
Exception.__init__(self, path)
self.path = path
def handle(self, log):
log.critical('missing file: %s', self.path, exc_info=True)
return 1
class MissingMachineNameException(Exception, Handleable):
def handle(self, log):
log.critical(
'unable to automatically detect the machine name; please specify a machine name',
exc_info=True)
return 1
class MultipleDriversException(Exception, Handleable):
def handle(self, log):
log.critical(
'there are multiple drivers available; please specify a driver',
exc_info=True)
return 1
class MultiplePortsException(Exception, Handleable):
def handle(self, log):
log.critical(
'there are multiple ports available; please specify a port',
exc_info=True)
return 1
class NoDriversException(Exception, Handleable):
def handle(self, log):
log.critical('there are no drivers available', exc_info=True)
return 1
class NoPortsException(Exception, Handleable):
def handle(self, log):
log.critical('there are no ports available', exc_info=True)
return 1
class NotFileException(Exception, Handleable):
def __init__(self, path):
Exception.__init__(self, path)
def handle(self, log):
log.critical('not a file: %s', self.path, exc_info=True)
return 1
class PortMismatchException(Exception, Handleable):
def handle(self, log):
log.critical(
'the requested port does not match the machine\'s current port',
exc_info=True)
return 1
class PrintQueuedException(Exception, Handleable):
def handle(self, log):
log.error('a print is already queued for the machine', exc_info=True)
return 1
class ProfileMismatchException(Exception, Handleable):
def handle(self, log):
log.critical(
'the requested profile does not match the machine\'s current profile',
exc_info=True)
return 1
class UnknownDriverError(KeyError, Handleable):
def __init__(self, driver_name):
KeyError.__init__(self, driver_name)
self.driver_name = driver_name
def handle(self, log):
log.critical('unknown driver: %s', e.driver_name, exc_info=True)
return 1
class UnknownJobError(KeyError, Handleable):
def __init__(self, job_id):
KeyError.__init__(self, job_id)
self.job_id = job_id
def handle(self, log):
log.critical('unknown job: %s', e.job_id, exc_info=True)
return 1
class UnknownMachineError(KeyError, Handleable):
def __init__(self, machine_name):
KeyError.__init__(self, machine_name)
self.machine_name = machine_name
def handle(self, log):
log.critical('unknown machine: %s', e.machine_name, exc_info=True)
return 1
class UnknownPortError(KeyError, Handleable):
def __init__(self, port_name):
KeyError.__init__(self, port_name)
self.port_name = port_name
def handle(self, log):
log.critical('unknown port: %s', e.port_name, exc_info=True)
return 1
class UnknownProfileError(KeyError, Handleable):
def __init__(self, profile_name):
KeyError.__init__(self, profile_name)
self.profile_name = profile_name
def handle(self, log):
log.critical('unknown profile: %s', e.profile_name, exc_info=True)
return 1
class UnsupportedModelTypeException(Exception, Handleable):
def __init__(self, path):
Exception.__init__(self, path)
self.path = path
def handle(self, log):
log.critical(
'not a supported model type: %s', self.path, exc_info=True)
return 1
class UnsupportedPlatformException(Exception, Handleable):
'''Raised when conveyor does not support your operating system.'''
def handle(self, log):
log.critical('conveyor does not support your platform', exc_info=True)
return 1
def guard(log, func):
try:
code = func()
except Handleable as e:
code = e.handle(log)
except KeyboardInterrupt:
code = 0
log.warning('interrupted')
log.debug('handled exception', exc_info=True)
except SystemExit as e:
code = e.code
log.debug('handled exception', exc_info=True)
except Exception, e:
code = 1
log.critical('internal error', exc_info=True)
log.exception(e)
return code