我有以下代碼:如何繼承RequestHandler以自動驗證參數?
class CounterIDHandler(RequestHandler):
@gen.coroutine
def get(self, counter_id):
try:
object_id = bson.objectid.ObjectId(counter_id)
except bson.errors.InvalidId as e:
self.finish(json_encode({'e': str(e)}))
return
# I want to finish execution here
class CounterHandler(CounterIDHandler):
@gen.coroutine
def get(self, counter_id):
super().get(counter_id)
print("this should not print if we get exception in super().get")
try:
# I want to use object_id here
except Exception as e:
self.finish(json_encode({'e': str(e)}))
這顯然是行不通的,但它顯示了什麼,我試圖做的。 self.finish()
終止與客戶端的連接,但不會終止執行。
我想驗證counter_id是一個有效的object_id而無需在所有處理程序中複製粘貼代碼。
真是太爽瞭解決方案,我認爲這是很一般的,你可以用它每次你需要驗證,但在這種情況下,我想本的更好的解決方案。 – ragezor