use of co.cask.cdap.common.HttpExceptionHandler in project cdap by caskdata.
the class MessagingHttpService method startUp.
@Override
protected void startUp() throws Exception {
httpService = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.MESSAGING_SERVICE).setHost(cConf.get(Constants.MessagingSystem.HTTP_SERVER_BIND_ADDRESS)).setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService, Constants.Service.MESSAGING_SERVICE))).setWorkerThreadPoolSize(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_WORKER_THREADS)).setExecThreadPoolSize(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_EXECUTOR_THREADS)).setHttpChunkLimit(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_MAX_REQUEST_SIZE_MB) * 1024 * 1024).setExceptionHandler(new HttpExceptionHandler() {
@Override
public void handle(Throwable t, HttpRequest request, HttpResponder responder) {
// TODO: CDAP-7688. Override the handling to return 400 on IllegalArgumentException
if (t instanceof IllegalArgumentException) {
logWithTrace(request, t);
responder.sendString(HttpResponseStatus.BAD_REQUEST, t.getMessage());
} else {
super.handle(t, request, responder);
}
}
private void logWithTrace(HttpRequest request, Throwable t) {
LOG.trace("Error in handling request={} {} for user={}:", request.method().name(), request.uri(), Objects.firstNonNull(SecurityRequestContext.getUserId(), "<null>"), t);
}
}).setHttpHandlers(handlers).build();
httpService.start();
cancelDiscovery = discoveryService.register(new Discoverable(Constants.Service.MESSAGING_SERVICE, httpService.getBindAddress()));
LOG.info("Messaging HTTP server started on {}", httpService.getBindAddress());
}
Aggregations