[英]How to call default error pages in dev mode but not production (Play Framework 2.3.4 - Java)
Following the docs I was able to implement custom error handlers in my application by overriding methods in the GlobalSettings class: 遵循该文档,我能够通过覆盖GlobalSettings类中的方法在我的应用程序中实现自定义错误处理程序:
@Override
public Promise<Result> onBadRequest(RequestHeader request, String error) {
return Promise.<Result> pure(Results.notFound(com.me.project.views.html.pages.ErrorPage
.render(Constants.HTTP_400)));
}
@Override
public Promise<Result> onHandlerNotFound(RequestHeader request) {
return Promise.<Result> pure(Results.notFound(com.me.project.views.html.pages.ErrorPage
.render(Constants.HTTP_404)));
}
@Override
public Promise<Result> onError(RequestHeader request, Throwable t) {
return Promise.<Result> pure(Results.notFound(com.me.project.views.html.pages.ErrorPage
.render(Constants.HTTP_500)));
}
However I only want these to be used in production and not in development, as the errors being displayed in the browser are quite useful. 但是,我只希望将它们用于生产而不是开发中,因为在浏览器中显示的错误非常有用。
I am able to detect whether the application is in dev mode using: 我能够使用以下命令检测应用程序是否处于开发模式:
boolen isDev = play.api.Play.isDev(play.api.Play.current());
However I cannot call the default error pages as I do not know how. 但是我不知道如何调用默认错误页面。 How do you call the default error pages? 您如何称呼默认错误页面? I have looked at the code here , but my Scala is poor and I cannot seem to get it working. 我在这里查看了代码,但是我的Scala很差,而且似乎无法正常工作。 The parts that I though I should port to java in my global object were: 我应该在全局对象中移植到java的部分是:
private def defaultErrorHandler: HttpErrorHandler = {
Play.maybeApplication.fold[HttpErrorHandler](DefaultHttpErrorHandler) { app =>
app.injector.instanceOf[DefaultHttpErrorHandler]
}
}
and 和
def onError(request: RequestHeader, ex: Throwable): Future[Result] =
defaultErrorHandler.onServerError(request, ex)
def onHandlerNotFound(request: RequestHeader): Future[Result] =
defaultErrorHandler.onClientError(request, play.api.http.Status.NOT_FOUND)
def onBadRequest(request: RequestHeader, error: String): Future[Result] =
defaultErrorHandler.onClientError(request, play.api.http.Status.BAD_REQUEST, error)
But I cannot port this as things like Play.maybeApplication
cannot be resoled (are they protected)?!? 但是我不能移植它,因为Play.maybeApplication
东西无法解析(它们是否受到保护)?!?
I'm sure this should be an easy one but I'm a bit stuck. 我敢肯定这应该很容易,但是我有点卡住了。
Also as a side note can anyone explain the existence of these docs that seem to suggest that the error handling will completely change in 2.4.x :S, I assume that this question will be short lived until 2.4 gets released? 另外作为一个旁注,任何人都可以解释这些文档的存在,这些文档似乎暗示错误处理将在2.4.x:S中完全改变,我认为这个问题在2.4发布之前将是短暂的?
If you browse GlobalSettings.java sources, you will find out, the default implementations of onError(RequestHeader request, Throwable t)
and onHandlerNotFound(RequestHeader request)
return null, thus you can override these methods this way: 如果浏览GlobalSettings.java源,则会发现onError(RequestHeader request, Throwable t)
和onHandlerNotFound(RequestHeader request)
的默认实现返回null,因此您可以通过以下方式覆盖这些方法:
if(Play.isDev()){
return null;
} else {
// return promise of result...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.