package org.talend.dataprep.exception;

import java.io.StringWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class TDPExceptionController {

    private static HttpEntity<String> buildHttpEntity(TDPException e) {
        StringWriter writer = new StringWriter();
        e.writeTo(writer);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return new HttpEntity<>(writer.toString(), headers);
    }

    @ExceptionHandler(InternalException.class)
    public HttpEntity<String> handleInternalError(HttpServletResponse response, InternalException e) {
        HttpEntity<String> entity = buildHttpEntity(e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return entity;
    }

    @ExceptionHandler(UserException.class)
    public HttpEntity<String> handleUserError(HttpServletResponse response, UserException e) {
        HttpEntity<String> entity = buildHttpEntity(e);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return entity;
    }

}

@@ -127,126 +127,51 @@
 rg.s
-pringframework.http.HttpEntity;%0Aimport org.springframework.http.HttpHeaders;%0Aimport org.springframework.http.MediaType
+lf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0Aim
@@ -297,16 +297,77 @@
 Handler;
+%0Aimport org.springframework.web.bind.annotation.ResponseBody;
 %0A%0A@Contr
@@ -426,22 +426,21 @@
    p
-rivate
+ublic
  static 
 Http
@@ -439,313 +439,84 @@
 tic 
-HttpEntity%3CString%3E buildHttpEntity(TDPException e) %7B%0A        StringWriter writer = new StringWriter();%0A        e.writeTo(writer);%0A        HttpHeaders headers = new HttpHeaders();%0A        headers.setContentType(MediaType.APPLICATION_JSON);%0A        return new HttpEntity%3C%3E(writer.toString(), headers);%0A    %7D
+final Logger LOGGER = LoggerFactory.getLogger(TDPExceptionController.class);
 %0A%0A  
@@ -571,34 +571,36 @@
  public 
-HttpEntity%3C
+@ResponseBody 
 String
-%3E
  handleI
@@ -669,64 +669,8 @@
 ) %7B%0A
-        HttpEntity%3CString%3E entity = buildHttpEntity(e);%0A
     
@@ -743,37 +743,134 @@
 R);%0A        
-return entity
+final StringWriter message = new StringWriter();%0A        e.writeTo(message);%0A        return message.toString()
 ;%0A    %7D%0A%0A   
@@ -920,34 +920,36 @@
  public 
-HttpEntity%3C
+@ResponseBody 
 String
-%3E
  handleU
@@ -1010,64 +1010,8 @@
 ) %7B%0A
-        HttpEntity%3CString%3E entity = buildHttpEntity(e);%0A
     
@@ -1082,21 +1082,118 @@
     
-return entity
+final StringWriter message = new StringWriter();%0A        e.writeTo(message);%0A        return message.toString()
 ;%0A  

Use @ResponseBody to write exception message.