Search in sources :

Example 1 with HttpExceptionHandler

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());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpResponder(co.cask.http.HttpResponder) Discoverable(org.apache.twill.discovery.Discoverable) MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) HttpExceptionHandler(co.cask.cdap.common.HttpExceptionHandler)

Aggregations

HttpExceptionHandler (co.cask.cdap.common.HttpExceptionHandler)1 CommonNettyHttpServiceBuilder (co.cask.cdap.common.http.CommonNettyHttpServiceBuilder)1 MetricsReporterHook (co.cask.cdap.common.metrics.MetricsReporterHook)1 HttpResponder (co.cask.http.HttpResponder)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 Discoverable (org.apache.twill.discovery.Discoverable)1