Replace vfmodule: expect optional retainVolumeGroups param
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / ExceptionTranslator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.model;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.lang3.exception.ExceptionUtils;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.springframework.web.bind.annotation.ControllerAdvice;
27 import org.springframework.web.bind.annotation.ExceptionHandler;
28 import org.springframework.web.bind.annotation.ResponseBody;
29 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
30
31 import static org.onap.vid.utils.Logging.getMethodCallerName;
32
33 @ControllerAdvice
34 public class ExceptionTranslator {
35
36     /** The logger. */
37     EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExceptionTranslator.class);
38
39     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
40     @ResponseBody
41     public ExceptionResponse handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
42         logger.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodCallerName(), ExceptionUtils.getMessage(e), e);
43         Class<?> type = e.getRequiredType();
44         String message;
45         if (type.isEnum()) {
46             message = "The parameter " + e.getName() + " must have a value among : " + StringUtils.join(type.getEnumConstants(), ", ");
47         }
48         else {
49             message = "The parameter " + e.getName() + " must be of type " + type.getTypeName();
50         }
51         return new  ExceptionResponse(new ArgumentTypeMismatchException(message));
52     }
53
54     public static class ArgumentTypeMismatchException extends RuntimeException {
55         public ArgumentTypeMismatchException(String message) {
56             super(message);
57         }
58     }
59 }