Frage

I have the following Controller action as form post resolver:

def importCompletionsSubmit(indexName: String)  = AuthenticatedAction {
  Action.async {
    implicit request => {
      completionsForm.bindFromRequest().fold(
        errors => Future.successful(Ok(html.crudindex.importCompletionsForm(indexName, errors))),
        completions => {
          val autoCompletionService = new AutoCompletionService(new Elasticsearch)
          autoCompletionService.importCompletions(indexName, completions.text) map {
            result: BulkImportResult =>
              if (result.error) Redirect(routes.ListIndices.index(Option.empty[String])).flashing("error" -> Messages("error.bulkItemsFailed", result.failures))
              else Redirect(routes.ListIndices.index(Option.empty[String])).flashing("success" -> Messages("success.completionsAdded", result.requests))
          }
        }
      )
    }
  }
}

I know that I can change the max length value for this action but what I would like to do is sending the user back to the form with a nice error message when he enters too much text. If the request body is exceeding the default max length I get a completly blank page and only the browser console shows "413 (Request Entity Too Large)". I tried to catch this error in my global object but that did not change anything. It seems to me that the global onError trigger is not entered when a parser sends back an errorpage. Still a blank page. I also tried to catch that error inside the action but it seems to me that the action code is not entered because the body parser is already throwing this blank error page.

Is there a way to send the user back to the form action when the body exceeds the max length?

War es hilfreich?

Lösung

Something like this should work for you:

def test = Action.async(parse.maxLength(1024, parse.multipartFormData)) { implicit request =>
  Future(request.body match {
    case Left(MaxSizeExceeded(length)) => Ok(your_pretty_error_page.scala.html)
    case Right(body) => {
      ...
    }
  })
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top