def ToJsonError(e, exc_info): """Converts an exception to an API error response.""" # Wrap some common exception types into kbapi types if isinstance(e, Http404): e = kbapi.NotFoundError(e.message) elif isinstance(e, ValueError): e = kbapi.BadRequestError(str(e)) elif isinstance(e, backend.NoTokenError): e = kbapi.NotFoundError(e.message) # Now determine the response based on the exception type. if isinstance(e, kbapi.Error): code = e.__class__.__name__ http_code = e.HTTP_CODE message = e.Message() else: code = 'ServerError' http_code = 500 message = 'An internal error occurred: %s' % str(e) if settings.DEBUG: message += "\n" + "\n".join(traceback.format_exception(*exc_info)) if settings.DEBUG and settings.HAVE_RAVEN: from raven.contrib.django.models import client client.captureException() result = {'error': {'code': code, 'message': message}} return result, http_code
def GetAuthToken(self, auth_device, token_value): try: return self._client.GetToken(auth_device, token_value) except kbapi.NotFoundError: raise except socket.error: self._logger.warning('Socket error fetching token; ignoring.') raise kbapi.NotFoundError()
def to_json_error(e, exc_info): """Converts an exception to an API error response.""" # Wrap some common exception types into kbapi types if isinstance(e, Http404): e = kbapi.NotFoundError(str(e)) elif isinstance(e, ValueError): e = kbapi.BadRequestError(str(e)) elif isinstance(e, NoTokenError): e = kbapi.NotFoundError(str(e)) # Now determine the response based on the exception type. if isinstance(e, kbapi.Error): code = e.__class__.__name__ http_code = e.HTTP_CODE message = e.Message() else: code = "ServerError" http_code = 500 message = "An internal error occurred: %s" % str(e) result = {"error": {"code": code, "message": message}} if settings.DEBUG: result["error"]["traceback"] = "".join(traceback.format_exception(*exc_info)) return result, http_code