How to handle Scala Play Framework 2 404 and 500 errors

To handle 404 and 500 errors in the Scala Play Framework 2, you need to create a Global.scala object in your main app directory. The object should extend GlobalSettings, and override the necessary methods.

The following example Play Framework Global object, saved as app/Global.scala, demonstrates this:

import play.api._
import play.api.mvc._
import play.api.mvc.Results._

object Global extends GlobalSettings {

  // called when a route is found, but it was not possible to bind the request parameters
  override def onBadRequest(request: RequestHeader, error: String) = {
    BadRequest("Bad Request: " + error)

  // 500 - internal server error
  override def onError(request: RequestHeader, throwable: Throwable) = {

  // 404 - page not found error
  override def onHandlerNotFound(request: RequestHeader): Result = {


The method views.html.errors.onError(throwable) refers to a Play template file I named onError.scala.html, and placed in my app/views/errors folder. A simple file looks like this:

@(throwable: Throwable)

@main("onError Happened") {

  <h1>onError Happened</h1>	


The method views.html.errors.onHandlerNotFound(request) refers to a Play template file I named onHandlerNotFound.scala.html, and is also in my app/views/errors folder. A simple version of that file looks like this:

@(request: RequestHeader)

@main("Handler Not Found") {

  <h1>Handler Not Found</h1>	
  <p>You requested: @request.path</p>

More information

For more information, see the following Play Framework pages:

If you want to handle 404 "not found" and 500 "internal server error" errors in your Play Framework 2 application, I hope this post has been helpful.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.