diff options
author | jiataotj <jiataotj@cn.ibm.com> | 2013-05-31 00:18:28 +0800 |
---|---|---|
committer | jiataotj <jiataotj@cn.ibm.com> | 2013-07-26 13:43:27 +0800 |
commit | f976bbe697002367ff00a0588a3181fd42008e1c (patch) | |
tree | a5763c4d4725ec29bb631044bd071e1ff77248c7 /keystone/exception.py | |
parent | 0a401529947c98ab761256e233b157071a57ab70 (diff) | |
download | keystone-f976bbe697002367ff00a0588a3181fd42008e1c.tar.gz keystone-f976bbe697002367ff00a0588a3181fd42008e1c.tar.xz keystone-f976bbe697002367ff00a0588a3181fd42008e1c.zip |
Implement exception module i18n support
The doc string in exception.py of Keystone will be
returned with __doc__ method, but cannot realize the
internationalization.Change exception module to enable
i18n support.
Changes in the patch are:
1, useing class variable msg_fmt to replace class __doc__
2, modify wsgi.render_exception function using unicode
function to replace str function
3, modify/add UT test cases
Fixes: bug # 1179425
Change-Id: I75c1229c905a2625d2f6961d1a8dd3958eac51a5
Diffstat (limited to 'keystone/exception.py')
-rw-r--r-- | keystone/exception.py | 128 |
1 files changed, 58 insertions, 70 deletions
diff --git a/keystone/exception.py b/keystone/exception.py index db5f5005..5e1defba 100644 --- a/keystone/exception.py +++ b/keystone/exception.py @@ -13,10 +13,10 @@ # 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 re from keystone.common import config from keystone.common import logging +from keystone.openstack.common.gettextutils import _ # noqa CONF = config.CONF @@ -29,15 +29,15 @@ _FATAL_EXCEPTION_FORMAT_ERRORS = False class Error(StandardError): """Base error class. - Child classes should define an HTTP status code, title, and a doc string. + Child classes should define an HTTP status code, title, and a + message_format. """ code = None title = None + message_format = None def __init__(self, message=None, **kwargs): - """Use the doc string as the error message by default.""" - try: message = self._build_message(message, **kwargs) except KeyError: @@ -45,8 +45,8 @@ class Error(StandardError): if _FATAL_EXCEPTION_FORMAT_ERRORS: raise else: - LOG.warning('missing exception kwargs (programmer error)') - message = self.__doc__ + LOG.warning(_('missing exception kwargs (programmer error)')) + message = self.message_format super(Error, self).__init__(message) @@ -57,42 +57,31 @@ class Error(StandardError): """ if not message: - message = re.sub('[ \n]+', ' ', self.__doc__ % kwargs) - message = message.strip() + message = self.message_format % kwargs return message class ValidationError(Error): - """Expecting to find %(attribute)s in %(target)s. - - The server could not comply with the request since it is either malformed - or otherwise incorrect. - - The client is assumed to be in error. - - """ + message_format = _("Expecting to find %(attribute)s in %(target)s." + " The server could not comply with the request" + " since it is either malformed or otherwise" + " incorrect. The client is assumed to be in error.") code = 400 title = 'Bad Request' class StringLengthExceeded(ValidationError): - """String length exceeded. - - The length of string "%(string)s" exceeded the limit of column - %(type)s(CHAR(%(length)d)). - - """ + message_format = _("String length exceeded.The length of" + " string '%(string)s' exceeded the limit" + " of column %(type)s(CHAR(%(length)d)).") class ValidationSizeError(Error): - """Request attribute %(attribute)s must be less than or equal to %(size)i. - - The server could not comply with the request because the attribute - size is invalid (too large). - - The client is assumed to be in error. - - """ + message_format = _("Request attribute %(attribute)s must be" + " less than or equal to %(size)i. The server" + " could not comply with the request because" + " the attribute size is invalid (too large)." + " The client is assumed to be in error.") code = 400 title = 'Bad Request' @@ -103,19 +92,19 @@ class SecurityError(Error): def _build_message(self, message, **kwargs): """Only returns detailed messages in debug mode.""" if CONF.debug: - return message or self.__doc__ % kwargs + return message or self.message_format % kwargs else: - return self.__doc__ % kwargs + return self.message_format % kwargs class Unauthorized(SecurityError): - """The request you have made requires authentication.""" + message_format = _("The request you have made requires authentication.") code = 401 title = 'Unauthorized' class AuthPluginException(Unauthorized): - """Authentication plugin error.""" + message_format = _("Authentication plugin error.") def __init__(self, *args, **kwargs): super(AuthPluginException, self).__init__(*args, **kwargs) @@ -123,7 +112,7 @@ class AuthPluginException(Unauthorized): class AuthMethodNotSupported(AuthPluginException): - """Attempted to authenticate with an unsupported method.""" + message_format = _("Attempted to authenticate with an unsupported method.") def __init__(self, *args, **kwargs): super(AuthMethodNotSupported, self).__init__(*args, **kwargs) @@ -131,7 +120,7 @@ class AuthMethodNotSupported(AuthPluginException): class AdditionalAuthRequired(AuthPluginException): - """Additional authentications steps required.""" + message_format = _("Additional authentications steps required.") def __init__(self, auth_response=None, **kwargs): super(AdditionalAuthRequired, self).__init__(message=None, **kwargs) @@ -139,112 +128,111 @@ class AdditionalAuthRequired(AuthPluginException): class Forbidden(SecurityError): - """You are not authorized to perform the requested action.""" + message_format = _("You are not authorized to perform the" + " requested action.") code = 403 title = 'Forbidden' class ForbiddenAction(Forbidden): - """You are not authorized to perform the requested action, %(action)s.""" + message_format = _("You are not authorized to perform the" + " requested action, %(action)s.") class NotFound(Error): - """Could not find, %(target)s.""" + message_format = _("Could not find, %(target)s.") code = 404 title = 'Not Found' class EndpointNotFound(NotFound): - """Could not find endpoint, %(endpoint_id)s.""" + message_format = _("Could not find endpoint, %(endpoint_id)s.") class MetadataNotFound(NotFound): - """An unhandled exception has occurred: Could not find metadata.""" - # (dolph): metadata is not a user-facing concept, - # so this exception should not be exposed + """(dolph): metadata is not a user-facing concept, + so this exception should not be exposed + """ + message_format = _("An unhandled exception has occurred:" + " Could not find metadata.") class PolicyNotFound(NotFound): - """Could not find policy, %(policy_id)s.""" + message_format = _("Could not find policy, %(policy_id)s.") class RoleNotFound(NotFound): - """Could not find role, %(role_id)s.""" + message_format = _("Could not find role, %(role_id)s.") class ServiceNotFound(NotFound): - """Could not find service, %(service_id)s.""" + message_format = _("Could not find service, %(service_id)s.") class DomainNotFound(NotFound): - """Could not find domain, %(domain_id)s.""" + message_format = _("Could not find domain, %(domain_id)s.") class ProjectNotFound(NotFound): - """Could not find project, %(project_id)s.""" + message_format = _("Could not find project, %(project_id)s.") class TokenNotFound(NotFound): - """Could not find token, %(token_id)s.""" + message_format = _("Could not find token, %(token_id)s.") class UserNotFound(NotFound): - """Could not find user, %(user_id)s.""" + message_format = _("Could not find user, %(user_id)s.") class GroupNotFound(NotFound): - """Could not find group, %(group_id)s.""" + message_format = _("Could not find group, %(group_id)s.") class TrustNotFound(NotFound): - """Could not find trust, %(trust_id)s.""" + message_format = _("Could not find trust, %(trust_id)s.") class CredentialNotFound(NotFound): - """Could not find credential, %(credential_id)s.""" + message_format = _("Could not find credential, %(credential_id)s.") class VersionNotFound(NotFound): - """Could not find version, %(version)s.""" + message_format = _("Could not find version, %(version)s.") class Conflict(Error): - """Conflict occurred attempting to store %(type)s. - - %(details)s - - """ + message_format = _("Conflict occurred attempting to store %(type)s." + " %(details)s") code = 409 title = 'Conflict' class RequestTooLarge(Error): - """Request is too large.""" + message_format = _("Request is too large.") code = 413 title = 'Request is too large.' class UnexpectedError(Error): - """An unexpected error prevented the server from fulfilling your request. - - %(exception)s - - """ + message_format = _("An unexpected error prevented the server" + " from fulfilling your request. %(exception)s") code = 500 title = 'Internal Server Error' class MalformedEndpoint(UnexpectedError): - """Malformed endpoint URL (%(endpoint)s), see ERROR log for details.""" + message_format = _("Malformed endpoint URL (%(endpoint)s)," + " see ERROR log for details.") class NotImplemented(Error): - """The action you have requested has not been implemented.""" + message_format = _("The action you have requested has not" + " been implemented.") code = 501 title = 'Not Implemented' class PasteConfigNotFound(UnexpectedError): - """The Keystone paste configuration file %(config_file)s could not be - found. - """ + message_format = _("The Keystone paste configuration file" + " %(config_file)s could not be found.") |