我正在使用play framework(2.3.x)构建一个restful API。在Play框架中处理异常
今天我有一个try/catch块围绕API控制器中的所有API函数,以便能够捕获异常并返回一个通用的“错误json”对象。
例子:
def someApiFuntion() = Action { implicit request =>
try {
// Do some magic
Ok(magicResult)
} catch {
case e: Exception =>
InternalServerError(Json.obj("code" -> INTERNAL_ERROR, "message" -> "Server error"))
}
}
我的问题是:是否有必要在每个API函数在try/catch啄,还是有解决的更好/更通用的方法?
退房这个问题:https://stackoverflow.com/questions/25547795/error-handling-in-play-framework – Mikesname 2014-08-29 19:30:33