Search code examples
scalaerror-handlingplayframeworkcustomizationclassnotfoundexception

Cannot load play.http.errorHandler - custom handler not invoked


I am attempting to load a custom error handler in Play Framework (2.6) as per the documentation but am receiving the following error message -

Cannot load play.http.errorHandler

Custom error handler (app/services/ErrorHandler)

   package services

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

   import scala.concurrent._

   @Singleton
   class ErrorHandler extends HttpErrorHandler {

     def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
       Future.successful(
         Status(statusCode)(
           message
         )
       )
     }

     def onServerError(request: RequestHeader, exception: Throwable) = {
       Future.successful(
         InternalServerError("A server errors occurred: " + exception.getMessage)
       )
     }
   }

application.conf

   play.http.errorHandler = "com.mySite.services.ErrorHandler"

Solution

  • In your code, the package isn't com.mySite.services. It's just services.

    application.conf

    play.http.errorHandler = "services.ErrorHandler"