Python google.appengine.api.datastore_errors.Error() Examples

The following are 16 code examples of google.appengine.api.datastore_errors.Error(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.api.datastore_errors , or try the search function .
Example #1
Source File: datastore.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def _MakeSyncCall(service, call, request, response, config=None):
  """The APIProxy entry point for a synchronous API call.

  Args:
    service: For backwards compatibility, must be 'datastore_v3'.
    call: String representing which function to call.
    request: Protocol buffer for the request.
    response: Protocol buffer for the response.
    config: Optional Configuration to use for this request.

  Returns:
    Response protocol buffer. Caller should always use returned value
    which may or may not be same as passed in 'response'.

  Raises:
    apiproxy_errors.Error or a subclass.
  """
  conn = _GetConnection()
  if isinstance(request, datastore_pb.Query):
    conn._set_request_read_policy(request, config)
    conn._set_request_transaction(request)
  rpc = conn._make_rpc_call(config, call, request, response)
  conn.check_rpc_success(rpc)
  return response 
Example #2
Source File: datastore.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def Get(keys, **kwargs):
  """Retrieves one or more entities from the datastore.

  Retrieves the entity or entities with the given key(s) from the datastore
  and returns them as fully populated Entity objects, as defined below. If
  there is an error, raises a subclass of datastore_errors.Error.

  If keys is a single key or string, an Entity will be returned, or
  EntityNotFoundError will be raised if no existing entity matches the key.

  However, if keys is a list or tuple, a list of entities will be returned
  that corresponds to the sequence of keys. It will include entities for keys
  that were found and None placeholders for keys that were not found.

  Args:
    keys: Key or string or list of Keys or strings
    config: Optional Configuration to use for this request, must be specified
      as a keyword argument.

  Returns:
    Entity or list of Entity objects
  """
  return GetAsync(keys, **kwargs).get_result() 
Example #3
Source File: datastore.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def Delete(keys, **kwargs):
  """Deletes one or more entities from the datastore. Use with care!

  Deletes the given entity(ies) from the datastore. You can only delete
  entities from your app. If there is an error, raises a subclass of
  datastore_errors.Error.

  Args:
    # the primary key(s) of the entity(ies) to delete
    keys: Key or string or list of Keys or strings
    config: Optional Configuration to use for this request, must be specified
      as a keyword argument.

  Raises:
    TransactionFailedError, if the Delete could not be committed.
  """
  return DeleteAsync(keys, **kwargs).get_result() 
Example #4
Source File: datastore_rpc.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def _DatastoreExceptionFromErrorCodeAndDetail(error, detail):
  """Converts a datastore_pb.Error into a datastore_errors.Error.

  Args:
    error: A member of the datastore_pb.Error enumeration.
    detail: A string providing extra details about the error.

  Returns:
    An instance of a subclass of datastore_errors.Error.
  """
  exception_class = _DATASTORE_EXCEPTION_CLASSES.get(error,
                                                     datastore_errors.Error)

  if detail is None:
    return exception_class()
  else:
    return exception_class(detail) 
Example #5
Source File: datastore_rpc.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def _DatastoreExceptionFromCanonicalErrorCodeAndDetail(error, detail):
  """Converts a canonical error code into a datastore_errors.Error.

  Args:
    error: A canonical error code from google.rpc.code.
    detail: A string providing extra details about the error.

  Returns:
    An instance of a subclass of datastore_errors.Error.
  """
  exception_class = _CLOUD_DATASTORE_EXCEPTION_CLASSES.get(
      error, datastore_errors.InternalError)

  if detail is None:
    return exception_class()
  else:
    return exception_class(detail) 
Example #6
Source File: datastore_locks.py    From upvote with Apache License 2.0 6 votes vote down vote up
def _AcquireAsync(self, timeout):
    """Acquires the lock via datastore or returns False."""

    @ndb.transactional_tasklet(retries=0)
    def _TransactionalAcquireAsync():
      lock_entity = yield _DatastoreLockEntity.get_or_insert_async(self._id)
      if lock_entity.lock_held:
        raise ndb.Return(False)

      lock_entity.lock_id = self._lock_id
      lock_entity.acquired = True
      lock_entity.timeout = timeout
      yield lock_entity.put_async()
      raise ndb.Return(True)

    try:
      raise ndb.Return((yield _TransactionalAcquireAsync()))
    except datastore_errors.Error:
      raise ndb.Return(False) 
Example #7
Source File: config.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def execute_jobs(jobs, txn_sleep_time):
  """Executes all jobs one by one, sleeping between them.

  This gives the datastore some time to "land" transactions. It's not a
  guarantee, but just a best effort attempt to avoid contention. If it
  happens, refetch_config() will resume unfinished work on the next iteration.

  There should be no casual dependencies between jobs. Even through they will
  be executed sequentially, some may fail, and this does NOT stop processing
  of subsequent jobs.

  Args:
    txn_sleep_time: how long to sleep between jobs.

  Returns:
    True if all jobs succeeded, False if at least one failed.
  """
  success = True
  for idx, job in enumerate(jobs):
    if idx:
      time.sleep(txn_sleep_time)
    try:
      job()
    except (
          apiproxy_errors.Error,
          datastore_errors.Error,
          replication.ReplicationTriggerError) as exc:
      logging.error(
          'Failed, will try again later: %s - %s',
          exc.__class__.__name__, exc)
      success = False
  return success 
Example #8
Source File: datastore.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def AllocateIds(model_key, size=None, **kwargs):
  """Allocates a range of IDs of size or with max for the given key.

  Allocates a range of IDs in the datastore such that those IDs will not
  be automatically assigned to new entities. You can only allocate IDs
  for model keys from your app. If there is an error, raises a subclass of
  datastore_errors.Error.

  Either size or max must be provided but not both. If size is provided then a
  range of the given size is returned. If max is provided then the largest
  range of ids that are safe to use with an upper bound of max is returned (can
  be an empty range).

  Max should only be provided if you have an existing numeric id range that you
  want to reserve, e.g. bulk loading entities that already have IDs. If you
  don't care about which IDs you receive, use size instead.

  Args:
    model_key: Key or string to serve as a model specifying the ID sequence
               in which to allocate IDs
    size: integer, number of IDs to allocate.
    max: integer, upper bound of the range of IDs to allocate.
    config: Optional Configuration to use for this request.

  Returns:
    (start, end) of the allocated range, inclusive.
  """
  return AllocateIdsAsync(model_key, size, **kwargs).get_result() 
Example #9
Source File: images_stub.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def _OpenBlob(self, blob_key):
    """Create an Image from the blob data read from blob_key."""

    try:
      _ = datastore.Get(
          blobstore_stub.BlobstoreServiceStub.ToDatastoreBlobKey(blob_key))
    except datastore_errors.Error:


      logging.exception('Blob with key %r does not exist', blob_key)
      raise apiproxy_errors.ApplicationError(
          images_service_pb.ImagesServiceError.UNSPECIFIED_ERROR)

    blobstore_storage = apiproxy_stub_map.apiproxy.GetStub('blobstore')


    try:
      blob_file = blobstore_storage.storage.OpenBlob(blob_key)
    except IOError:
      logging.exception('Could not get file for blob_key %r', blob_key)

      raise apiproxy_errors.ApplicationError(
          images_service_pb.ImagesServiceError.BAD_IMAGE_DATA)

    try:
      return Image.open(blob_file)
    except IOError:
      logging.exception('Could not open image %r for blob_key %r',
                        blob_file, blob_key)

      raise apiproxy_errors.ApplicationError(
          images_service_pb.ImagesServiceError.BAD_IMAGE_DATA) 
Example #10
Source File: datastore_rpc.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def __commit_hook(self, rpc):
    """Internal method used as get_result_hook for Commit."""
    try:
      rpc.check_success()
      self._state = TransactionalConnection.CLOSED
      self.__transaction = None
    except apiproxy_errors.ApplicationError, err:
      self._state = TransactionalConnection.FAILED
      if err.application_error == datastore_pb.Error.CONCURRENT_TRANSACTION:
        return False
      else:
        raise _ToDatastoreError(err) 
Example #11
Source File: datastore_rpc.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def _ToDatastoreError(err):
  """Converts an apiproxy.ApplicationError to an error in datastore_errors.

  Args:
    err: An apiproxy.ApplicationError object.

  Returns:
    An instance of a subclass of datastore_errors.Error.
  """
  return _DatastoreExceptionFromErrorCodeAndDetail(err.application_error,
                                                   err.error_detail) 
Example #12
Source File: main.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def ListActions(self, error=None):
    """Handler for get requests to datastore_admin/confirm_delete."""
    use_stats_kinds = False
    kinds = []
    more_kinds = False
    try:
      kinds, more_kinds = utils.GetKinds()
      if not kinds:
        use_stats_kinds = True
        logging.warning('Found no kinds. Using datastore stats instead.')
    except datastore_errors.Error, e:
      logging.exception(e)
      use_stats_kinds = True 
Example #13
Source File: __init__.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def process_next_chunk(self, up_to):
    """Processes as much minutes starting at a specific time.

    This class should be called from a non-synchronized cron job, so it will
    rarely have more than one instance running at a time. Every entity is self
    contained so it explicitly handles datastore inconsistency.

    Arguments:
    - up_to: number of minutes to buffer between 'now' and the last minute to
             process. Will usually be in the range of 1 to 10.

    Returns:
      Number of self.stats_minute_cls generated, e.g. the number of minutes
      processed successfully by self_generate_snapshot. Returns None in case of
      failure.
    """
    count = 0
    original_minute = None
    try:
      now = utils.utcnow()
      original_minute = self._get_next_minute_to_process(now)
      next_minute = original_minute
      while now - next_minute >= datetime.timedelta(minutes=up_to):
        self._process_one_minute(next_minute)
        count += 1
        self._set_last_processed_time(next_minute)
        if self._max_minutes_per_process == count:
          break
        next_minute = next_minute + datetime.timedelta(minutes=1)
        now = utils.utcnow()
      return count
    except (
        datastore_errors.Error,
        logservice.Error,
        DeadlineExceededError) as e:
      msg = (
          'Got an error while processing stats.\n'
          'Processing started at %s; tried to get up to %smins from now; '
          'Processed %dmins\n%s') % (
          original_minute, up_to, count, e)
      if not count:
        logging.error(msg)
        # This is bad, it means that for the lifespan of the cron handler
        # (currently 10 minutes), it was unable to even process a single minute
        # worth of statistics.
        return None
      logging.warning(msg)
      # At least something was processed, so it's fine.
      return count 
Example #14
Source File: __init__.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def process_next_chunk(self, up_to):
    """Processes as much minutes starting at a specific time.

    This class should be called from a non-synchronized cron job, so it will
    rarely have more than one instance running at a time. Every entity is self
    contained so it explicitly handles datastore inconsistency.

    Arguments:
    - up_to: number of minutes to buffer between 'now' and the last minute to
             process. Will usually be in the range of 1 to 10.

    Returns:
      Number of self.stats_minute_cls generated, e.g. the number of minutes
      processed successfully by self_generate_snapshot. Returns None in case of
      failure.
    """
    count = 0
    original_minute = None
    try:
      now = utils.utcnow()
      original_minute = self._get_next_minute_to_process(now)
      next_minute = original_minute
      while now - next_minute >= datetime.timedelta(minutes=up_to):
        self._process_one_minute(next_minute)
        count += 1
        self._set_last_processed_time(next_minute)
        if self._max_minutes_per_process == count:
          break
        next_minute = next_minute + datetime.timedelta(minutes=1)
        now = utils.utcnow()
      return count
    except (
        datastore_errors.Error,
        logservice.Error,
        DeadlineExceededError) as e:
      msg = (
          'Got an error while processing stats.\n'
          'Processing started at %s; tried to get up to %smins from now; '
          'Processed %dmins\n%s') % (
          original_minute, up_to, count, e)
      if not count:
        logging.error(msg)
        # This is bad, it means that for the lifespan of the cron handler
        # (currently 10 minutes), it was unable to even process a single minute
        # worth of statistics.
        return None
      logging.warning(msg)
      # At least something was processed, so it's fine.
      return count 
Example #15
Source File: __init__.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def process_next_chunk(self, up_to):
    """Processes as much minutes starting at a specific time.

    This class should be called from a non-synchronized cron job, so it will
    rarely have more than one instance running at a time. Every entity is self
    contained so it explicitly handles datastore inconsistency.

    Arguments:
    - up_to: number of minutes to buffer between 'now' and the last minute to
             process. Will usually be in the range of 1 to 10.

    Returns:
      Number of self.stats_minute_cls generated, e.g. the number of minutes
      processed successfully by self_generate_snapshot. Returns None in case of
      failure.
    """
    count = 0
    original_minute = None
    try:
      now = utils.utcnow()
      original_minute = self._get_next_minute_to_process(now)
      next_minute = original_minute
      while now - next_minute >= datetime.timedelta(minutes=up_to):
        self._process_one_minute(next_minute)
        count += 1
        self._set_last_processed_time(next_minute)
        if self._max_minutes_per_process == count:
          break
        next_minute = next_minute + datetime.timedelta(minutes=1)
        now = utils.utcnow()
      return count
    except (
        datastore_errors.Error,
        logservice.Error,
        DeadlineExceededError) as e:
      msg = (
          'Got an error while processing stats.\n'
          'Processing started at %s; tried to get up to %smins from now; '
          'Processed %dmins\n%s') % (
          original_minute, up_to, count, e)
      if not count:
        logging.error(msg)
        # This is bad, it means that for the lifespan of the cron handler
        # (currently 10 minutes), it was unable to even process a single minute
        # worth of statistics.
        return None
      logging.warning(msg)
      # At least something was processed, so it's fine.
      return count 
Example #16
Source File: __init__.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def process_next_chunk(self, up_to):
    """Processes as much minutes starting at a specific time.

    This class should be called from a non-synchronized cron job, so it will
    rarely have more than one instance running at a time. Every entity is self
    contained so it explicitly handles datastore inconsistency.

    Arguments:
    - up_to: number of minutes to buffer between 'now' and the last minute to
             process. Will usually be in the range of 1 to 10.

    Returns:
      Number of self.stats_minute_cls generated, e.g. the number of minutes
      processed successfully by self_generate_snapshot. Returns None in case of
      failure.
    """
    count = 0
    original_minute = None
    try:
      now = utils.utcnow()
      original_minute = self._get_next_minute_to_process(now)
      next_minute = original_minute
      while now - next_minute >= datetime.timedelta(minutes=up_to):
        self._process_one_minute(next_minute)
        count += 1
        self._set_last_processed_time(next_minute)
        if self._max_minutes_per_process == count:
          break
        next_minute = next_minute + datetime.timedelta(minutes=1)
        now = utils.utcnow()
      return count
    except (
        datastore_errors.Error,
        logservice.Error,
        DeadlineExceededError) as e:
      msg = (
          'Got an error while processing stats.\n'
          'Processing started at %s; tried to get up to %smins from now; '
          'Processed %dmins\n%s') % (
          original_minute, up_to, count, e)
      if not count:
        logging.error(msg)
        # This is bad, it means that for the lifespan of the cron handler
        # (currently 10 minutes), it was unable to even process a single minute
        # worth of statistics.
        return None
      logging.warning(msg)
      # At least something was processed, so it's fine.
      return count