PlayFramework ErrorHandling onClientError

Hi, I am trying to use a custom HTTP handler with play framework in Java by following the documentation Java Error Handling - 2.8.x. I know if I throw a runtime exception, by default the onServerError method will be called in the custom HTTP Handler. Can anyone tell me, how the onClientError method in the HTTP Handler will be trigger?

I know that the onClientError will be invoked when a client error (or error in 4xx series will occur) but how exactly do I throw the client error from inside a controller.
P.S. I am using the play framework(Java) as a gateway server due to which I need to rethrow the exception thrown by the internal servers.

The client errors won’t happen in your controllers. It covers things like a route that can’t be found or a malformed header, and when those things happen onClientError will be called and your controller won’t even be hit.

@slisaasquatch Thank you for answering.

Is there any way to trigger the onClientError from within the controller? Since I am using the play framework as a gateway server, the internal server may throw a client error (4xx series error) and I may need to create a 400 type error and let the error handler handle that error.

Also, since you are saying that it won’t happen in the controller, but how is a 404 error handled when doing a database query or findById DB query using play framework Java ?

Thank you.

I don’t think you can trigger onClientError from within the controller. If you want to throw an exception in the controller and you want to turn that into a 400 error, then you can do that by checking the exception in onServerError and return 400.

You can inject HttpErrorHandler into your controller and then later call errorhandler.onClientError(...) on it. Didn’t try but something like that should work.