Creating a customized 404/500 error page in Play Framework

前端 未结 4 540
感情败类
感情败类 2020-12-30 06:44

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

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 07:13

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

    The documentation for 2.5.x says:

    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

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

提交回复
热议问题