package org.datavaultplatform.broker.exceptions;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

/**
 * User: Robin Taylor
 * Date: 01/07/2016
 * Time: 09:11
 */

@ControllerAdvice
public class GlobalExceptionHandler {

    // Note - Add methods for more specific Exceptions here.

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<String> defaultExceptionHandler(HttpServletRequest req, Exception e) throws Exception {
        // If the exception is annotated with @ResponseStatus rethrow it and let the framework handle it.
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
            throw e;

        return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);

    }

}

@@ -440,16 +440,196 @@
 t;%0A%0A/**%0A
+ * Catches any Exceptions thrown by a Controller, or anything further down the stack, allowing us to return%0A * a ResponseEntity that can be nicely formatted as Json by Jackson.%0A *%0A
  * User:

Add description to GlobalExceptionHandler