diff options
| author | John Tran <jtran@attinteractive.com> | 2011-05-02 18:04:29 +0000 |
|---|---|---|
| committer | Tarmac <> | 2011-05-02 18:04:29 +0000 |
| commit | 42c8e73994c7d787aef0e2d97ea162ee5a2e1304 (patch) | |
| tree | d6bacfaf33137aedf4dbc4d5b91221f8af138a11 | |
| parent | 585a1819f365018a4536bf7364722965ba994845 (diff) | |
| parent | c3ab4f023e2636e254f940e08da0aded42c0e96b (diff) | |
| download | nova-42c8e73994c7d787aef0e2d97ea162ee5a2e1304.tar.gz nova-42c8e73994c7d787aef0e2d97ea162ee5a2e1304.tar.xz nova-42c8e73994c7d787aef0e2d97ea162ee5a2e1304.zip | |
ApiError 'code' arg set to None, and will only display a 'code' as part of the str if specified.
| -rw-r--r-- | nova/exception.py | 8 | ||||
| -rw-r--r-- | nova/tests/test_exception.py | 34 |
2 files changed, 40 insertions, 2 deletions
diff --git a/nova/exception.py b/nova/exception.py index e8444cb14..8cdd0cb5a 100644 --- a/nova/exception.py +++ b/nova/exception.py @@ -49,10 +49,14 @@ class Error(Exception): class ApiError(Error): - def __init__(self, message='Unknown', code='ApiError'): + def __init__(self, message='Unknown', code=None): self.message = message self.code = code - super(ApiError, self).__init__('%s: %s' % (code, message)) + if code: + outstr = '%s: %s' % (code, message) + else: + outstr = '%s' % message + super(ApiError, self).__init__(outstr) class DBError(Error): diff --git a/nova/tests/test_exception.py b/nova/tests/test_exception.py new file mode 100644 index 000000000..1b0e41d9a --- /dev/null +++ b/nova/tests/test_exception.py @@ -0,0 +1,34 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# 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. + +from nova import test +from nova import exception + + +class ApiErrorTestCase(test.TestCase): + def test_return_valid_error(self): + # without 'code' arg + err = exception.ApiError('fake error') + self.assertEqual(err.__str__(), 'fake error') + self.assertEqual(err.code, None) + self.assertEqual(err.message, 'fake error') + # with 'code' arg + err = exception.ApiError('fake error', 'blah code') + self.assertEqual(err.__str__(), 'blah code: fake error') + self.assertEqual(err.code, 'blah code') + self.assertEqual(err.message, 'fake error') |
