def error_handler(self, e, request, meth, em_format): """ Override this method to add handling of errors customized for your needs """ if isinstance(e, FormValidationError): return self.form_validation_response(e, self.determine_emitter(request)) elif isinstance(e, TypeError): result = rc.BAD_REQUEST msg = "Method signature does not match.\n\n" try: hm = HandlerMethod(meth) sig = hm.signature except TypeError: msg += "Signature could not be determined" else: if sig: msg += "Signature should be: %s" % sig else: msg += "Resource does not expect any parameters." if self.display_errors: msg += "\n\nException was: %s" % str(e) result.content = format_error(msg) return result elif isinstance(e, Http404): return rc.NOT_FOUND elif isinstance(e, HttpStatusCode): return e.response else: """ On errors (like code errors), we'd like to be able to give crash reports to both admins and also the calling user. There's two setting parameters for this: Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback to the caller, so he can tell you what error they got. If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep) if self.display_errors: return HttpResponseServerError(format_error("\n".join(rep.format_exception()))) else: raise
def error_handler(self, e, request, meth, em_format): """ Override this method to add handling of errors customized for your needs """ if settings.DEBUG: raise if isinstance(e, FormValidationError): return self.form_validation_response(e) elif isinstance(e, TypeError): result = rc.BAD_REQUEST hm = HandlerMethod(meth) sig = hm.signature msg = 'Method signature does not match.\n\n' if sig: msg += 'Signature should be: %s' % sig else: msg += 'Resource does not expect any parameters.' if self.display_errors: msg += '\n\nException was: %s' % str(e) result.content = format_error(msg) return result elif isinstance(e, Http404): return rc.NOT_FOUND elif isinstance(e, HttpStatusCode): return e.response else: """ On errors (like code errors), we'd like to be able to give crash reports to both admins and also the calling user. There's two setting parameters for this: Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback to the caller, so he can tell you what error they got. If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep) if self.display_errors: return HttpResponseServerError( format_error('\n'.join(rep.format_exception()))) else: raise
def emit(self, record): exc_type, exc_value, traceback = record.exc_info request = record.request reporter = ExceptionReporter(request, exc_type, exc_value, traceback, is_email=True) exc = reporter.format_exception() message = '*%s* at `%s`\n\n```%s```' % ( exc[-1].strip(), request.path_info, ''.join(exc[:-1]), ) slack_debug(message)
def error_handler(self, e, request, meth, em_format): """ Override this method to add handling of errors customized for your needs """ if isinstance(e, FormValidationError): return self.form_validation_response(e) elif isinstance(e, Http404): return rc.NOT_FOUND elif isinstance(e, HttpStatusCode): return e.response else: """ On errors (like code errors), we'd like to be able to give crash reports to both admins and also the calling user. There's two setting parameters for this: Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback to the caller, so he can tell you what error they got. If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep) if self.display_errors: return HttpResponseServerError( format_error('\n'.join(rep.format_exception()))) else: raise
def error_handler(self, e, request, meth, em_format): """ Override this method to add handling of errors customized for your needs """ if isinstance(e, FormValidationError): return self.form_validation_response(e) elif isinstance(e, Http404): return rc.NOT_FOUND elif isinstance(e, HttpStatusCode): return e.response else: """ On errors (like code errors), we'd like to be able to give crash reports to both admins and also the calling user. There's two setting parameters for this: Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback to the caller, so he can tell you what error they got. If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep) if self.display_errors: return HttpResponseServerError( format_error('\n'.join(rep.format_exception()))) else: raise
Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback to the caller, so he can tell you what error they got. If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep) if self.display_errors: return HttpResponseServerError( format_error('\n'.join(rep.format_exception()))) else: raise content_range = None if isinstance(result, QuerySet): """ Limit results based on requested items. This is a based on HTTP 1.1 Partial GET, RFC 2616 sec 14.35, but is intended to operate on the record level rather than the byte level. We will still respond with code 206 and a range header. """ request_range = None if 'HTTP_RANGE' in request.META:
error email to people in `settings.ADMINS`. - `FULCRUM_DISPLAY_ERRORS`: Will return a simple traceback to the caller, so he can tell you what error they got. If `FULCRUM_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep) if self.display_errors: return HttpResponseServerError( format_error('\n'.join(rep.format_exception()))) else: raise # Return serialized data emitter, ct = Emitter.get(em_format) srl = emitter(result, recurse_level, typemapper, handler, handler.fields, anonymous) try: """ Decide whether or not we want a generator here, or we just want to buffer up the entire result before sending it to the client. Won't matter for smaller datasets, but larger will have an impact. """ if self.stream: stream = srl.stream_render(request)
def error_handler(self, response, e, request, meth): """ Override this method to add handling of errors customized for your needs """ response.status_code = 500 if isinstance(e, PermissionDenied): response.error_message = e.message or "Permission Denied" response.status_code = 403 elif isinstance( e, (PistonException, PistonBadRequestException, PistonForbiddenException, PistonMethodException, PistonNotFoundException, PistonUnauthorizedException)): response.status_code = e.status_code response.error_message = e.message response.headers.update(e.headers) elif isinstance(e, FormValidationError): response.status_code = 400 response.form_errors = e.form.errors elif isinstance(e, TypeError) and meth: hm = HandlerMethod(meth) sig = hm.signature msg = 'Method signature does not match.\n\n' if sig: msg += 'Signature should be: %s' % sig else: msg += 'Resource does not expect any parameters.' if self.display_errors: msg += '\n\nException was: %s' % str(e) response.error_message = format_error(msg) # TODO: As we start using Piston exceptions, the following 2 errors can be phased out elif isinstance(e, Http404): response.status_code = 404 response.error_message = 'Not Found' elif isinstance(e, ValidationError): response.status_code = 400 if hasattr(e, "message_dict"): response.error_message = "\n".join( "%s: %s" % (k[0], k[1][0]) for k in e.message_dict.iteritems()) else: response.error_message = " ".join(e.messages) elif isinstance(e, ObjectDoesNotExist): response.status_code = 404 if hasattr(e, "message"): response.error_message = e.message else: response.error_message = 'Not Found' elif isinstance(e, HttpStatusCode): response.error_message = e.response else: """ On errors (like code errors), we'd like to be able to give crash reports to both admins and also the calling user. There's two setting parameters for this: Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple message/full traceback depending on `PISTON_DISPLAY_TRACEBACK`. Default is simple message to the caller, so he can tell you what error they got. If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep, e, request) if self.display_errors: if self.display_traceback: response.error_message = format_error('\n'.join( rep.format_exception())) else: response.error_message = str(e) else: raise
def exception_to_http_response(self, e, request): """ Any exceptions that are raised within the API handler, are taken care of here. @type e: Exception @param e: Exception object @type request: HTTPRequest @param request: Incoming request @rtype: tuple @return: Tuple of (HttpResponseObject, message) The HttpResponseObject, is simply an HttpRespone object of the appropriate form, depending on the error that occured. The message is any kind of message that we will be included in the response body. """ def format_error(error): return u'django-icetea crash report:\n\n%s' % error http_response, message = None, '' def validation_error_message(e): if hasattr(e, 'message_dict'): if '__all__' in e.message_dict: errors = e.message_dict['__all__'] else: errors = e.message_dict elif hasattr(e, 'messages'): errors = e.messages message = dict( type='Validation Error', errors=errors, ) if hasattr(e, 'params') and e.params: message.update(**e.params) return message def unprocessable_entity_message(e): message = dict( type='Unprocessable Entity Error', errors=e.errors, ) if hasattr(e, 'params') and e.params: message.update(**e.params) return message if isinstance(e, ValidationError): message = validation_error_message(e) http_response = HttpResponseBadRequest() elif isinstance(e, UnprocessableEntity): message = unprocessable_entity_message(e) http_response = HttpResponse(status=422) elif isinstance(e, (NotImplementedError, ObjectDoesNotExist, ValueError)): http_response = HttpResponseGone() elif isinstance(e, MethodNotAllowed): http_response = HttpResponseNotAllowed(e.permitted_methods) elif isinstance(e, PermissionDenied): http_response = HttpResponseForbidden() elif isinstance(e, ValidationErrorList): http_response = HttpResponseBadRequest() # TODO: Differentiate between validation error and unprocessable # entity errors message = [validation_error_message(error) for error in e.error_list] elif isinstance(e, UnprocessableEntityList): http_response = HttpResponse(status=422) message = [unprocessable_entity_message(error) for error in e.error_list] else: # Consider it a Server Error. # Send email, respond with a 500 Error code, display error. exc_type, exc_value, traceback = sys.exc_info() reporter = ExceptionReporter( request, exc_type, exc_value, traceback.tb_next ) if self.email_errors: self.email_exception(reporter) if self.display_errors and settings.DEBUG: message = format_error('\n'.join(reporter.format_exception())) http_response = HttpResponseServerError() return http_response, message
def error_handler(self, response, e, request, meth): """ Override this method to add handling of errors customized for your needs """ response.status_code = 500 if isinstance(e, (PistonException, PistonBadRequestException, PistonForbiddenException, PistonMethodException, PistonNotFoundException, PistonUnauthorizedException)): response.status_code = e.status_code response.error_message = e.message response.headers.update(e.headers) elif isinstance(e, FormValidationError): response.status_code = 400 response.form_errors = e.form.errors elif isinstance(e, TypeError) and meth: hm = HandlerMethod(meth) sig = hm.signature msg = 'Method signature does not match.\n\n' if sig: msg += 'Signature should be: %s' % sig else: msg += 'Resource does not expect any parameters.' if self.display_errors: msg += '\n\nException was: %s' % str(e) response.error_message = format_error(msg) # TODO: As we start using Piston exceptions, the following 2 errors can be phased out elif isinstance(e, Http404): response.status_code = 404 response.error_message = 'Not Found' elif isinstance(e, HttpStatusCode): response.error_message = e.response else: """ On errors (like code errors), we'd like to be able to give crash reports to both admins and also the calling user. There's two setting parameters for this: Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple message/full traceback depending on `PISTON_DISPLAY_TRACEBACK`. Default is simple message to the caller, so he can tell you what error they got. If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep) if self.display_errors: if self.display_traceback: response.error_message = format_error('\n'.join(rep.format_exception())) else: response.error_message = str(e) else: raise
Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback to the caller, so he can tell you what error they got. If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ exc_type, exc_value, tb = sys.exc_info() rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next) if self.email_errors: self.email_exception(rep) if self.display_errors: return HttpResponseServerError(format_error("\n".join(rep.format_exception()))) else: raise emitter, ct = Emitter.get(em_format) srl = emitter(result, typemapper, handler, handler.fields, anonymous) try: """ Decide whether or not we want a generator here, or we just want to buffer up the entire result before sending it to the client. Won't matter for smaller datasets, but larger will have an impact. """ if self.stream: stream = srl.stream_render(request)