package org.talend.dataprep.exception;

import java.io.StringWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class TDPExceptionController {

    @ExceptionHandler(InternalException.class)
    public @ResponseBody String handleInternalError(HttpServletResponse response, InternalException e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        final StringWriter message = new StringWriter();
        e.writeTo(message);
        return message.toString();
    }

    @ExceptionHandler(UserException.class)
    public @ResponseBody String handleUserError(HttpServletResponse response, UserException e) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        final StringWriter message = new StringWriter();
        e.writeTo(message);
        return message.toString();
    }

}

@@ -111,16 +111,59 @@
 ponse;%0A%0A
+import org.springframework.http.MediaType;%0A
 import o
@@ -219,16 +219,16 @@
 Advice;%0A
-
 import o
@@ -629,16 +629,83 @@
 ERROR);%0A
+        response.setContentType(MediaType.APPLICATION_JSON_VALUE);%0A
         
@@ -955,32 +955,32 @@
 rException e) %7B%0A
-
         response
@@ -1027,16 +1027,83 @@
 QUEST);%0A
+        response.setContentType(MediaType.APPLICATION_JSON_VALUE);%0A
         

Fix wrong content-type in case of exception (should be application/json).