object ExceptionFilter extends EssentialFilter {
def apply(nextFilter: EssentialAction) = new EssentialAction {
def apply(requestHeader: RequestHeader) = {
val next: Iteratee[Array[Byte], Result] = nextFilter(requestHeader)
// Say your backend throws an ItemNotFound exception.
next recoverWith {
case e: ItemNotFound => Iteratee.ignore[Array[Byte]]
.map(_ => Results.NotFound("Item not in the database!"))
}
}
}
}
然后在全局设置中将其挂钩:
object Global extends WithFilters(CSRFFilter(), ExceptionFilter) with GlobalSettings
2 回答
你应该看一下
GlobalSettings
:https://www.playframework.com/documentation/2.3.x/ScalaGlobal特别是,它允许您覆盖:
onError
可能是你要找的那个,但其他的也可能有用:)另一种方法是使用filter,例如:
然后在全局设置中将其挂钩:
如果需要,这可能允许您对请求正文执行某些操作 . 我同意在大多数情况下使用
GlobalSettings.onError
可能是最简单的方法 .