[scaldi-play 0.5.8] Custom ErrorHandler in Play 2.4 Not Taking Effect

69 views
Skip to first unread message

Neil Chaudhuri

unread,
Sep 25, 2015, 10:56:25 PM9/25/15
to scaldi
I have a custom error handler bound like this

bind [HttpErrorHandler] to injected [MyErrorHandler] ('router -> injectProvider[Router])

But my custom error handler isn't taking effect. For example, I have implemented the onClientError method in my handler to render a custom page when a 404 occurs:

override def onClientError(request: RequestHeader, statusCode: Int, message: String = ""): Future[Result] = {
val result = if (statusCode == play.api.http.Status.NOT_FOUND) {
NotFound(
views.html.errors.
handlerNotFound(request)
)
}
...

Future.
successful(result)
}


But when I go to a nonexistent page, I get a Play error page instead of my own. Any ideas why my error handler isn't taking effect?

Thanks.

Oleg Ilyenko

unread,
Sep 26, 2015, 12:47:52 PM9/26/15
to scaldi
I debugged it a bit and turns out that there was a bug that made behavior in this case unpredictable (issue was with module loading). I fixed it in scaldi-play v0.5.9 which I already released, so very soon it should be available from central. I verified it with this example (also note that the `mailer` from your previous post works correctly without injecting it as a provider):

class MyModule extends Module {
  // ...

  bind [Mailer] to new SMTPMailer

  bind [HttpErrorHandler] to injected [MyErrorHandler] ('router -> injectProvider[Router])
}

class MyErrorHandler(env: Environment, config: Configuration, sourceMapper: OptionalSourceMapper, router: () => Router, mailer: Mailer)
    extends DefaultHttpErrorHandler(env, config, sourceMapper, new Provider[Router] {def get() = router()}) {
  override def onClientError(request: RequestHeader, statusCode: Int, message: String = ""): Future[Result] = {
    Future.successful(...)
  }
}

Hope this helps and solves the issue you are currently facing. If not, please don't hesitate to drop an email :)

Neil Chaudhuri

unread,
Sep 26, 2015, 11:45:20 PM9/26/15
to scaldi
First of all, thank you for the quick turnaround on the bug fix. I am sure I speak on behalf of the entire Scaldi user community when I say we appreciate your timely attention and guidance--and even bug fixes when necessary.

The bug fix works; I now see my custom error page. 

There is one small thing though. If I get rid of the Mailer provider (and another provider for an object I need in the handler I never mentioned before), I get a StackOverflowError when Scaldi initializes the handler. If I leave the providers as they were and use the latest version, it works. This isn't a huge deal, but I just thought I would bring it to your attention.

Thanks again.
Reply all
Reply to author
Forward
0 new messages