You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
argos/dmp-backend/web/src/main/java/eu/eudat/controllers/controllerhandler/ControllerErrorHandler.java

52 lines
2.0 KiB
Java

6 years ago
package eu.eudat.controllers.controllerhandler;
6 years ago
import eu.eudat.core.logger.Logger;
import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
6 years ago
import eu.eudat.models.data.helpers.responses.ResponseItem;
6 years ago
import eu.eudat.models.data.security.Principal;
6 years ago
import eu.eudat.types.ApiMessageCode;
6 years ago
import org.springframework.beans.factory.annotation.Autowired;
6 years ago
import org.springframework.core.annotation.Order;
6 years ago
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
6 years ago
import types.LoggingType;
6 years ago
6 years ago
import javax.annotation.Priority;
6 years ago
import java.util.HashMap;
import java.util.Map;
6 years ago
6 years ago
/**
* Created by ikalyvas on 6/12/2018.
*/
6 years ago
@ControllerAdvice
6 years ago
@Priority(5)
6 years ago
public class ControllerErrorHandler {
6 years ago
private Logger logger;
6 years ago
@Autowired
6 years ago
public ControllerErrorHandler(Logger logger) {
this.logger = logger;
6 years ago
}
6 years ago
@ExceptionHandler(Exception.class)
6 years ago
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
6 years ago
public ResponseItem<Exception> processValidationError(Principal principal, Exception ex) throws Exception {
6 years ago
ApiExceptionLoggingModel<Exception, Principal> apiExceptionLoggingModel = new ApiExceptionLoggingModel<>();
apiExceptionLoggingModel.setCode(HttpStatus.BAD_REQUEST);
apiExceptionLoggingModel.setUser(principal);
6 years ago
Map<String, Exception> exceptionMap = new HashMap<>();
exceptionMap.put("exception", ex);
6 years ago
apiExceptionLoggingModel.setData(exceptionMap);
apiExceptionLoggingModel.setMessage(ex.getMessage());
apiExceptionLoggingModel.setType(LoggingType.ERROR);
6 years ago
ex.printStackTrace();
6 years ago
this.logger.error(apiExceptionLoggingModel);
6 years ago
return new ResponseItem<Exception>().message(ex.getMessage()).status(ApiMessageCode.DEFAULT_ERROR_MESSAGE);
6 years ago
}
}