add escalator frame
[escalator.git] / api / escalator / common / exception.py
1 # Copyright 2010 United States Government as represented by the
2 # Administrator of the National Aeronautics and Space Administration.
3 # All Rights Reserved.
4 #
5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
6 #    not use this file except in compliance with the License. You may obtain
7 #    a copy of the License at
8 #
9 #         http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #    Unless required by applicable law or agreed to in writing, software
12 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 #    License for the specific language governing permissions and limitations
15 #    under the License.
16
17 """Escalator exception subclasses"""
18
19 import six
20 import six.moves.urllib.parse as urlparse
21
22 from escalator import i18n
23
24 _ = i18n._
25
26 _FATAL_EXCEPTION_FORMAT_ERRORS = False
27
28
29 class RedirectException(Exception):
30
31     def __init__(self, url):
32         self.url = urlparse.urlparse(url)
33
34
35 class EscalatorException(Exception):
36     """
37     Base Escalator Exception
38
39     To correctly use this class, inherit from it and define
40     a 'message' property. That message will get printf'd
41     with the keyword arguments provided to the constructor.
42     """
43     message = _("An unknown exception occurred")
44
45     def __init__(self, message=None, *args, **kwargs):
46         if not message:
47             message = self.message
48         try:
49             if kwargs:
50                 message = message % kwargs
51         except Exception:
52             if _FATAL_EXCEPTION_FORMAT_ERRORS:
53                 raise
54             else:
55                 # at least get the core message out if something happened
56                 pass
57         self.msg = message
58         self.message = message
59         super(EscalatorException, self).__init__(message)
60
61     def __unicode__(self):
62         # NOTE(flwang): By default, self.msg is an instance of Message, which
63         # can't be converted by str(). Based on the definition of
64         # __unicode__, it should return unicode always.
65         return six.text_type(self.msg)
66
67
68 class MissingCredentialError(EscalatorException):
69     message = _("Missing required credential: %(required)s")
70
71
72 class BadAuthStrategy(EscalatorException):
73     message = _("Incorrect auth strategy, expected \"%(expected)s\" but "
74                 "received \"%(received)s\"")
75
76
77 class NotFound(EscalatorException):
78     message = _("An object with the specified identifier was not found.")
79
80
81 class BadStoreUri(EscalatorException):
82     message = _("The Store URI was malformed.")
83
84
85 class Duplicate(EscalatorException):
86     message = _("An object with the same identifier already exists.")
87
88
89 class Conflict(EscalatorException):
90     message = _("An object with the same identifier is currently being "
91                 "operated on.")
92
93
94 class AuthBadRequest(EscalatorException):
95     message = _("Connect error/bad request to Auth service at URL %(url)s.")
96
97
98 class AuthUrlNotFound(EscalatorException):
99     message = _("Auth service at URL %(url)s not found.")
100
101
102 class AuthorizationFailure(EscalatorException):
103     message = _("Authorization failed.")
104
105
106 class NotAuthenticated(EscalatorException):
107     message = _("You are not authenticated.")
108
109
110 class Forbidden(EscalatorException):
111     message = _("You are not authorized to complete this action.")
112
113
114 class ProtectedMetadefNamespaceDelete(Forbidden):
115     message = _("Metadata definition namespace %(namespace)s is protected"
116                 " and cannot be deleted.")
117
118
119 class ProtectedMetadefNamespacePropDelete(Forbidden):
120     message = _("Metadata definition property %(property_name)s is protected"
121                 " and cannot be deleted.")
122
123
124 class ProtectedMetadefObjectDelete(Forbidden):
125     message = _("Metadata definition object %(object_name)s is protected"
126                 " and cannot be deleted.")
127
128
129 class ProtectedMetadefResourceTypeAssociationDelete(Forbidden):
130     message = _("Metadata definition resource-type-association"
131                 " %(resource_type)s is protected and cannot be deleted.")
132
133
134 class ProtectedMetadefResourceTypeSystemDelete(Forbidden):
135     message = _("Metadata definition resource-type %(resource_type_name)s is"
136                 " a seeded-system type and cannot be deleted.")
137
138
139 class ProtectedMetadefTagDelete(Forbidden):
140     message = _("Metadata definition tag %(tag_name)s is protected"
141                 " and cannot be deleted.")
142
143
144 class Invalid(EscalatorException):
145     message = _("Data supplied was not valid.")
146
147
148 class InvalidSortKey(Invalid):
149     message = _("Sort key supplied was not valid.")
150
151
152 class InvalidSortDir(Invalid):
153     message = _("Sort direction supplied was not valid.")
154
155
156 class InvalidPropertyProtectionConfiguration(Invalid):
157     message = _("Invalid configuration in property protection file.")
158
159
160 class InvalidFilterRangeValue(Invalid):
161     message = _("Unable to filter using the specified range.")
162
163
164 class InvalidOptionValue(Invalid):
165     message = _("Invalid value for option %(option)s: %(value)s")
166
167
168 class ReadonlyProperty(Forbidden):
169     message = _("Attribute '%(property)s' is read-only.")
170
171
172 class ReservedProperty(Forbidden):
173     message = _("Attribute '%(property)s' is reserved.")
174
175
176 class AuthorizationRedirect(EscalatorException):
177     message = _("Redirecting to %(uri)s for authorization.")
178
179
180 class ClientConnectionError(EscalatorException):
181     message = _("There was an error connecting to a server")
182
183
184 class ClientConfigurationError(EscalatorException):
185     message = _("There was an error configuring the client.")
186
187
188 class MultipleChoices(EscalatorException):
189     message = _("The request returned a 302 Multiple Choices. This generally "
190                 "means that you have not included a version indicator in a "
191                 "request URI.\n\nThe body of response returned:\n%(body)s")
192
193
194 class LimitExceeded(EscalatorException):
195     message = _("The request returned a 413 Request Entity Too Large. This "
196                 "generally means that rate limiting or a quota threshold was "
197                 "breached.\n\nThe response body:\n%(body)s")
198
199     def __init__(self, *args, **kwargs):
200         self.retry_after = (int(kwargs['retry']) if kwargs.get('retry')
201                             else None)
202         super(LimitExceeded, self).__init__(*args, **kwargs)
203
204
205 class ServiceUnavailable(EscalatorException):
206     message = _("The request returned 503 Service Unavailable. This "
207                 "generally occurs on service overload or other transient "
208                 "outage.")
209
210     def __init__(self, *args, **kwargs):
211         self.retry_after = (int(kwargs['retry']) if kwargs.get('retry')
212                             else None)
213         super(ServiceUnavailable, self).__init__(*args, **kwargs)
214
215
216 class ServerError(EscalatorException):
217     message = _("The request returned 500 Internal Server Error.")
218
219
220 class UnexpectedStatus(EscalatorException):
221     message = _("The request returned an unexpected status: %(status)s."
222                 "\n\nThe response body:\n%(body)s")
223
224
225 class InvalidContentType(EscalatorException):
226     message = _("Invalid content type %(content_type)s")
227
228
229 class BadRegistryConnectionConfiguration(EscalatorException):
230     message = _("Registry was not configured correctly on API server. "
231                 "Reason: %(reason)s")
232
233
234 class BadDriverConfiguration(EscalatorException):
235     message = _("Driver %(driver_name)s could not be configured correctly. "
236                 "Reason: %(reason)s")
237
238
239 class MaxRedirectsExceeded(EscalatorException):
240     message = _("Maximum redirects (%(redirects)s) was exceeded.")
241
242
243 class InvalidRedirect(EscalatorException):
244     message = _("Received invalid HTTP redirect.")
245
246
247 class NoServiceEndpoint(EscalatorException):
248     message = _("Response from Keystone does not contain a Glance endpoint.")
249
250
251 class RegionAmbiguity(EscalatorException):
252     message = _("Multiple 'image' service matches for region %(region)s. This "
253                 "generally means that a region is required and you have not "
254                 "supplied one.")
255
256
257 class WorkerCreationFailure(EscalatorException):
258     message = _("Server worker creation failed: %(reason)s.")
259
260
261 class SchemaLoadError(EscalatorException):
262     message = _("Unable to load schema: %(reason)s")
263
264
265 class InvalidObject(EscalatorException):
266     message = _("Provided object does not match schema "
267                 "'%(schema)s': %(reason)s")
268
269
270 class UnsupportedHeaderFeature(EscalatorException):
271     message = _("Provided header feature is unsupported: %(feature)s")
272
273
274 class InUseByStore(EscalatorException):
275     message = _("The image cannot be deleted because it is in use through "
276                 "the backend store outside of escalator.")
277
278
279 class SIGHUPInterrupt(EscalatorException):
280     message = _("System SIGHUP signal received.")
281
282
283 class RPCError(EscalatorException):
284     message = _("%(cls)s exception was raised in the last rpc call: %(val)s")
285
286
287 class TaskException(EscalatorException):
288     message = _("An unknown task exception occurred")
289
290
291 class BadTaskConfiguration(EscalatorException):
292     message = _("Task was not configured properly")
293
294
295 class TaskNotFound(TaskException, NotFound):
296     message = _("Task with the given id %(task_id)s was not found")
297
298
299 class InvalidTaskStatus(TaskException, Invalid):
300     message = _("Provided status of task is unsupported: %(status)s")
301
302
303 class InvalidTaskType(TaskException, Invalid):
304     message = _("Provided type of task is unsupported: %(type)s")
305
306
307 class InvalidTaskStatusTransition(TaskException, Invalid):
308     message = _("Status transition from %(cur_status)s to"
309                 " %(new_status)s is not allowed")
310
311
312 class DuplicateLocation(Duplicate):
313     message = _("The location %(location)s already exists")
314
315
316 class InvalidParameterValue(Invalid):
317     message = _("Invalid value '%(value)s' for parameter '%(param)s': "
318                 "%(extra_msg)s")
319
320
321 class MetadefDuplicateNamespace(Duplicate):
322     message = _("The metadata definition namespace=%(namespace_name)s"
323                 " already exists.")
324
325
326 class MetadefDuplicateObject(Duplicate):
327     message = _("A metadata definition object with name=%(object_name)s"
328                 " already exists in namespace=%(namespace_name)s.")
329
330
331 class MetadefDuplicateProperty(Duplicate):
332     message = _("A metadata definition property with name=%(property_name)s"
333                 " already exists in namespace=%(namespace_name)s.")
334
335
336 class MetadefDuplicateResourceType(Duplicate):
337     message = _("A metadata definition resource-type with"
338                 " name=%(resource_type_name)s already exists.")
339
340
341 class MetadefDuplicateResourceTypeAssociation(Duplicate):
342     message = _("The metadata definition resource-type association of"
343                 " resource-type=%(resource_type_name)s to"
344                 " namespace=%(namespace_name)s"
345                 " already exists.")
346
347
348 class MetadefDuplicateTag(Duplicate):
349     message = _("A metadata tag with name=%(name)s"
350                 " already exists in namespace=%(namespace_name)s.")
351
352
353 class MetadefForbidden(Forbidden):
354     message = _("You are not authorized to complete this action.")
355
356
357 class MetadefIntegrityError(Forbidden):
358     message = _("The metadata definition %(record_type)s with"
359                 " name=%(record_name)s not deleted."
360                 " Other records still refer to it.")
361
362
363 class MetadefNamespaceNotFound(NotFound):
364     message = _("Metadata definition namespace=%(namespace_name)s"
365                 "was not found.")
366
367
368 class MetadefObjectNotFound(NotFound):
369     message = _("The metadata definition object with"
370                 " name=%(object_name)s was not found in"
371                 " namespace=%(namespace_name)s.")
372
373
374 class MetadefPropertyNotFound(NotFound):
375     message = _("The metadata definition property with"
376                 " name=%(property_name)s was not found in"
377                 " namespace=%(namespace_name)s.")
378
379
380 class MetadefResourceTypeNotFound(NotFound):
381     message = _("The metadata definition resource-type with"
382                 " name=%(resource_type_name)s, was not found.")
383
384
385 class MetadefResourceTypeAssociationNotFound(NotFound):
386     message = _("The metadata definition resource-type association of"
387                 " resource-type=%(resource_type_name)s to"
388                 " namespace=%(namespace_name)s,"
389                 " was not found.")
390
391
392 class MetadefTagNotFound(NotFound):
393     message = _("The metadata definition tag with"
394                 " name=%(name)s was not found in"
395                 " namespace=%(namespace_name)s.")
396
397
398 class InvalidVersion(Invalid):
399     message = _("Version is invalid: %(reason)s")
400
401
402 class InvalidArtifactTypePropertyDefinition(Invalid):
403     message = _("Invalid property definition")
404
405
406 class InvalidArtifactTypeDefinition(Invalid):
407     message = _("Invalid type definition")
408
409
410 class InvalidArtifactPropertyValue(Invalid):
411     message = _("Property '%(name)s' may not have value '%(val)s': %(msg)s")
412
413     def __init__(self, message=None, *args, **kwargs):
414         super(InvalidArtifactPropertyValue, self).__init__(message, *args,
415                                                            **kwargs)
416         self.name = kwargs.get('name')
417         self.value = kwargs.get('val')
418
419
420 class ArtifactNotFound(NotFound):
421     message = _("Artifact with id=%(id)s was not found")
422
423
424 class ArtifactForbidden(Forbidden):
425     message = _("Artifact with id=%(id)s is not accessible")
426
427
428 class ArtifactDuplicateNameTypeVersion(Duplicate):
429     message = _("Artifact with the specified type, name and version"
430                 " already exists")
431
432
433 class InvalidArtifactStateTransition(Invalid):
434     message = _("Artifact cannot change state from %(source)s to %(target)s")
435
436
437 class ArtifactDuplicateDirectDependency(Duplicate):
438     message = _("Artifact with the specified type, name and version"
439                 " already has the direct dependency=%(dep)s")
440
441
442 class ArtifactDuplicateTransitiveDependency(Duplicate):
443     message = _("Artifact with the specified type, name and version"
444                 " already has the transitive dependency=%(dep)s")
445
446
447 class ArtifactUnsupportedPropertyOperator(Invalid):
448     message = _("Operator %(op)s is not supported")
449
450
451 class ArtifactUnsupportedShowLevel(Invalid):
452     message = _("Show level %(shl)s is not supported in this operation")
453
454
455 class ArtifactPropertyValueNotFound(NotFound):
456     message = _("Property's %(prop)s value has not been found")
457
458
459 class ArtifactInvalidProperty(Invalid):
460     message = _("Artifact has no property %(prop)s")
461
462
463 class ArtifactInvalidPropertyParameter(Invalid):
464     message = _("Cannot use this parameter with the operator %(op)s")
465
466
467 class ArtifactLoadError(EscalatorException):
468     message = _("Cannot load artifact '%(name)s'")
469
470
471 class ArtifactNonMatchingTypeName(ArtifactLoadError):
472     message = _(
473         "Plugin name '%(plugin)s' should match artifact typename '%(name)s'")
474
475
476 class ArtifactPluginNotFound(NotFound):
477     message = _("No plugin for '%(name)s' has been loaded")
478
479
480 class UnknownArtifactType(NotFound):
481     message = _("Artifact type with name '%(name)s' and version '%(version)s' "
482                 "is not known")
483
484
485 class ArtifactInvalidStateTransition(Invalid):
486     message = _("Artifact state cannot be changed from %(curr)s to %(to)s")
487
488
489 class JsonPatchException(EscalatorException):
490     message = _("Invalid jsonpatch request")
491
492
493 class InvalidJsonPatchBody(JsonPatchException):
494     message = _("The provided body %(body)s is invalid "
495                 "under given schema: %(schema)s")
496
497
498 class InvalidJsonPatchPath(JsonPatchException):
499     message = _("The provided path '%(path)s' is invalid: %(explanation)s")
500
501     def __init__(self, message=None, *args, **kwargs):
502         self.explanation = kwargs.get("explanation")
503         super(InvalidJsonPatchPath, self).__init__(message, *args, **kwargs)
504
505
506 class ThreadBinException(EscalatorException):
507
508     def __init__(self, *args):
509         super(ThreadBinException, self).__init__(*args)
510
511
512 class SubprocessCmdFailed(EscalatorException):
513     message = _("suprocess command failed.")
514
515
516 class DeleteConstrainted(EscalatorException):
517     message = _("delete is not allowed.")
518
519
520 class TrustMeFailed(EscalatorException):
521     message = _("Trust me script failed.")