2013-10-23 45 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

我也不知道,說實話,但它只是修復它。我想,在錯誤處理方面有一些事情要做。也許... –