1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright © 2018 European Support Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.sdc.workflow.api;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
import java.util.function.Function;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.text.RandomStringGenerator;
import org.onap.sdc.workflow.api.types.ErrorResponse;
import org.onap.sdc.workflow.api.types.UnexpectedErrorResponse;
import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException;
import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException;
import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
import org.onap.sdc.workflow.services.exceptions.VersionCreationException;
import org.onap.sdc.workflow.services.exceptions.VersionModificationException;
import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException;
import org.onap.sdc.workflow.services.exceptions.VersionStateModificationMissingArtifactException;
import org.onap.sdc.workflow.services.exceptions.VersionStatusModificationException;
import org.onap.sdc.workflow.services.exceptions.WorkflowModificationException;
import org.onap.sdc.workflow.services.exceptions.WorkflowStatusModificationException;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
@RestController
public class ExceptionsHandler extends ResponseEntityExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(ExceptionsHandler.class);
private static final String LOG_MSG = "Exception was mapped to {} response";
private static final String UNEXPECTED_ERROR_MSG = "Something bad happened. Please contact support with code %s";
private static final RandomStringGenerator CODE_GENERATOR =
new RandomStringGenerator.Builder().withinRange('A', 'Z').build();
private static final int CODE_LENGTH = 8;
private static final Function<Exception, UnexpectedErrorResponse> UNEXPECTED_EXCEPTION_MAPPER =
isDevInfoDisabled()
? e -> new UnexpectedErrorResponse(getUnexpectedErrorMessage())
: e -> new UnexpectedErrorResponse(getUnexpectedErrorMessage(), ExceptionUtils.getStackTrace(e));
@Override
public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
LOG.debug(LOG_MSG, BAD_REQUEST, ex);
// Convert Spring-generated binding exceptions into the format of an application message
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), BAD_REQUEST);
}
@Override
protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException exception,
final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
LOG.debug(LOG_MSG, BAD_REQUEST, exception);
String errorMsg = exception.getBindingResult().getFieldErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage).findFirst()
.orElse(exception.getMessage());
return new ResponseEntity<>(new ErrorResponse(errorMsg), BAD_REQUEST);
}
@ExceptionHandler(EntityNotFoundException.class)
public final ResponseEntity<ErrorResponse> handleNotFoundException(Exception exception) {
LOG.debug(LOG_MSG, NOT_FOUND, exception);
return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), NOT_FOUND);
}
@ExceptionHandler(
{InvalidArtifactException.class, VersionCreationException.class, VersionModificationException.class,
VersionStateModificationException.class,
VersionStateModificationMissingArtifactException.class,
VersionStatusModificationException.class,
UniqueValueViolationException.class, WorkflowStatusModificationException.class,
WorkflowModificationException.class})
public final ResponseEntity<ErrorResponse> handleUnprocessableEntityException(Exception exception) {
LOG.debug(LOG_MSG, UNPROCESSABLE_ENTITY, exception);
return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), UNPROCESSABLE_ENTITY);
}
@ExceptionHandler(Exception.class)
public final ResponseEntity<UnexpectedErrorResponse> handleUnexpectedException(Exception exception) {
UnexpectedErrorResponse response = UNEXPECTED_EXCEPTION_MAPPER.apply(exception);
LOG.error(response.getMessage(), exception);
return new ResponseEntity<>(response, INTERNAL_SERVER_ERROR);
}
private static boolean isDevInfoDisabled() {
return Boolean.FALSE.toString().equalsIgnoreCase(System.getProperty("errors.includeDevInfo"));
}
private static String getUnexpectedErrorMessage() {
return String.format(UNEXPECTED_ERROR_MSG, CODE_GENERATOR.generate(CODE_LENGTH));
}
}
|