Jonik Jonik - 1 year ago 84
Scala Question

How to disable HTML error responses when using Action(parse.json)?

In a REST API implemented with Play Framework (2.4), I'm using

to parse JSON from incoming POST request body.

With my current code (see below),

Answer Source

The long html is produced by the default HttpErrorHandler. You can provide your own by following this guide. Quoting the example code:

class ErrorHandler extends HttpErrorHandler {

  def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Future.successful(
      Status(statusCode)("A client error occurred: " + message)
    )
  }

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

Note: if you manage your dependencies without Guice, you will have to provide your custom HttpErrorHandler in the ApplicationLoader

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download