我尝试使用有关如何处理异常的建议功能:
http://webapp-improved.appspot.com/guide/exceptions.html
在 main.py 中:
def handle_404(request, response, exception):
logging.exception(exception)
response.write('404 Error')
response.set_status(404)
def handle_500(request, response, exception):
logging.exception(exception)
response.write('A server error occurred!')
response.set_status(500)
class AdminPage(webapp2.RequestHandler):
def get(self):
...
admin_id = admin.user_id()
queues = httpRequests.get_queues(admin_id)
app = webapp2.WSGIApplication(...)
app.error_handlers[404] = handle_404
app.error_handlers[500] = handle_500
httpRequests.py 中的函数:
def get_queues(admin_id):
url = "http://localhost:8080/api/" + admin_id + "/queues"
result = urlfetch.fetch(url)
if (result.status_code == 200):
received_data = json.loads(result.content)
return received_data
在 API 中调用的函数:
class Queues(webapp2.RequestHandler):
def get(self, admin_id):
queues = queues(admin_id)
if queues == []:
self.abort(404)
else:
self.response.write(json.dumps(queues))
我被困在 httpRequests.py 中的 get_queues 中。如何使用 urlfetch 处理 HTTP 异常?