Documentation

You are viewing the documentation for the 2.4.0-M2 development release. The latest stable release series is 3.0.x.

§Handling errors

There are two main types of errors that an HTTP application can return - client errors and server errors. Client errors indicate that the connecting client has done something wrong, server errors indicate that there is something wrong with the server.

Play will in many circumstances automatically detect client errors - these include errors such as malformed header values, unsupported content types, and requests for resources that can’t be found. Play will also in many circumstances automatically handle server errors - if your action code throws an exception, Play will catch this and generate a server error page to send to the client.

The interface through which Play handles these errors is HttpErrorHandler. It defines two methods, onClientError, and onServerError.

§Supplying a custom error handler

A custom error handler can be supplied by creating a class in the root package called ErrorHandler that implements HttpErrorHandler, for example:

import play.http.HttpErrorHandler;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;

public class ErrorHandler implements HttpErrorHandler {
    public Promise<Result> onClientError(RequestHeader request, int statusCode, String message) {
        return Promise.<Result>pure(
            Results.status(statusCode, "A client error occurred: " + message)
        );
    }

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

If you don’t want to place your error handler in the root package, or if you want to be able to configure different error handlers for different environments, you can do this by configuring the play.application.errorHandler configuration property in application.conf:

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

§Extending the default error handler

Out of the box, Play’s default error handler provides a lot of useful functionality. For example, in dev mode, when a server error occurs, Play will attempt to locate and render the piece of code in your application that caused that exception, so that you can quickly see and identify the problem. You may want to provide custom server errors in production, while still maintaining that functionality in development. To facilitate this, Play provides a DefaultHttpErrorHandler that has some convenience methods that you can override so that you can mix in your custom logic with Play’s existing behavior.

For example, to just provide a custom server error message in production, leaving the development error message untouched, and you also wanted to provide a specific forbidden error page:

import play.*;
import play.api.OptionalSourceMapper;
import play.api.UsefulException;
import play.core.Router;
import play.http.DefaultHttpErrorHandler;
import play.libs.F.*;
import play.mvc.Http.*;
import play.mvc.*;

import javax.inject.*;

public class ErrorHandler extends DefaultHttpErrorHandler {

    @Inject
    public ErrorHandler(Configuration configuration, Environment environment,
                        OptionalSourceMapper sourceMapper, Provider<Router.Routes> routes) {
        super(configuration, environment, sourceMapper, routes);
    }

    protected Promise<Result> onProdServerError(RequestHeader request, UsefulException exception) {
        return Promise.<Result>pure(
                Results.internalServerError("A server error occurred: " + exception.getMessage())
        );
    }

    protected Promise<Result> onForbidden(RequestHeader request, String message) {
        return Promise.<Result>pure(
                Results.forbidden("You're not allowed to access this resource.")
        );
    }
}

Checkout the full API documentation for DefaultHttpErrorHandler to see what methods are available to override, and how you can take advantage of them.

Next: Asynchronous HTTP programming