Reputation: 545
According to the following pages, one can serve one's own error page when encountering server errors.
https://www.playframework.com/documentation/2.2.x/ScalaGlobal http://alvinalexander.com/scala/handling-scala-play-framework-2-404-500-errors
To do this, you need to override the app.Global.onError() method with something like the following:
override def onError(request: RequestHeader, ex: Throwable) = {
Future.successful(InternalServerError(
views.html.errorPage(ex)
))
}
However, I would like to not use InternalServerError(), but instead use my own page creation method that creates the entire page: html, head, body tags and all.
So I would like to override the method in the following way:
override def onError(request: RequestHeader, ex: Throwable) = {
controllers.PageCtrl.render("error", "500")
}
The PageCtrl.render() method creates all my pages. I would like it to create the error page as well. The render() method ensures all pages include all the proper JavaScript and CSS references, the proper header and footer, etc.
However, when I call the render() method, I get the following error:
type mismatch;
found : play.api.mvc.Action[play.api.mvc.AnyContent]
required: scala.concurrent.Future[play.api.mvc.Result]
Question: So how would I go about converting the AnyContent datatype into a Result datatype?
Upvotes: 0
Views: 858
Reputation: 33741
I would advise keeping the default error handler in place for local development (because it's useful to see exceptions and compile issues). You can do that and accomplish what you want with the following:
override def onError(request: RequestHeader, ex: Throwable): Future[Result] = {
if (play.api.Play.isDev(play.api.Play.current)) {
super.onError(request, ex)
} else
Future {
InternalServerError(
views.html.myawesomeerrorpage()
)
}
}
Upvotes: 1