Pythonを使用してGoogle App Engineでアプリケーションを開発しています。Python/GAE Webリクエストのエラー処理
さまざまな出力(現時点ではhtmlとjson)を返すハンドラがありますが、リクエストハンドラに送信された無効なパラメータに基づいてシステムの明白なエラーをテストしています。
しかし、私がやっているものを(下記参照)汚い感じている:
class FeedHandler(webapp.RequestHandler):
def get(self):
app = self.request.get("id")
name = self.request.get("name")
output_type = self.request.get("output", default_value = "html")
pretty = self.request.get("pretty", default_value = "")
application = model.Application.GetByKey(app)
if application is None:
if output_type == "json":
self.response.out.write(simplejson.dumps({ "errorCode" : "Application not found."}))
self.set_status(404)
return
category = model.FeedCategory.GetByKey(application, name)
if category is None:
if output_type == "json":
self.response.out.write(simplejson.dumps({ "errorCode" : "Category not found."}))
self.set_status(404)
return
私は特に、出力タイプごとに例を処理してもおよび、「アサート」あたりにしています。
私は、ここで提案、パターン、例を明確にすることに熱心です(私がやっていることを試して維持するのは悪夢になるだろうと知っています)。
私は、カスタム例外を持ち上げていて、エラーメッセージを表示する方法を自動的に実現するデコレータを持っていると考えています。良いアイデアだと思っていますが、フィードバックや提案過去に人々がこれをどのようにしたかに基づいています。補助方法に
if application is None:
if output_type == "json":
self.response.out.write(simplejson.dumps({ "errorCode" : "Application not found."}))
self.set_status(404)
return
:
それを愛してください - handle_exceptionが存在していたということは考えていませんでした...(私がページを早く見ても:)これは助けになると私は確信しています - 私はhandle_exceptionのself.response.out.writeを行うことができます出力json? – Kinlan
handle_exceptionは定期的なリクエスト/レスポンスサイクルの一部なので、そこで何かできることができます。 –
甘い - それは私のコードの終わりをクリアします:) – Kinlan