Handling uploaded files exceeding max size

play-1
#1

application.conf property upload.maxFileSize allows limiting the size of uploaded files (https://www.playframework.com/documentation/1.5.x/configuration#upload).

When such limit is exceeded, an exception like the one below is thrown.

Is it possible to catch and handle these exceptions so a proper page is displayed to users?

Note: Adding a handler such as

@Catch(value = Exception.class)
public static void handleException(Exception e) {
	...
}

to the controller doesn’t work as the exception seems to be thrown before any controller action is invoked (as shown in the stack trace).

Exception

play.data.parsing.ApacheMultipartParser$FileSizeLimitExceededException: The field test exceeds its maximum permitted  size of 204800 characters.
	at play.data.parsing.ApacheMultipartParser$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(ApacheMultipartParser.java:875)
	at org.apache.commons.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:78)
	at org.apache.commons.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:137)
	at java.io.FilterInputStream.read(FilterInputStream.java:107)
	at org.apache.commons.fileupload.util.Streams.copy(Streams.java:100)
	at org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
	at play.data.parsing.ApacheMultipartParser.parse(ApacheMultipartParser.java:529)
	at play.mvc.Scope$Params.checkAndParse(Scope.java:323)
	at play.mvc.Scope$Params.getAll(Scope.java:411)
	at play.mvc.ActionInvoker.getActionMethodArgs(ActionInvoker.java:633)
	at play.data.validation.ValidationPlugin$Validator.validateAction(ValidationPlugin.java:125)
	at play.data.validation.ValidationPlugin.beforeActionInvocation(ValidationPlugin.java:73)
	at play.plugins.PluginCollection.beforeActionInvocation(PluginCollection.java:726)
	at play.mvc.ActionInvoker.invoke(ActionInvoker.java:137)
	at Invocation.HTTP Request(Play!)

@7bnfb67pg
Internal Server Error (500) for request POST /test

Oops: IllegalStateException
Unexpected error : Unexpected Error, caused by exception IllegalStateException: Error when handling upload

play.exceptions.UnexpectedException: Unexpected Error
	at play.data.validation.ValidationPlugin.beforeActionInvocation(ValidationPlugin.java:88)
	at play.plugins.PluginCollection.beforeActionInvocation(PluginCollection.java:726)
	at play.mvc.ActionInvoker.invoke(ActionInvoker.java:137)
	at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalStateException: Error when handling upload
	at play.data.parsing.ApacheMultipartParser.parse(ApacheMultipartParser.java:568)
	at play.mvc.Scope$Params.checkAndParse(Scope.java:323)
	at play.mvc.Scope$Params.getAll(Scope.java:411)
	at play.mvc.ActionInvoker.getActionMethodArgs(ActionInvoker.java:633)
	at play.data.validation.ValidationPlugin$Validator.validateAction(ValidationPlugin.java:125)
	at play.data.validation.ValidationPlugin.beforeActionInvocation(ValidationPlugin.java:73)
	... 3 more
Caused by: play.data.parsing.ApacheMultipartParser$FileSizeLimitExceededException: The field test exceeds its maximum permitted  size of 204800 characters.
	at play.data.parsing.ApacheMultipartParser$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(ApacheMultipartParser.java:875)
	at org.apache.commons.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:78)
	at org.apache.commons.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:137)
	at java.io.FilterInputStream.read(FilterInputStream.java:107)
	at org.apache.commons.fileupload.util.Streams.copy(Streams.java:100)
	at org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
	at play.data.parsing.ApacheMultipartParser.parse(ApacheMultipartParser.java:529)
	... 8 more