Greg
Greg

Reputation: 167

Playframework: override BadRequest

I'm using Playframework 2.0 (scala version) and I want to override "BadRequest" method in one of my controllers. My controller extends a trait:

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.json.JsValue
import play.api.libs.json.JsObject

/*
 * Simple trait to factor common code used by all controllers...
 */
trait AbstractController extends Controller {
 // Personal useful methods
 // ...
 // Implementation of an override of BadRequest ?
}

My controller:

package controllers

import play.api._
import play.api.mvc._
import play.api.Play.current
import play.api.libs.json._
import play.api.libs.json._
import play.api.libs.concurrent.Promise

object MyController extends AbstractController
{

 def myFunc (s: String) = {
    BadRequest(s) // should return what I'll define in MY BadRequest implementation
  }
}

What I want to do: implement a new comportment of BadRequest (i.e. return a JSON with error details) only for controllers extending AbstractController. Play documentation suggest doing that in Global Object (extends GlobalSettings) and override onBadRequest method. But I do not want a so global method... only in specific controllers.

What is the best solution?

Thanks for your help. Greg

Upvotes: 2

Views: 1035

Answers (1)

Kim Stebel
Kim Stebel

Reputation: 42047

I don't really see what the problem is here. What is wrong with just overriding it?

trait AbstractController extends Controller {
  override val BadRequest = ... 

}

Upvotes: 1

Related Questions