Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 612
ripper234 Avatar asked Jan 15 '12 16:01

ripper234


1 Answers

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

like image 73
bjfletcher Avatar answered Oct 21 '22 15:10

bjfletcher