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

46 lines
1.6 KiB
Java

package eu.eudat.controllers.controllerhandler;
import eu.eudat.core.logger.Logger;
import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
import eu.eudat.models.data.security.Principal;
import org.springframework.beans.factory.annotation.Autowired;
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;
import types.LoggingType;
import java.util.HashMap;
import java.util.Map;
/**
* Created by ikalyvas on 6/12/2018.
*/
@ControllerAdvice
public class ControllerErrorHandler {
private Logger logger;
@Autowired
public ControllerErrorHandler(Logger logger) {
this.logger = logger;
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public void processValidationError(Principal principal, Exception ex) throws Exception {
ApiExceptionLoggingModel<Exception, Principal> apiExceptionLoggingModel = new ApiExceptionLoggingModel<>();
apiExceptionLoggingModel.setCode(HttpStatus.BAD_REQUEST);
apiExceptionLoggingModel.setUser(principal);
Map<String,Exception> exceptionMap = new HashMap<>();
exceptionMap.put("exception",ex);
apiExceptionLoggingModel.setData(exceptionMap);
apiExceptionLoggingModel.setMessage(ex.getMessage());
apiExceptionLoggingModel.setType(LoggingType.ERROR);
this.logger.error(apiExceptionLoggingModel);
return;
}
}