在Play框架中创建一个定制的404/500错误页面

时间:2021-09-29 07:37:54

How can one create global, custom looks for their 404/505 error pages using Play?

如何使用Play创建全局、自定义的404/505错误页面?

3 个解决方案

#1


12  

In Play Framework 1, you simply have to modify the provided app/views/errors/404.html and app/views/errors/500.html .

在Play Framework 1中,您只需修改所提供的应用程序/视图/错误/404。html和app / views /错误/ 500。html。

#2


6  

Error handling changed in 2.5.x. You now need an ErrorHandler to handle errors and display custom error pages yourself.

错误处理在2.5.x中更改。现在需要一个ErrorHandler来处理错误并显示自定义错误页面。

The documentation for 2.5.x says:

2.5的文档。x表示:

Supplying a custom error handler

import play.api.http.HttpErrorHandler
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent._
import javax.inject.Singleton;

@Singleton
class ErrorHandler extends HttpErrorHandler {

  def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Future.successful(
      Status(statusCode)("A client error occurred: " + message)
    )
  }

  def onServerError(request: RequestHeader, exception: Throwable) = {
    Future.successful(
      InternalServerError("A server error occurred: " + exception.getMessage)
    )
  }
}

You can find the default error pages on Github: https://github.com/playframework/playframework/tree/master/framework/src/play/src/main/scala/views/defaultpages

您可以在Github上找到默认的错误页面:https://github.com/playframework/playframework/playframework/tree/master/framework/framework/src/play/src/play/src/main/main/main/scala/scala/views/defaultpages

See https://www.playframework.com/documentation/2.5.x/ScalaErrorHandling for more detail.

详情请参见https://www.playframework.com/documentation/2.5.x/ScalaErrorHandling。

#3


2  

In documentation for 2.3.x:

在文档2.3.x:

Providing an application error page

When an exception occurs in your application, the onError operation will be called. The default is to use the internal framework error page:

当应用程序中发生异常时,将调用onError操作。默认是使用内部框架错误页面:

import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent.Future

object Global extends GlobalSettings {

  override def onError(request: RequestHeader, ex: Throwable) = {
    Future.successful(InternalServerError(
      views.html.errorPage(ex)
    ))
  }

}

Source: https://www.playframework.com/documentation/2.3.x/ScalaGlobal#Providing-an-application-error-page

来源:https://www.playframework.com/documentation/2.3.x/ScalaGlobal Providing-an-application-error-page

Not found (404) error page

You'll need a onHandlerNotFound handler alongside the above onError handler:

您将需要一个onHandlerNotFound处理程序和上面的onError处理程序:

override def onHandlerNotFound(request: RequestHeader) = {
  Future.successful(NotFound(views.html.errors.notFoundPage()))
}

Source: this is not documented but have a look in the GlobalSettings trait definition.

资料来源:没有文件记载,但请参阅GlobalSettings trait的定义。

Default error page template source

The source for the default error template in production for 2.3.x can be read here:

产品中默认错误模板的源代码。x可以在这里阅读:

https://github.com/playframework/playframework/blob/2.3.x/framework/src/play/src/main/scala/views/defaultpages/error.scala.html

https://github.com/playframework/playframework/blob/2.3.x/framework/src/play/src/main/scala/views/defaultpages/error.scala.html

#1


12  

In Play Framework 1, you simply have to modify the provided app/views/errors/404.html and app/views/errors/500.html .

在Play Framework 1中,您只需修改所提供的应用程序/视图/错误/404。html和app / views /错误/ 500。html。

#2


6  

Error handling changed in 2.5.x. You now need an ErrorHandler to handle errors and display custom error pages yourself.

错误处理在2.5.x中更改。现在需要一个ErrorHandler来处理错误并显示自定义错误页面。

The documentation for 2.5.x says:

2.5的文档。x表示:

Supplying a custom error handler

import play.api.http.HttpErrorHandler
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent._
import javax.inject.Singleton;

@Singleton
class ErrorHandler extends HttpErrorHandler {

  def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Future.successful(
      Status(statusCode)("A client error occurred: " + message)
    )
  }

  def onServerError(request: RequestHeader, exception: Throwable) = {
    Future.successful(
      InternalServerError("A server error occurred: " + exception.getMessage)
    )
  }
}

You can find the default error pages on Github: https://github.com/playframework/playframework/tree/master/framework/src/play/src/main/scala/views/defaultpages

您可以在Github上找到默认的错误页面:https://github.com/playframework/playframework/playframework/tree/master/framework/framework/src/play/src/play/src/main/main/main/scala/scala/views/defaultpages

See https://www.playframework.com/documentation/2.5.x/ScalaErrorHandling for more detail.

详情请参见https://www.playframework.com/documentation/2.5.x/ScalaErrorHandling。

#3


2  

In documentation for 2.3.x:

在文档2.3.x:

Providing an application error page

When an exception occurs in your application, the onError operation will be called. The default is to use the internal framework error page:

当应用程序中发生异常时,将调用onError操作。默认是使用内部框架错误页面:

import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent.Future

object Global extends GlobalSettings {

  override def onError(request: RequestHeader, ex: Throwable) = {
    Future.successful(InternalServerError(
      views.html.errorPage(ex)
    ))
  }

}

Source: https://www.playframework.com/documentation/2.3.x/ScalaGlobal#Providing-an-application-error-page

来源:https://www.playframework.com/documentation/2.3.x/ScalaGlobal Providing-an-application-error-page

Not found (404) error page

You'll need a onHandlerNotFound handler alongside the above onError handler:

您将需要一个onHandlerNotFound处理程序和上面的onError处理程序:

override def onHandlerNotFound(request: RequestHeader) = {
  Future.successful(NotFound(views.html.errors.notFoundPage()))
}

Source: this is not documented but have a look in the GlobalSettings trait definition.

资料来源:没有文件记载,但请参阅GlobalSettings trait的定义。

Default error page template source

The source for the default error template in production for 2.3.x can be read here:

产品中默认错误模板的源代码。x可以在这里阅读:

https://github.com/playframework/playframework/blob/2.3.x/framework/src/play/src/main/scala/views/defaultpages/error.scala.html

https://github.com/playframework/playframework/blob/2.3.x/framework/src/play/src/main/scala/views/defaultpages/error.scala.html