2013-10-23 46 views
0

,当我有这样的代码,它用来升级GAE的Python NDB前工作:ProtocolBufferDecodeError:截断使用ndb.Key

class MyHandler(webapp2.RequestHandler): 
    def get(self,urlString): 
    resume = ndb.Key(urlsafe=urlString).get() 

现在,我有这样的错误:

Traceback (most recent call last): 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "C:\xampp\htdocs\mapjobs\main.py", line 127, in get 
    resume_key = ndb.Key(urlsafe=urlString) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\key.py", line 212, in __new__ 
    self.__reference = _ConstructReference(cls, **kwargs) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\utils.py", line 136, in positional_wrapper 
    return wrapped(*args, **kwds) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\key.py", line 642, in _ConstructReference 
    reference = _ReferenceFromSerialized(serialized) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\key.py", line 774, in _ReferenceFromSerialized 
    return entity_pb.Reference(serialized) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\entity_pb.py", line 1791, in __init__ 
    if contents is not None: self.MergeFromString(contents) 
    File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 84, in MergeFromString 
    self.MergePartialFromString(s) 
    File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 98, in MergePartialFromString 
    self.TryMerge(d) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\entity_pb.py", line 1920, in TryMerge 
    d.skipData(tt) 
    File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 524, in skipData 
    self.skip(4) 
    File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 499, in skip 
    if self.idx + n > self.limit: raise ProtocolBufferDecodeError, "truncated" 
ProtocolBufferDecodeError: truncated 

哪些亮点可能是:

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
File "C:\xampp\htdocs\mapjobs\main.py", line 127, in get 
    resume_key = ndb.Key(urlsafe=urlString) 

怎么了?

回答

3

我的猜测是,您正在通过Url参数接收密钥作为url安全字符串,该参数由于浏览器中的最大URL长度限制而被截断。浏览器在实践中限制Url长度达到最多2000个字符。看到这个问题:What is the maximum length of a URL in different browsers?

如果你的密钥包含(多个)父键,那么有可能超过2000个字符。是这样吗?请在创建和收到时检查urlsafe编码密钥的长度。

如果是这种情况,那么解决方法是仅使用实体的ID(或者在父键的情况下使用实体)并手动构建密钥。

+0

我想我现在明白了。我misarranged webapp2接收器。我指的是'/resume/(.*)'。我会发布我的答案,只是一秒钟。不过,这也可能是一种可能性。谢谢。 –

0

我不匹配的URL,其中前:

app = webapp2.WSGIApplication([ 
    ('/', MainHandler), 
    ('/jobs',JobsHandler), 
    ('/job/(.*)',JobHandler), 
    ('/job/(.*)/update', JobUpdateHandler), 
    ('/job/(.*)/delete', JobDeleteHandler), 
    ('/job/create', JobCreateHandler), 
    ('/resume/(.*)',ResumeHandler), 
    ('/resume/(.*)/update', ResumeUpdateHandler), 
    ('/resume/(.*)/delete', ResumeDeleteHandler), 
    ('/resume/create', ResumeCreateHandler), 
    ('/resumes',ResumesHandler), 
    ('/profile',ProfileHandler), 
    ('/profile/(.*)/update', ProfileUpdateHandler), 
], debug=True) 

请注意,我misarranged '/resume/(.*)',因为这应该是在底部接收urlString最后。这是它现在是什么:

app = webapp2.WSGIApplication([ 
    ('/', MainHandler), 
    ('/jobs',JobsHandler), 
    ('/job/(.*)/update', JobUpdateHandler), 
    ('/job/(.*)/delete', JobDeleteHandler), 
    ('/job/create', JobCreateHandler), 
    ('/job/(.*)',JobHandler), 
    ('/resume/(.*)/update', ResumeUpdateHandler), 
    ('/resume/(.*)/delete', ResumeDeleteHandler), 
    ('/resume/create', ResumeCreateHandler), 
    ('/resume/(.*)',ResumeHandler), 
    ('/resumes',ResumesHandler), 
    ('/profile',ProfileHandler), 
    ('/profile/(.*)/update', ProfileUpdateHandler), 
], debug=True) 
+0

我之前就已经解决了这个问题,但是有些事情因为过度思考而被我误解了。所以,我认为这是对自己的一个提示,这很好。我希望错误消息可以更明显。 –

+0

我可以看到如何URL顺序会阻止你到达创建/更新/删除处理程序......我不明白这可能已经修复了你的ProtocolBufferDecodeError? – Anentropic

+0

我也不知道,说实话,但它只是修复它。我想,在错误处理方面有一些事情要做。也许... –