package se.kth.csc.controller;

import com.google.common.base.Throwables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;

/**
 * General error handler for the application.
 */
@ControllerAdvice
class ExceptionHandlerAdvice {
    private static final Logger log = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);

    /**
     * Handle exceptions thrown by handlers.
     */
    @ExceptionHandler(value = NotFoundException.class)
    public String notFound(Exception exception, WebRequest request, HttpServletResponse response) {
        log.error("Not found: {}", request.getDescription(true), exception);
        response.setStatus(404); // Not found

        return "error/not-found";
    }

    @ExceptionHandler(value = DataIntegrityViolationException.class)
    public void dataIntegrityViolation(HttpServletResponse response) {
        response.setStatus(409); // Conflict
    }
}

@@ -314,24 +314,85 @@
 ionHandler;%0A
+import org.springframework.web.bind.annotation.ResponseBody;%0A
 import org.s
@@ -845,32 +845,50 @@
 xception.class)%0A
+    @ResponseBody%0A
     public Strin
@@ -983,156 +983,138 @@
     
-log.error(%22Not found: %7B%7D%22, request.getDescription(true), exception);%0A        response.setStatus(404); // Not found%0A%0A        return %22error/not-found%22
+response.setStatus(404); // Not found%0A        return String.format(%22%25s: %25s%22, request.getDescription(true), exception.getMessage())
 ;%0A  
@@ -1183,24 +1183,42 @@
 tion.class)%0A
+    @ResponseBody%0A
     public v
@@ -1220,12 +1220,14 @@
 lic 
-void
+String
  dat
@@ -1246,16 +1246,57 @@
 olation(
+Exception exception, WebRequest request, 
 HttpServ
@@ -1364,16 +1364,110 @@
 onflict%0A
+        return String.format(%22%25s: %25s%22, request.getDescription(true), exception.getMessage());%0A
     %7D%0A%7D%0A

Change error response codes