org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / ExceptionTranslator.java
1 package org.onap.vid.model;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.commons.lang3.exception.ExceptionUtils;
5 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
6 import org.springframework.web.bind.annotation.ControllerAdvice;
7 import org.springframework.web.bind.annotation.ExceptionHandler;
8 import org.springframework.web.bind.annotation.ResponseBody;
9 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
10
11 import static org.onap.vid.utils.Logging.getMethodCallerName;
12
13 @ControllerAdvice
14 public class ExceptionTranslator {
15
16     /** The logger. */
17     EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExceptionTranslator.class);
18
19     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
20     @ResponseBody
21     public ExceptionResponse handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
22         logger.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodCallerName(), ExceptionUtils.getMessage(e), e);
23         Class<?> type = e.getRequiredType();
24         String message;
25         if (type.isEnum()) {
26             message = "The parameter " + e.getName() + " must have a value among : " + StringUtils.join(type.getEnumConstants(), ", ");
27         }
28         else {
29             message = "The parameter " + e.getName() + " must be of type " + type.getTypeName();
30         }
31         return new  ExceptionResponse(new ArgumentTypeMismatchException(message));
32     }
33
34     public static class ArgumentTypeMismatchException extends RuntimeException {
35         public ArgumentTypeMismatchException(String message) {
36             super(message);
37         }
38     }
39 }