2017-12-23 16:56:05 +01:00
|
|
|
package eu.eudat.controllers.controllerhandler;
|
|
|
|
|
2018-10-16 17:13:02 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
2019-05-29 12:55:13 +02:00
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
|
2018-08-31 16:12:31 +02:00
|
|
|
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.models.data.security.Principal;
|
2018-08-31 16:12:31 +02:00
|
|
|
import eu.eudat.types.ApiMessageCode;
|
2020-01-16 16:46:24 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2017-12-23 16:56:05 +01:00
|
|
|
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;
|
2018-06-27 12:29:21 +02:00
|
|
|
import types.LoggingType;
|
2017-12-23 16:56:05 +01:00
|
|
|
|
2018-09-18 14:41:24 +02:00
|
|
|
import javax.annotation.Priority;
|
2018-06-27 12:29:21 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2018-02-01 10:08:06 +01:00
|
|
|
|
2018-06-27 12:29:21 +02:00
|
|
|
/**
|
|
|
|
* Created by ikalyvas on 6/12/2018.
|
|
|
|
*/
|
2017-12-23 16:56:05 +01:00
|
|
|
@ControllerAdvice
|
2018-09-18 14:41:24 +02:00
|
|
|
@Priority(5)
|
2017-12-23 16:56:05 +01:00
|
|
|
public class ControllerErrorHandler {
|
2020-01-16 16:46:24 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ControllerErrorHandler.class);
|
2017-12-23 16:56:05 +01:00
|
|
|
|
2020-01-16 16:46:24 +01:00
|
|
|
// private Logger logger;
|
2017-12-23 16:56:05 +01:00
|
|
|
|
2020-01-16 16:46:24 +01:00
|
|
|
/*@Autowired
|
2018-06-27 12:29:21 +02:00
|
|
|
public ControllerErrorHandler(Logger logger) {
|
|
|
|
this.logger = logger;
|
2020-01-16 16:46:24 +01:00
|
|
|
}*/
|
2017-12-23 16:56:05 +01:00
|
|
|
|
2018-09-18 14:41:24 +02:00
|
|
|
@ExceptionHandler(Exception.class)
|
2017-12-23 16:56:05 +01:00
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
@ResponseBody
|
2018-08-31 16:12:31 +02:00
|
|
|
public ResponseItem<Exception> processValidationError(Principal principal, Exception ex) throws Exception {
|
2018-10-16 17:13:02 +02:00
|
|
|
ApiExceptionLoggingModel<String, Principal> apiExceptionLoggingModel = new ApiExceptionLoggingModel<>();
|
2018-06-27 12:29:21 +02:00
|
|
|
apiExceptionLoggingModel.setCode(HttpStatus.BAD_REQUEST);
|
|
|
|
apiExceptionLoggingModel.setUser(principal);
|
2018-10-16 17:13:02 +02:00
|
|
|
Map<String, String> exceptionMap = new HashMap<>();
|
2019-05-29 12:55:13 +02:00
|
|
|
ObjectWriter ow = new ObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false).writer().withDefaultPrettyPrinter();
|
2018-10-16 17:13:02 +02:00
|
|
|
try {
|
|
|
|
String json = ow.writeValueAsString(ex);
|
|
|
|
exceptionMap.put("exception", json);
|
|
|
|
apiExceptionLoggingModel.setData(ow.writeValueAsString(exceptionMap));
|
|
|
|
} catch (JsonProcessingException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2018-10-16 17:13:02 +02:00
|
|
|
}
|
2018-06-27 12:29:21 +02:00
|
|
|
apiExceptionLoggingModel.setMessage(ex.getMessage());
|
|
|
|
apiExceptionLoggingModel.setType(LoggingType.ERROR);
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(ex.getMessage(), ex);
|
2018-08-31 16:12:31 +02:00
|
|
|
return new ResponseItem<Exception>().message(ex.getMessage()).status(ApiMessageCode.DEFAULT_ERROR_MESSAGE);
|
2017-12-23 16:56:05 +01:00
|
|
|
}
|
|
|
|
}
|