0
votes

Hey i am working on scala play framework, what i need to do is, when someone enter some random(unknown) url, that is not defined in routes file, they need to be routed to the some another not found page, with 404 page not found http header response. instead of displaying the whole routes file as error.

using sbt : sbt launcher version 0.13.8

using scala : scalaVersion := "2.11.6" using play framework 2x

Action not found

4
Which version of play framework are you using?Tyth
@Tyth sbt launcher version 0.13.8Hackaholic
@Tyth scalaVersion := "2.11.6"Hackaholic
The routes are only displayed when you're running in development mode.Steve Chaloner

4 Answers

1
votes

You can override these in your Global.scala / Global.java file. For Java the could look similar to this:

public F.Promise<Result> onHandlerNotFound(Http.RequestHeader request) {
    return F.Promise.<Result>pure(notFound(
            Messages.get("error.routeNotFound")
    ));
}
0
votes

I guess you are looking for Scala code for Global.scala to display your own 404 Page.

Global.scala should be under your /views folder. And override the onHandlerNotFound method in it, like this:

override def onHandlerNotFound(request: RequestHeader) = {
  var cookies: Seq[Cookie] = Seq()
  Future.successful(NotFound("hello world!!!!").withCookies(cookies:_*))
}

The NotFound is a PageCtrl.Status method which may need a import at the top of Global.scala. You can try using NotFound("Hello World") to see what's happening. It will display pure text "hello world" instead of default 404 page.

So the final code should be like

override def onHandlerNotFound(request: RequestHeader) = {
  var cookies: Seq[Cookie] = Seq()
  Future.successful(NotFound(view.html.yourOwn404PageHtml).withCookies(cookies:_*))
}

The withCookies is just used to create a Result object. You can use other method belongs to NotFound().

0
votes

In Play2.6 you can handle unknown/broken API call buy using below code.

import javax.inject._
import models.Response
import play.api._
import play.api.http.DefaultHttpErrorHandler
import play.api.libs.json.Json
import play.api.mvc.Results._
import play.api.mvc._
import play.api.routing.Router
import _root_.controllers.JSONHelper.ResponseWrites

import scala.concurrent._

class ErrorHandler @Inject()(env: Environment,
                              config: Configuration,
                              sourceMapper: OptionalSourceMapper,
                              router: Provider[Router])
  extends DefaultHttpErrorHandler(env, config, sourceMapper, router) {

  override protected def onNotFound(request: RequestHeader, message: String): Future[Result] = {
    Future.successful(
      NotFound(Json.toJson(Response(isSuccess = false,"Resource Not Found", ("", ""))))
    )
  }

}
-1
votes

You can use customized ErrorHandler to return a json format error message, status result with status code or blank page instead.

package errors

import play.api.http.HttpErrorHandler
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent._
import javax.inject.Singleton

@Singleton
class MyErrorHandler extends HttpErrorHandler {

  def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Future.successful(
      Ok() // Replace with your error handle here
    )
  }

  def onServerError(request: RequestHeader, exception: Throwable) = ???
}

In application.conf

play.http.errorHandler = "errors.MyErrorHandler"

You can find detail on playframework document. https://www.playframework.com/documentation/2.6.x/ScalaErrorHandling