I have been trying to setup a jetty server with common error handling. I'm using jetty 9.4.44.v20210927 home distribution. As per the jetty-9 doc, i have written a class that extends ErrorHandler
class and overrides the handle()
method. I exported it as a jar and included it in lib/ext and the class is being discovered and loaded properly (verified using --debug
and -Dorg.eclipse.jetty.LEVEL=DEBUG
flags). I've also added this to jetty.xml server instance as instructed in the doc. No matter what change is done, the error is only being handed by the default ErrorPageErrorHandler and not my custom class. This is the xml context file that i have
<?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
...
<Call name="addBean">
<Arg>
<New class="com.example.test.CustomErrorHandler"/>
</Arg>
</Call>
...
</Configure>
NOTE : I'm trying to set the errorHandler for the entire server instance and not for a specific context. Setting the same error handler for a single context works. But the same does not work for server instance. Also, i'm trying to achieve this using jetty IoC.
You want to use Server.setErrorHandler(ErrorHandler)
So that means ...
<?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
...
<Set name="errorHandler">
<New class="com.example.test.CustomErrorHandler"/>
</Set>
...
</Configure>
This server level ErrorHandler
is used for non-webapp based requests, or even requests that have an error that cannot be dispatched to a webapp.
Each Web App will have it's own unique ErrorPageErrorHandler
, created at the time when the Web App is being deployed.