package de.rwth.idsg.bikeman.psinterface.exception;

import de.rwth.idsg.bikeman.psinterface.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
 * @since 23.02.2015
 */
@ControllerAdvice(basePackages = "de.rwth.idsg.bikeman.psinterface.rest")
@Slf4j
public class PsExceptionHandler {

    @ExceptionHandler(PsException.class)
    public ResponseEntity<PsExceptionMessage> processPsException(PsException e) {
        log.error("Exception happened", e);

        HttpStatus status;
        PsErrorCode errorCode = e.getErrorCode();
        switch (errorCode) {
            case NOT_REGISTERED:
                status = HttpStatus.NOT_ACCEPTABLE;
                break;

            case AUTH_ATTEMPTS_EXCEEDED:
                status = HttpStatus.FORBIDDEN;
                break;

            default:
                status = HttpStatus.INTERNAL_SERVER_ERROR;
        }

        PsExceptionMessage msg = new PsExceptionMessage(
                Utils.nowInSeconds(),
                errorCode.name(),
                e.getMessage()
        );
        return new ResponseEntity<>(msg, status);
    }
}

@@ -104,33 +104,140 @@
 ort 
-lombok.extern.slf4j.Slf4j
+de.rwth.idsg.bikeman.web.rest.exception.DatabaseException;%0Aimport lombok.extern.slf4j.Slf4j;%0Aimport org.hibernate.HibernateException
 ;%0Aim
@@ -457,16 +457,64 @@
 ndler;%0A%0A
+import javax.persistence.PersistenceException;%0A%0A
 /**%0A * @
@@ -1381,24 +1381,24 @@
 nSeconds(),%0A
-
             
@@ -1516,11 +1516,1218 @@
 );%0A    %7D
+%0A%0A    @ExceptionHandler(%7BHibernateException.class, PersistenceException.class, DatabaseException.class%7D)%0A    public ResponseEntity%3CPsExceptionMessage%3E processDatabaseException(Exception e) %7B%0A        log.error(%22Exception happened%22, e);%0A%0A        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;%0A        PsExceptionMessage msg = new PsExceptionMessage(%0A                Utils.nowInSeconds(),%0A                PsErrorCode.DATABASE_OPERATION_FAILED.name(),%0A                e.getMessage()%0A        );%0A        return new ResponseEntity%3C%3E(msg, status);%0A    %7D%0A%0A    // -------------------------------------------------------------------------%0A    // Fall-back%0A    // -------------------------------------------------------------------------%0A%0A    @ExceptionHandler(Exception.class)%0A    public ResponseEntity%3CPsExceptionMessage%3E processException(Exception e) %7B%0A        log.error(%22Exception happened%22, e);%0A%0A        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;%0A        PsExceptionMessage msg = new PsExceptionMessage(%0A                Utils.nowInSeconds(),%0A                PsErrorCode.UNKNOWN_SERVER_ERROR.name(),%0A                e.getMessage()%0A        );%0A        return new ResponseEntity%3C%3E(msg, status);%0A    %7D
 %0A%7D%0A

wrap every possible exception in a message