diff --git a/mistral/exceptions.py b/mistral/exceptions.py index 6dea06416..e854321d3 100644 --- a/mistral/exceptions.py +++ b/mistral/exceptions.py @@ -21,96 +21,56 @@ class MistralException(ex.Error): """Base Exception for the project To correctly use this class, inherit from it and define - a 'message' and 'code' properties. + a 'message' and 'http_code' properties. """ message = "An unknown exception occurred" - code = "UNKNOWN_EXCEPTION" + http_code = 500 def __str__(self): return self.message - def __init__(self, message=message): - self.message = message + def __init__(self, message=None): + if message is not None: + self.message = message super(MistralException, self).__init__( - '%s: %s' % (self.code, self.message)) + '%d: %s' % (self.http_code, self.message)) class DataAccessException(MistralException): - def __init__(self, message=None): - super(DataAccessException, self).__init__(message) - if message: - self.message = message + http_code = 400 class NotFoundException(MistralException): + http_code = 404 message = "Object not found" - def __init__(self, message=None): - super(NotFoundException, self).__init__(message) - if message: - self.message = message - class DBDuplicateEntry(MistralException): + http_code = 409 message = "Database object already exists" - code = "DB_DUPLICATE_ENTRY" - - def __init__(self, message=None): - super(DBDuplicateEntry, self).__init__(message) - if message: - self.message = message class ActionException(MistralException): - code = "ACTION_ERROR" - - def __init__(self, message=None): - super(MistralException, self).__init__(message) - if message: - self.message = message + http_code = 400 class InvalidActionException(MistralException): - def __init__(self, message=None): - super(InvalidActionException, self).__init__(message) - if message: - self.message = message + http_code = 400 class ActionRegistrationException(MistralException): message = "Failed to register action" - code = "ACTION_REGISTRATION_ERROR" - - def __init__(self, message=None): - super(ActionRegistrationException, self).__init__(message) - if message: - self.message = message class EngineException(MistralException): - code = "ENGINE_ERROR" - - def __init__(self, message=None): - super(EngineException, self).__init__(message) - if message: - self.message = message + pass class ApplicationContextNotFoundException(MistralException): + http_code = 400 message = "Application context not found" - code = "APP_CTX_NOT_FOUND_ERROR" - - def __init__(self, message=None): - super(ApplicationContextNotFoundException, self).__init__(message) - if message: - self.message = message class InvalidModelException(MistralException): + http_code = 400 message = "Wrong entity definition" - code = "INVALID_MODEL_EXCEPTION" - - def __init__(self, message=None): - super(InvalidModelException, self).__init__(message) - if message: - self.message = message diff --git a/mistral/tests/unit/test_exception_base.py b/mistral/tests/unit/test_exception_base.py new file mode 100644 index 000000000..7dbfc6786 --- /dev/null +++ b/mistral/tests/unit/test_exception_base.py @@ -0,0 +1,51 @@ +# -*- encoding: utf-8 -*- +# +# Copyright 2014 Rackspace Hosting. +# +# 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. + +import six + +from mistral import exceptions +from mistral.tests import base + + +class ExceptionTestCase(base.BaseTest): + """Test cases for exception code.""" + + def test_nf_with_message(self): + exc = exceptions.NotFoundException('check_for_this') + self.assertIn('check_for_this', + six.text_type(exc)) + self.assertEqual(exc.http_code, 404) + + def test_nf_with_no_message(self): + exc = exceptions.NotFoundException() + self.assertIn("Object not found", + six.text_type(exc)) + self.assertEqual(exc.http_code, 404) + + def test_duplicate_obj_code(self): + exc = exceptions.DBDuplicateEntry() + self.assertIn("Database object already exists", + six.text_type(exc)) + self.assertEqual(exc.http_code, 409) + + def test_default_code(self): + exc = exceptions.EngineException() + self.assertEqual(exc.http_code, 500) + + def test_default_message(self): + exc = exceptions.EngineException() + self.assertIn("An unknown exception occurred", + six.text_type(exc))