12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- /*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.server.v2.common;
-
- import java.util.Optional;
- import java.util.stream.Collectors;
- import org.sonar.server.exceptions.BadRequestException;
- import org.sonar.server.exceptions.ForbiddenException;
- import org.sonar.server.exceptions.ServerException;
- import org.sonar.server.exceptions.UnauthorizedException;
- import org.sonar.server.v2.api.model.RestError;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.validation.BindException;
- import org.springframework.validation.FieldError;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
-
- @RestControllerAdvice
- public class RestResponseEntityExceptionHandler {
-
- @ExceptionHandler(IllegalStateException.class)
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- protected ResponseEntity<RestError> handleIllegalStateException(IllegalStateException illegalStateException) {
- return new ResponseEntity<>(new RestError(illegalStateException.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
- }
-
- @ExceptionHandler(BindException.class)
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- protected ResponseEntity<RestError> handleBindException(BindException bindException) {
- String validationErrors = bindException.getFieldErrors().stream()
- .map(RestResponseEntityExceptionHandler::handleFieldError)
- .collect(Collectors.joining());
- return new ResponseEntity<>(new RestError(validationErrors), HttpStatus.BAD_REQUEST);
- }
-
- private static String handleFieldError(FieldError fieldError) {
- String fieldName = fieldError.getField();
- String rejectedValueAsString = Optional.ofNullable(fieldError.getRejectedValue()).map(Object::toString).orElse("{}");
- String defaultMessage = fieldError.getDefaultMessage();
- return String.format("Value %s for field %s was rejected. Error: %s", rejectedValueAsString, fieldName, defaultMessage);
- }
-
- @ExceptionHandler({ServerException.class, ForbiddenException.class, UnauthorizedException.class, BadRequestException.class})
- protected ResponseEntity<RestError> handleServerException(ServerException serverException) {
- return new ResponseEntity<>(new RestError(serverException.getMessage()),
- Optional.ofNullable(HttpStatus.resolve(serverException.httpCode())).orElse(HttpStatus.INTERNAL_SERVER_ERROR));
- }
- }
|