ripper234
ripper234

Reputation: 229998

Creating a customized 404/500 error page in Play Framework

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

Upvotes: 15

Views: 11093

Answers (4)

John
John

Reputation: 2761

You can use this :

import javax.inject.{Inject, Singleton}
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc.Results._
import play.api.mvc.{RequestHeader, Result}

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class ErrorHandler @Inject()(implicit executionContext:ExecutionContext) extends   DefaultHttpErrorHandler{



override def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = {
exception match {
  case e:scala.MatchError => Future(Ok("Page Not Found"))
  case _ => Future(Ok("Internal Server Error :("))

}

}


}

Upvotes: 0

resamsel
resamsel

Reputation: 225

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.

Upvotes: 8

bjfletcher
bjfletcher

Reputation: 11508

In documentation for 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:

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

Not found (404) error page

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

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.

Default error page template source

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

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

Upvotes: 4

electrotype
electrotype

Reputation: 8796

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

Upvotes: 13

Related Questions