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.ServerException;
27 import org.sonar.server.exceptions.UnauthorizedException;
28 import org.sonar.server.v2.api.model.RestError;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.validation.BindException;
32 import org.springframework.validation.FieldError;
33 import org.springframework.web.bind.annotation.ExceptionHandler;
34 import org.springframework.web.bind.annotation.ResponseStatus;
35 import org.springframework.web.bind.annotation.RestControllerAdvice;
38 public class RestResponseEntityExceptionHandler {
40 @ExceptionHandler(IllegalStateException.class)
41 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
42 protected ResponseEntity<RestError> handleIllegalStateException(IllegalStateException illegalStateException) {
43 return new ResponseEntity<>(new RestError(illegalStateException.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
46 @ExceptionHandler(BindException.class)
47 @ResponseStatus(HttpStatus.BAD_REQUEST)
48 protected ResponseEntity<RestError> handleBindException(BindException bindException) {
49 String validationErrors = bindException.getFieldErrors().stream()
50 .map(RestResponseEntityExceptionHandler::handleFieldError)
51 .collect(Collectors.joining());
52 return new ResponseEntity<>(new RestError(validationErrors), HttpStatus.BAD_REQUEST);
55 private static String handleFieldError(FieldError fieldError) {
56 String fieldName = fieldError.getField();
57 String rejectedValueAsString = Optional.ofNullable(fieldError.getRejectedValue()).map(Object::toString).orElse("{}");
58 String defaultMessage = fieldError.getDefaultMessage();
59 return String.format("Value %s for field %s was rejected. Error: %s", rejectedValueAsString, fieldName, defaultMessage);
62 @ExceptionHandler({ServerException.class, ForbiddenException.class, UnauthorizedException.class, BadRequestException.class})
63 protected ResponseEntity<RestError> handleServerException(ServerException serverException) {
64 return new ResponseEntity<>(new RestError(serverException.getMessage()),
65 Optional.ofNullable(HttpStatus.resolve(serverException.httpCode())).orElse(HttpStatus.INTERNAL_SERVER_ERROR));