Vyacheslav
Vyacheslav

Reputation: 1256

Custom ErrorHandler in dropwizard application

I'm trying to set a custom ErrorHandler in dropwizard 1.0.2

In my Application class in the run method I have the following lines:

  environment.getApplicationContext().setErrorHandler(new CustomErrorHandler());
  environment.getAdminContext().setErrorHandler(new CustomErrorHandler());

However, the code from CustomErrorHandler is not called, instead default ErrorHandler class is being used when e.g. I hit a URL that cannot be served.

While debugging the issue, I realize that ContainerLifeCycle object contains the following bean: {org.eclipse.jetty.server.handler.ErrorHandler@375084c9,AUTO} and does not contain a bean for CustomErrorHandler. My guess is that my error handler gets overwritten when default ErroHandler is set later upon application startup.

Any pointers how to set a custom error handler would be highly appreciated.

Upvotes: 0

Views: 419

Answers (1)

hbakkum
hbakkum

Reputation: 73

The only way I've managed to do this is within a server lifecycle listener, e.g.

environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
        @Override
        public void serverStarted(Server server) {
            server.setErrorHandler(new MyCustomErrorHandler());
        }
    });

Upvotes: 1

Related Questions