1# Copyright (c) 2015 The Johns Hopkins University/Applied Physics Laboratory
2# All Rights Reserved.
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15
16"""
17Castellan exception subclasses
18"""
19
20import urllib
21
22from castellan.i18n import _
23
24_FATAL_EXCEPTION_FORMAT_ERRORS = False
25
26
27class RedirectException(Exception):
28    def __init__(self, url):
29        self.url = urllib.parse.urlparse(url)
30
31
32class CastellanException(Exception):
33    """Base Castellan Exception
34
35    To correctly use this class, inherit from it and define
36    a 'message' property. That message will get printf'd
37    with the keyword arguments provided to the constructor.
38    """
39    message = _("An unknown exception occurred")
40
41    def __init__(self, message_arg=None, *args, **kwargs):
42        if not message_arg:
43            message_arg = self.message
44        try:
45            self.message = message_arg % kwargs
46        except Exception:
47            if _FATAL_EXCEPTION_FORMAT_ERRORS:
48                raise
49            else:
50                # at least get the core message out if something happened
51                pass
52        super(CastellanException, self).__init__(self.message)
53
54
55class Forbidden(CastellanException):
56    message = _("You are not authorized to complete this action.")
57
58
59class KeyManagerError(CastellanException):
60    message = _("Key manager error: %(reason)s")
61
62
63class ManagedObjectNotFoundError(CastellanException):
64    message = _("Key not found, uuid: %(uuid)s")
65
66
67class InvalidManagedObjectDictError(CastellanException):
68    message = _("Dict has no field '%(field)s'.")
69
70
71class UnknownManagedObjectTypeError(CastellanException):
72    message = _("Type not found, type: %(type)s")
73
74
75class AuthTypeInvalidError(CastellanException):
76    message = _("Invalid auth_type was specified, auth_type: %(type)s")
77
78
79class InsufficientCredentialDataError(CastellanException):
80    message = _("Insufficient credential data was provided, either "
81                "\"token\" must be set in the passed conf, or a context "
82                "with an \"auth_token\" property must be passed.")
83