How to create custom “Action Not Found” page on Play Framework 2.4 (Java)

783 views
Skip to first unread message

Satomi Moriki

unread,
Dec 15, 2015, 8:01:20 PM12/15/15
to play-framework

Currently, if I access to non-defined path on my play framework application, default "Action Not Found page" appears. 

I tried to make custom "Action Not Found page" in order not to show system information on browser.


I checked the page.

https://www.playframework.com/documentation/2.4.x/JavaErrorHandling


And I implemented the following code.

package controllers;

import play.Logger;
import play.http.HttpErrorHandler;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;
import views.html.*;

public class ErrorHandler implements HttpErrorHandler {
    public Promise<Result> onClientError(RequestHeader request, int statusCode, String message) {
        Logger.debug("onClientError");
        return Promise.<Result> pure(
                Results.badRequest(error.render())
        );
    }

    public Promise<Result> onServerError(RequestHeader request, Throwable exception) {
        Logger.debug("onServerError");
        return Promise.<Result> pure(
                Results.internalServerError("A server error occurred: " + exception.getMessage())
        );
    }
}


However error.scala.html isn't shown and default "Action Not Found page" appears. 

Logger.debug also wasn't called.


I found same question for Scala, but there isn't any answer.

How to handle "Action not found" with Dependency Injection Play framework 2.4

Could you give me any advice?



Satomi Moriki

unread,
Dec 15, 2015, 8:04:36 PM12/15/15
to play-framework
I forgot to put version : [2.4-java]

Satomi Moriki

unread,
Dec 16, 2015, 2:00:58 AM12/16/15
to play-framework

I solved this problem by myself! I noticed that I should add ErrorHandler.java to default package or add to another package and write its class path to application.conf like this.

play.http.errorHandler = "com.example.ErrorHandler"

Thank you for your support!


On Wednesday, December 16, 2015 at 10:01:20 AM UTC+9, Satomi Moriki wrote:

Enrico Morelli

unread,
Dec 16, 2015, 11:13:00 AM12/16/15
to play-framework


On Wednesday, December 16, 2015 at 8:00:58 AM UTC+1, Satomi Moriki wrote:

I solved this problem by myself! I noticed that I should add ErrorHandler.java to default package or add to another package and write its class path to application.conf like this.

play.http.errorHandler = "com.example.ErrorHandler"

Thank you for your support!



 
I'm trying the same thing and I've the same problem. But for default package do you mean the controllers package?
I've put the ErrorHandler.java in the controllers package without modify the application.conf, is it correct?
Reply all
Reply to author
Forward
0 new messages