def __call__(self, req): """Generate a WSGI response based on the exception passed to ctor.""" # Replace the body with fault details. locale = req.best_match_language() code = self.wrapped_exc.status_int fault_name = self._fault_names.get(code, "computeFault") explanation = self.wrapped_exc.explanation fault_data = { fault_name: { 'code': code, 'message': i18n.translate(explanation, locale)}} if code == 413: retry = self.wrapped_exc.headers.get('Retry-After', None) if retry: fault_data[fault_name]['retryAfter'] = retry # 'code' is an attribute on the fault tag itself metadata = {'attributes': {fault_name: 'code'}} xml_serializer = XMLDictSerializer(metadata, XML_NS_V1) content_type = req.best_match_content_type() serializer = { 'application/xml': xml_serializer, 'application/json': JSONDictSerializer(), }[content_type] if content_type == 'application/xml': global XML_WARNING if not XML_WARNING: msg = _('XML support has been deprecated and will be removed ' 'in the N release.') versionutils.report_deprecated_feature(LOG, msg) XML_WARNING = True body = serializer.serialize(fault_data) if isinstance(body, six.text_type): body = body.encode('utf-8') self.wrapped_exc.body = body self.wrapped_exc.content_type = content_type _set_request_id_header(req, self.wrapped_exc.headers) return self.wrapped_exc
def translate(msg): locale = request.best_match_language() return i18n.translate(msg, locale)