Frank Zinner bio photo

Frank Zinner

I'm a passionated senior software developer ...

Twitter Facebook LinkedIn Github Xing

Play2 - a custom error handler

Sometimes you need a custom error handling and don’t want to stick with play’s default handler, so this short post is about writing a custom one.

To use a custom error handler with the play2 framework one needs to override the default methods in the GlobalSettings object. Our methods are namely the onError, onHandlerNotFound and the onBadRequest method.

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[Result] = {

  override def onHandlerNotFound(request: RequestHeader): Future[Result] = {

  override def onBadRequest(request: RequestHeader, error: String): Future[Result] = {
	Future.successful(BadRequest("Bad Request: " + error))

The templates used in this example are under the views folder in a subfolder named errors namely onError, onHandlerNotFound. The BadRequest() is the default one.