blob: 7ba99590123067dca6ae7c7ebedd4249973786ea [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
22
23package org.onap.osam.model;
24
25import org.apache.commons.lang.StringUtils;
26import org.apache.commons.lang3.exception.ExceptionUtils;
27import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
28import org.springframework.web.bind.annotation.ControllerAdvice;
29import org.springframework.web.bind.annotation.ExceptionHandler;
30import org.springframework.web.bind.annotation.ResponseBody;
31import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
32
33import static org.onap.osam.utils.Logging.getMethodCallerName;
34
35@ControllerAdvice
36public class ExceptionTranslator {
37
38 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExceptionTranslator.class);
39
40 @ExceptionHandler(MethodArgumentTypeMismatchException.class)
41 @ResponseBody
42 public ExceptionResponse handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
43 logger.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodCallerName(), ExceptionUtils.getMessage(e), e);
44 Class<?> type = e.getRequiredType();
45 String message;
46 if (type.isEnum()) {
47 message = "The parameter " + e.getName() + " must have a value among : " + StringUtils.join(type.getEnumConstants(), ", ");
48 }
49 else {
50 message = "The parameter " + e.getName() + " must be of type " + type.getTypeName();
51 }
52 return new ExceptionResponse(new ArgumentTypeMismatchException(message));
53 }
54
55 public static class ArgumentTypeMismatchException extends RuntimeException {
56 public ArgumentTypeMismatchException(String message) {
57 super(message);
58 }
59 }
60}