question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Key raises ProtocolBufferDecodeError for some invalid serialized keys

See original GitHub issue
What steps will reproduce the problem?
from google.appengine.ext.ndb import model
model.Key(urlsafe='foo')

What is the expected output?
ValueError?

What do you see instead?
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/key.py", line 209, in __new__
    self.__reference = _ConstructReference(cls, **kwargs)
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 93, in positional_wrapper
    return wrapped(*args, **kwds)
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/key.py", line 599, in _ConstructReference
    reference = _ReferenceFromSerialized(serialized)
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/key.py", line 731, in _ReferenceFromSerialized
    return entity_pb.Reference(serialized)
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/_internal/proto1/internal/cpp_message.py", line 555, in Init
    self.MergeFromString(contents)
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/_internal/proto1/internal/cpp_message.py", line 633, in MergeFromString
    raise ProtocolBufferDecodeError('Unable to merge from string.')
ProtocolBufferDecodeError: Unable to merge from string.

  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/ndb/key.py", line 209, in __new__
    self.__reference = _ConstructReference(cls, **kwargs)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 93, in positional_wrapper
    return wrapped(*args, **kwds)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/ndb/key.py", line 599, in _ConstructReference
    reference = _ReferenceFromSerialized(serialized)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/ndb/key.py", line 731, in _ReferenceFromSerialized
    return entity_pb.Reference(serialized)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/datastore/entity_pb.py", line 1774, in __init__
    if contents is not None: self.MergeFromString(contents)
  File "/base/python_runtime/python_lib/versions/1/google/net/proto/ProtocolBuffer.py", line 85, in MergeFromString
    self.MergePartialFromString(s)
  File "/base/python_runtime/python_lib/versions/1/google/net/proto/ProtocolBuffer.py", line 92, in MergePartialFromString
    self._CMergeFromString(s)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/datastore/entity_pb.py", line 1819, in _CMergeFromString
    _net_proto___parse__python.MergeFromString(self, 'storage_onestore_v3.Reference', s)
ProtocolBufferDecodeError: Corrupted message: invalid wire type 6 encountered 
for tag 15

What version of the product are you using?
NDB 0.9.3
App Engine production 1.6.1

Original issue reported on code.google.com by sa...@google.com on 22 Jan 2012 at 11:11

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:6
  • Comments:10

github_iconTop GitHub Comments

1reaction
GoogleCodeExportercommented, Jun 10, 2015
At least to catch it, you can

    try:
        some_key = ndb.Key(urlsafe=123)
    except Exception, e:
        if e.__class__.__name__ == 'ProtocolBufferDecodeError':
            return ...
        else:
            raise

Original comment by herr.ka...@gmail.com on 24 Sep 2013 at 10:36

0reactions
salvador-byndcommented, Jul 5, 2019

Run into this issue while refactoring code.

This bit of code catches this type of exception:

from google.net.proto import ProtocolBuffer
except (TypeError, ProtocolBuffer.ProtocolBufferDecodeError):

Example:

from google.appengine.ext import ndb
from google.net.proto import ProtocolBuffer

def get_entity_by_ident(ident):
    try:
        key = ndb.Key(urlsafe=ident)
    except (TypeError, ProtocolBuffer.ProtocolBufferDecodeError):
        raise endpoints.BadRequestException('Malformed key')

    if not valid_key_namespace(key):
        raise endpoints.BadRequestException('Invalid key namespace')

    # IMPORTANT: Fetch from datastore, to avoid fetching stale entities.
    return key.get(use_cache=False, use_memcache=False)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Key raises ProtocolBufferDecodeError for some invalid ...
Unit testing works fine catching from google.net.proto.ProtocolBuffer import ProtocolBufferDecodeError, but in production does not. File "/base/ ...
Read more >
ProtocolBufferDecodeError: truncated when using ndb.Key
If this is the case, then the workaround would be to just use ID of the entity (or entities in case of parent...
Read more >
google.appengine.datastore.datastore_query - Google Cloud
BadPropertyError: if the property name is invalid. datastore_errors. ... property_names): """Extracts key values from the given entity.
Read more >
google/appengine/ext/ndb/context.py - Google Git
raise ValueError('Cannot specify %s and %s at the same time' %. (key, translation)) ... Key gets the singleton Context instance and invokes Context.get....
Read more >
google.cloud.ndb.key — ndb documentation
Key ` for Google Cloud Datastore. .. testsetup:: * from google.cloud import ndb ... Raises: TypeError: If none of ``reference``, ``serialized``, ``urlsafe``, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found