1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
14 TOSCA exception classes
20 from toscaparser.utils.gettextutils import _
23 log = logging.getLogger(__name__)
26 class TOSCAException(Exception):
27 '''Base exception class for TOSCA
29 To correctly use this class, inherit from it and define
34 _FATAL_EXCEPTION_FORMAT_ERRORS = False
36 message = _('An unknown exception occurred.')
38 def __init__(self, **kwargs):
40 self.message = self.msg_fmt % kwargs
42 exc_info = sys.exc_info()
43 log.exception(_('Exception in string format operation: %s')
46 if TOSCAException._FATAL_EXCEPTION_FORMAT_ERRORS:
53 def generate_inv_schema_property_error(self, attr, value, valid_values):
54 msg = (_('Schema definition of "%(propname)s" has '
55 '"%(attr)s" attribute with invalid value '
56 '"%(value1)s". The value must be one of '
57 '"%(value2)s".') % {"propname": self.name,
60 "value2": valid_values})
61 ExceptionCollector.appendException(
62 InvalidSchemaError(message=msg))
65 def set_fatal_format_exception(flag):
66 if isinstance(flag, bool):
67 TOSCAException._FATAL_EXCEPTION_FORMAT_ERRORS = flag
70 class MissingRequiredFieldError(TOSCAException):
71 msg_fmt = _('%(what)s is missing required field "%(required)s".')
74 class UnknownFieldError(TOSCAException):
75 msg_fmt = _('%(what)s contains unknown field "%(field)s". Refer to the '
76 'definition to verify valid values.')
79 class TypeMismatchError(TOSCAException):
80 msg_fmt = _('%(what)s must be of type "%(type)s".')
83 class InvalidNodeTypeError(TOSCAException):
84 msg_fmt = _('Node type "%(what)s" is not a valid type.')
87 class InvalidTypeError(TOSCAException):
88 msg_fmt = _('Type "%(what)s" is not a valid type.')
91 class InvalidTypeAdditionalRequirementsError(TOSCAException):
92 msg_fmt = _('Additional requirements for type "%(type)s" not met.')
95 class RangeValueError(TOSCAException):
96 msg_fmt = _('The value "%(pvalue)s" of property "%(pname)s" is out of '
97 'range "(min:%(vmin)s, max:%(vmax)s)".')
100 class InvalidSchemaError(TOSCAException):
101 msg_fmt = _('%(message)s')
104 class ValidationError(TOSCAException):
105 msg_fmt = _('%(message)s')
108 class UnknownInputError(TOSCAException):
109 msg_fmt = _('Unknown input "%(input_name)s".')
112 class InvalidPropertyValueError(TOSCAException):
113 msg_fmt = _('Value of property "%(what)s" is invalid.')
116 class InvalidTemplateVersion(TOSCAException):
117 msg_fmt = _('The template version "%(what)s" is invalid. '
118 'Valid versions are "%(valid_versions)s".')
121 class InvalidTOSCAVersionPropertyException(TOSCAException):
122 msg_fmt = _('Value of TOSCA version property "%(what)s" is invalid.')
125 class URLException(TOSCAException):
126 msg_fmt = _('%(what)s')
129 class ToscaExtImportError(TOSCAException):
130 msg_fmt = _('Unable to import extension "%(ext_name)s". '
131 'Check to see that it exists and has no '
132 'language definition errors.')
135 class ToscaExtAttributeError(TOSCAException):
136 msg_fmt = _('Missing attribute in extension "%(ext_name)s". '
137 'Check to see that it has required attributes '
138 '"%(attrs)s" defined.')
141 class InvalidGroupTargetException(TOSCAException):
142 msg_fmt = _('"%(message)s"')
145 class ExceptionCollector(object):
152 del ExceptionCollector.exceptions[:]
156 ExceptionCollector.clear()
157 ExceptionCollector.collecting = True
161 ExceptionCollector.collecting = False
164 def contains(exception):
165 for ex in ExceptionCollector.exceptions:
166 if str(ex) == str(exception):
171 def appendException(exception):
172 if ExceptionCollector.collecting:
173 if not ExceptionCollector.contains(exception):
174 exception.trace = traceback.extract_stack()[:-1]
175 ExceptionCollector.exceptions.append(exception)
180 def exceptionsCaught():
181 return len(ExceptionCollector.exceptions) > 0
184 def getTraceString(traceList):
186 for entry in traceList:
187 f, l, m, c = entry[0], entry[1], entry[2], entry[3]
188 traceString += (_('\t\tFile %(file)s, line %(line)s, in '
189 '%(method)s\n\t\t\t%(call)s\n')
190 % {'file': f, 'line': l, 'method': m, 'call': c})
194 def getExceptionReportEntry(exception, full=True):
195 entry = exception.__class__.__name__ + ': ' + str(exception)
197 entry += '\n' + ExceptionCollector.getTraceString(exception.trace)
202 return ExceptionCollector.exceptions
205 def getExceptionsReport(full=True):
207 for exception in ExceptionCollector.exceptions:
209 ExceptionCollector.getExceptionReportEntry(exception, full))
213 def assertExceptionMessage(exception, message):
214 err_msg = exception.__name__ + ': ' + message
215 report = ExceptionCollector.getExceptionsReport(False)
216 assert err_msg in report, (_('Could not find "%(msg)s" in "%(rep)s".')
217 % {'rep': report.__str__(), 'msg': err_msg})