Home > OS >  Error Handler For Server Instance using Jetty9 Ioc
Error Handler For Server Instance using Jetty9 Ioc

Time:03-18

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" >
    ...
    <Call name="addBean">
        <Arg>
            <New />
        </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.

CodePudding user response:

You want to use Server.setErrorHandler(ErrorHandler)

See: https://javadoc.io/static/org.eclipse.jetty/jetty-server/9.4.45.v20220203/org/eclipse/jetty/server/Server.html#setErrorHandler(org.eclipse.jetty.server.handler.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" >
    ...
    <Set name="errorHandler">
      <New />
    </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.

  • Related