def error(self, request, status_code, error_dict=None): """ Return XML error response that includes a human readable error message, application-specific errors and a machine readable status code. """ from django.conf import settings if not error_dict: error_dict = ErrorDict() response = HttpResponse(mimetype = self.mimetype) response.status_code = status_code xml = SimplerXMLGenerator(response, settings.DEFAULT_CHARSET) xml.startDocument() xml.startElement("django-error", {}) xml.addQuickElement(name="error-message", contents='%d %s' % (status_code, STATUS_CODE_TEXT[status_code])) xml.addQuickElement(name="status-code", contents=str(status_code)) if error_dict: xml.startElement("model-errors", {}) for (model_field, errors) in error_dict.items(): for error in errors: xml.addQuickElement(name=model_field, contents=error) xml.endElement("model-errors") xml.endElement("django-error") xml.endDocument() return response
def error(self, request, status_code, error_dict=None): """ Return XML error response that includes a human readable error message, application-specific errors and a machine readable status code. """ from django.conf import settings if not error_dict: error_dict = ErrorDict() response = HttpResponse(mimetype=self.mimetype) response.status_code = status_code xml = SimplerXMLGenerator(response, settings.DEFAULT_CHARSET) xml.startDocument() xml.startElement("django-error", {}) xml.addQuickElement(name="error-message", contents='%d %s' % (status_code, STATUS_CODE_TEXT[status_code])) xml.addQuickElement(name="status-code", contents=str(status_code)) if error_dict: xml.startElement("model-errors", {}) for (model_field, errors) in error_dict.items(): for error in errors: xml.addQuickElement(name=model_field, contents=error) xml.endElement("model-errors") xml.endElement("django-error") xml.endDocument() return response
def error(self, request, status_code, error_dict=None): """ Handles errors in a RESTful way. - appropriate status code - appropriate mimetype - human-readable error message """ if not error_dict: error_dict = ErrorDict() response = HttpResponse(mimetype = self.mimetype) response.write('%d %s' % (status_code, STATUS_CODE_TEXT[status_code])) if error_dict: response.write('\n\nErrors:\n') response.write(error_dict.as_text()) response.status_code = status_code return response
def error(self, request, status_code, error_dict=None): """ Handles errors in a RESTful way. - appropriate status code - appropriate mimetype - human-readable error message """ if not error_dict: error_dict = ErrorDict() response = HttpResponse(mimetype=self.mimetype) response.write('%d %s' % (status_code, STATUS_CODE_TEXT[status_code])) if error_dict: response.write('\n\nErrors:\n') response.write(error_dict.as_text()) response.status_code = status_code return response
def error(self, request, status_code, error_dict=None): """ Renders error template (template name: error status code). """ if not error_dict: error_dict = ErrorDict() response = direct_to_template(request, template='%s/%s.html' % (self.template_dir, str(status_code)), extra_context={'errors': error_dict}, mimetype=self.mimetype) response.status_code = status_code return response
def error(self, request, status_code, error_dict=None): """ Return JSON error response that includes a human readable error message, application-specific errors and a machine readable status code. """ if not error_dict: error_dict = ErrorDict() response = HttpResponse(mimetype=self.mimetype) response.status_code = status_code response_dict = { "error-message": '%d %s' % (status_code, STATUS_CODE_TEXT[status_code]), "status-code": status_code, "model-errors": error_dict } simplejson.dump(response_dict, response) return response
def __init__(self, errors=None): if not errors: errors = ErrorDict() self.errors = errors