Better way to handle errors in tornado request handler

前端 未结 3 925
星月不相逢
星月不相逢 2021-02-15 17:47

There are two similar handlers: AgeHandler1 and AgeHandler2. In the first one we simply raise a specific exception to return an error message, in the second - we manually return

3条回答
  •  难免孤独
    2021-02-15 18:09

    In general the best approach is to override RequestHandler.write_error. This is similar to your first approach, but you don't need the try/except in the body of the handler because Tornado will handle this for you.

    Explicit tests like those in your second example are also good, but it's impractical to catch all possible errors this way so you'll always need something to handle uncaught exceptions.

提交回复
热议问题