summaryrefslogtreecommitdiffstats
path: root/api/escalator/common/exception.py
blob: 690507422bf4c6a3119546ec0efd65645d8519dc (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Escalator exception subclasses"""

import six
import six.moves.urllib.parse as urlparse

from escalator import i18n

_ = i18n._

_FATAL_EXCEPTION_FORMAT_ERRORS = False


class RedirectException(Exception):

    def __init__(self, url):
        self.url = urlparse.urlparse(url)


class EscalatorException(Exception):
    """
    Base Escalator Exception

    To correctly use this class, inherit from it and define
    a 'message' property. That message will get printf'd
    with the keyword arguments provided to the constructor.
    """
    message = _("An unknown exception occurred")

    def __init__(self, message=None, *args, **kwargs):
        if not message:
            message = self.message
        try:
            if kwargs:
                message = message % kwargs
        except Exception:
            if _FATAL_EXCEPTION_FORMAT_ERRORS:
                raise
            else:
                # at least get the core message out if something happened
                pass
        self.msg = message
        self.message = message
        super(EscalatorException, self).__init__(message)

    def __unicode__(self):
        # NOTE(flwang): By default, self.msg is an instance of Message, which
        # can't be converted by str(). Based on the definition of
        # __unicode__, it should return unicode always.
        return six.text_type(self.msg)


class MissingCredentialError(EscalatorException):
    message = _("Missing required credential: %(required)s")


class BadAuthStrategy(EscalatorException):
    message = _("Incorrect auth strategy, expected \"%(expected)s\" but "
                "received \"%(received)s\"")


class NotFound(EscalatorException):
    message = _("An object with the specified identifier was not found.")


class BadStoreUri(EscalatorException):
    message = _("The Store URI was malformed.")


class Duplicate(EscalatorException):
    message = _("An object with the same identifier already exists.")


class Conflict(EscalatorException):
    message = _("An object with the same identifier is currently being "
                "operated on.")


class AuthBadRequest(EscalatorException):
    message = _("Connect error/bad request to Auth service at URL %(url)s.")


class AuthUrlNotFound(EscalatorException):
    message = _("Auth service at URL %(url)s not found.")


class AuthorizationFailure(EscalatorException):
    message = _("Authorization failed.")


class NotAuthenticated(EscalatorException):
    message = _("You are not authenticated.")


class Forbidden(EscalatorException):
    message = _("You are not authorized to complete this action.")


class ProtectedMetadefNamespaceDelete(Forbidden):
    message = _("Metadata definition namespace %(namespace)s is protected"
                " and cannot be deleted.")


class ProtectedMetadefNamespacePropDelete(Forbidden):
    message = _("Metadata definition property %(property_name)s is protected"
                " and cannot be deleted.")


class ProtectedMetadefObjectDelete(Forbidden):
    message = _("Metadata definition object %(object_name)s is protected"
                " and cannot be deleted.")


class ProtectedMetadefResourceTypeAssociationDelete(Forbidden):
    message = _("Metadata definition resource-type-association"
                " %(resource_type)s is protected and cannot be deleted.")


class ProtectedMetadefResourceTypeSystemDelete(Forbidden):
    message = _("Metadata definition resource-type %(resource_type_name)s is"
                " a seeded-system type and cannot be deleted.")


class ProtectedMetadefTagDelete(Forbidden):
    message = _("Metadata definition tag %(tag_name)s is protected"
                " and cannot be deleted.")


class Invalid(EscalatorException):
    message = _("Data supplied was not valid.")


class InvalidSortKey(Invalid):
    message = _("Sort key supplied was not valid.")


class InvalidSortDir(Invalid):
    message = _("Sort direction supplied was not valid.")


class InvalidPropertyProtectionConfiguration(Invalid):
    message = _("Invalid configuration in property protection file.")


class InvalidFilterRangeValue(Invalid):
    message = _("Unable to filter using the specified range.")


class InvalidOptionValue(Invalid):
    message = _("Invalid value for option %(option)s: %(value)s")


class ReadonlyProperty(Forbidden):
    message = _("Attribute '%(property)s' is read-only.")


class ReservedProperty(Forbidden):
    message = _("Attribute '%(property)s' is reserved.")


class AuthorizationRedirect(EscalatorException):
    message = _("Redirecting to %(uri)s for authorization.")


class ClientConnectionError(EscalatorException):
    message = _("There was an error connecting to a server")


class ClientConfigurationError(EscalatorException):
    message = _("There was an error configuring the client.")


class MultipleChoices(EscalatorException):
    message = _("The request returned a 302 Multiple Choices. This generally "
                "means that you have not included a version indicator in a "
                "request URI.\n\nThe body of response returned:\n%(body)s")


class LimitExceeded(EscalatorException):
    message = _("The request returned a 413 Request Entity Too Large. This "
                "generally means that rate limiting or a quota threshold was "
                "breached.\n\nThe response body:\n%(body)s")

    def __init__(self, *args, **kwargs):
        self.retry_after = (int(kwargs['retry']) if kwargs.get('retry')
                            else None)
        super(LimitExceeded, self).__init__(*args, **kwargs)


class ServiceUnavailable(EscalatorException):
    message = _("The request returned 503 Service Unavailable. This "
                "generally occurs on service overload or other transient "
                "outage.")

    def __init__(self, *args, **kwargs):
        self.retry_after = (int(kwargs['retry']) if kwargs.get('retry')
                            else None)
        super(ServiceUnavailable, self).__init__(*args, **kwargs)


class ServerError(EscalatorException):
    message = _("The request returned 500 Internal Server Error.")


class UnexpectedStatus(EscalatorException):
    message = _("The request returned an unexpected status: %(status)s."
                "\n\nThe response body:\n%(body)s")


class InvalidContentType(EscalatorException):
    message = _("Invalid content type %(content_type)s")


class BadRegistryConnectionConfiguration(EscalatorException):
    message = _("Registry was not configured correctly on API server. "
                "Reason: %(reason)s")


class BadDriverConfiguration(EscalatorException):
    message = _("Driver %(driver_name)s could not be configured correctly. "
                "Reason: %(reason)s")


class MaxRedirectsExceeded(EscalatorException):
    message = _("Maximum redirects (%(redirects)s) was exceeded.")


class InvalidRedirect(EscalatorException):
    message = _("Received invalid HTTP redirect.")


class NoServiceEndpoint(EscalatorException):
    message = _("Response from Keystone does not contain a Glance endpoint.")


class RegionAmbiguity(EscalatorException):
    message = _("Multiple 'image' service matches for region %(region)s. This "
                "generally means that a region is required and you have not "
                "supplied one.")


class WorkerCreationFailure(EscalatorException):
    message = _("Server worker creation failed: %(reason)s.")


class SchemaLoadError(EscalatorException):
    message = _("Unable to load schema: %(reason)s")


class InvalidObject(EscalatorException):
    message = _("Provided object does not match schema "
                "'%(schema)s': %(reason)s")


class UnsupportedHeaderFeature(EscalatorException):
    message = _("Provided header feature is unsupported: %(feature)s")


class InUseByStore(EscalatorException):
    message = _("The image cannot be deleted because it is in use through "
                "the backend store outside of escalator.")


class SIGHUPInterrupt(EscalatorException):
    message = _("System SIGHUP signal received.")


class RPCError(EscalatorException):
    message = _("%(cls)s exception was raised in the last rpc call: %(val)s")


class TaskException(EscalatorException):
    message = _("An unknown task exception occurred")


class BadTaskConfiguration(EscalatorException):
    message = _("Task was not configured properly")


class TaskNotFound(TaskException, NotFound):
    message = _("Task with the given id %(task_id)s was not found")


class InvalidTaskStatus(TaskException, Invalid):
    message = _("Provided status of task is unsupported: %(status)s")


class InvalidTaskType(TaskException, Invalid):
    message = _("Provided type of task is unsupported: %(type)s")


class InvalidTaskStatusTransition(TaskException, Invalid):
    message = _("Status transition from %(cur_status)s to"
                " %(new_status)s is not allowed")


class DuplicateLocation(Duplicate):
    message = _("The location %(location)s already exists")


class InvalidParameterValue(Invalid):
    message = _("Invalid value '%(value)s' for parameter '%(param)s': "
                "%(extra_msg)s")


class MetadefDuplicateNamespace(Duplicate):
    message = _("The metadata definition namespace=%(namespace_name)s"
                " already exists.")


class MetadefDuplicateObject(Duplicate):
    message = _("A metadata definition object with name=%(object_name)s"
                " already exists in namespace=%(namespace_name)s.")


class MetadefDuplicateProperty(Duplicate):
    message = _("A metadata definition property with name=%(property_name)s"
                " already exists in namespace=%(namespace_name)s.")


class MetadefDuplicateResourceType(Duplicate):
    message = _("A metadata definition resource-type with"
                " name=%(resource_type_name)s already exists.")


class MetadefDuplicateResourceTypeAssociation(Duplicate):
    message = _("The metadata definition resource-type association of"
                " resource-type=%(resource_type_name)s to"
                " namespace=%(namespace_name)s"
                " already exists.")


class MetadefDuplicateTag(Duplicate):
    message = _("A metadata tag with name=%(name)s"
                " already exists in namespace=%(namespace_name)s.")


class MetadefForbidden(Forbidden):
    message = _("You are not authorized to complete this action.")


class MetadefIntegrityError(Forbidden):
    message = _("The metadata definition %(record_type)s with"
                " name=%(record_name)s not deleted."
                " Other records still refer to it.")


class MetadefNamespaceNotFound(NotFound):
    message = _("Metadata definition namespace=%(namespace_name)s"
                "was not found.")


class MetadefObjectNotFound(NotFound):
    message = _("The metadata definition object with"
                " name=%(object_name)s was not found in"
                " namespace=%(namespace_name)s.")


class MetadefPropertyNotFound(NotFound):
    message = _("The metadata definition property with"
                " name=%(property_name)s was not found in"
                " namespace=%(namespace_name)s.")


class MetadefResourceTypeNotFound(NotFound):
    message = _("The metadata definition resource-type with"
                " name=%(resource_type_name)s, was not found.")


class MetadefResourceTypeAssociationNotFound(NotFound):
    message = _("The metadata definition resource-type association of"
                " resource-type=%(resource_type_name)s to"
                " namespace=%(namespace_name)s,"
                " was not found.")


class MetadefTagNotFound(NotFound):
    message = _("The metadata definition tag with"
                " name=%(name)s was not found in"
                " namespace=%(namespace_name)s.")


class InvalidVersion(Invalid):
    message = _("Version is invalid: %(reason)s")


class InvalidArtifactTypePropertyDefinition(Invalid):
    message = _("Invalid property definition")


class InvalidArtifactTypeDefinition(Invalid):
    message = _("Invalid type definition")


class InvalidArtifactPropertyValue(Invalid):
    message = _("Property '%(name)s' may not have value '%(val)s': %(msg)s")

    def __init__(self, message=None, *args, **kwargs):
        super(InvalidArtifactPropertyValue, self).__init__(message, *args,
                                                           **kwargs)
        self.name = kwargs.get('name')
        self.value = kwargs.get('val')


class ArtifactNotFound(NotFound):
    message = _("Artifact with id=%(id)s was not found")


class ArtifactForbidden(Forbidden):
    message = _("Artifact with id=%(id)s is not accessible")


class ArtifactDuplicateNameTypeVersion(Duplicate):
    message = _("Artifact with the specified type, name and version"
                " already exists")


class InvalidArtifactStateTransition(Invalid):
    message = _("Artifact cannot change state from %(source)s to %(target)s")


class ArtifactDuplicateDirectDependency(Duplicate):
    message = _("Artifact with the specified type, name and version"
                " already has the direct dependency=%(dep)s")


class ArtifactDuplicateTransitiveDependency(Duplicate):
    message = _("Artifact with the specified type, name and version"
                " already has the transitive dependency=%(dep)s")


class ArtifactUnsupportedPropertyOperator(Invalid):
    message = _("Operator %(op)s is not supported")


class ArtifactUnsupportedShowLevel(Invalid):
    message = _("Show level %(shl)s is not supported in this operation")


class ArtifactPropertyValueNotFound(NotFound):
    message = _("Property's %(prop)s value has not been found")


class ArtifactInvalidProperty(Invalid):
    message = _("Artifact has no property %(prop)s")


class ArtifactInvalidPropertyParameter(Invalid):
    message = _("Cannot use this parameter with the operator %(op)s")


class ArtifactLoadError(EscalatorException):
    message = _("Cannot load artifact '%(name)s'")


class ArtifactNonMatchingTypeName(ArtifactLoadError):
    message = _(
        "Plugin name '%(plugin)s' should match artifact typename '%(name)s'")


class ArtifactPluginNotFound(NotFound):
    message = _("No plugin for '%(name)s' has been loaded")


class UnknownArtifactType(NotFound):
    message = _("Artifact type with name '%(name)s' and version '%(version)s' "
                "is not known")


class ArtifactInvalidStateTransition(Invalid):
    message = _("Artifact state cannot be changed from %(curr)s to %(to)s")


class JsonPatchException(EscalatorException):
    message = _("Invalid jsonpatch request")


class InvalidJsonPatchBody(JsonPatchException):
    message = _("The provided body %(body)s is invalid "
                "under given schema: %(schema)s")


class InvalidJsonPatchPath(JsonPatchException):
    message = _("The provided path '%(path)s' is invalid: %(explanation)s")

    def __init__(self, message=None, *args, **kwargs):
        self.explanation = kwargs.get("explanation")
        super(InvalidJsonPatchPath, self).__init__(message, *args, **kwargs)


class ThreadBinException(EscalatorException):

    def __init__(self, *args):
        super(ThreadBinException, self).__init__(*args)


class SubprocessCmdFailed(EscalatorException):
    message = _("suprocess command failed.")


class DeleteConstrainted(EscalatorException):
    message = _("delete is not allowed.")


class TrustMeFailed(EscalatorException):
    message = _("Trust me script failed.")