【发布时间】:2009-08-23 16:35:41
【问题描述】:
我正在使用 Python 在 Google App Engine 上开发一个应用程序。
我有一个可以返回各种输出的处理程序(目前是 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
我专门处理每个输出类型以及每个“断言”的案例。
我很想在这里提供有关如何清除它的建议、模式和示例(我知道尝试并保持我正在做的事情将是一场噩梦)。
我正在玩弄拥有和引发自定义异常并拥有一个装饰器的想法,该装饰器将自动计算出如何显示错误消息 - 我认为这是一个好主意,但我很想获得一些基于反馈和建议过去人们是如何做到这一点的。
【问题讨论】:
标签: python google-app-engine error-handling design-patterns