3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.v2.common;
22 import java.util.Optional;
23 import java.util.stream.Collectors;
24 import org.sonar.server.exceptions.BadRequestException;
25 import org.sonar.server.exceptions.ForbiddenException;
26 import org.sonar.server.exceptions.NotFoundException;
27 import org.sonar.server.exceptions.ServerException;
28 import org.sonar.server.exceptions.UnauthorizedException;
29 import org.sonar.server.v2.api.model.RestError;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.validation.BindException;
33 import org.springframework.validation.FieldError;
34 import org.springframework.web.bind.annotation.ExceptionHandler;
35 import org.springframework.web.bind.annotation.ResponseStatus;
36 import org.springframework.web.bind.annotation.RestControllerAdvice;
39 public class RestResponseEntityExceptionHandler {
41 @ExceptionHandler({IllegalStateException.class})
42 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
43 protected ResponseEntity<RestError> handleIllegalStateException(IllegalStateException illegalStateException) {
44 return new ResponseEntity<>(new RestError(illegalStateException.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
47 @ExceptionHandler({IllegalArgumentException.class})
48 @ResponseStatus(HttpStatus.BAD_REQUEST)
49 protected ResponseEntity<RestError> handleIllegalArgumentException(IllegalArgumentException illegalArgumentException) {
50 return new ResponseEntity<>(new RestError(illegalArgumentException.getMessage()), HttpStatus.BAD_REQUEST);
53 @ExceptionHandler(BindException.class)
54 @ResponseStatus(HttpStatus.BAD_REQUEST)
55 protected ResponseEntity<RestError> handleBindException(BindException bindException) {
56 String validationErrors = bindException.getFieldErrors().stream()
57 .map(RestResponseEntityExceptionHandler::handleFieldError)
58 .collect(Collectors.joining());
59 return new ResponseEntity<>(new RestError(validationErrors), HttpStatus.BAD_REQUEST);
62 private static String handleFieldError(FieldError fieldError) {
63 String fieldName = fieldError.getField();
64 String rejectedValueAsString = Optional.ofNullable(fieldError.getRejectedValue()).map(Object::toString).orElse("{}");
65 String defaultMessage = fieldError.getDefaultMessage();
66 return String.format("Value %s for field %s was rejected. Error: %s.", rejectedValueAsString, fieldName, defaultMessage);
69 @ExceptionHandler({ServerException.class, ForbiddenException.class, UnauthorizedException.class, BadRequestException.class})
70 protected ResponseEntity<RestError> handleServerException(ServerException serverException) {
71 return new ResponseEntity<>(new RestError(serverException.getMessage()),
72 Optional.ofNullable(HttpStatus.resolve(serverException.httpCode())).orElse(HttpStatus.INTERNAL_SERVER_ERROR));
75 @ExceptionHandler({NotFoundException.class})
76 @ResponseStatus(HttpStatus.NOT_FOUND)
77 protected ResponseEntity<RestError> handleNotFoundException(NotFoundException notFoundException) {
78 return new ResponseEntity<>(new RestError(notFoundException.getMessage()), HttpStatus.NOT_FOUND);