You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RestResponseEntityExceptionHandler.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.v2.common;
  21. import java.util.Optional;
  22. import java.util.stream.Collectors;
  23. import org.sonar.server.exceptions.BadRequestException;
  24. import org.sonar.server.exceptions.ForbiddenException;
  25. import org.sonar.server.exceptions.ServerException;
  26. import org.sonar.server.exceptions.UnauthorizedException;
  27. import org.sonar.server.v2.api.model.RestError;
  28. import org.springframework.http.HttpStatus;
  29. import org.springframework.http.ResponseEntity;
  30. import org.springframework.validation.BindException;
  31. import org.springframework.validation.FieldError;
  32. import org.springframework.web.bind.annotation.ExceptionHandler;
  33. import org.springframework.web.bind.annotation.ResponseStatus;
  34. import org.springframework.web.bind.annotation.RestControllerAdvice;
  35. @RestControllerAdvice
  36. public class RestResponseEntityExceptionHandler {
  37. @ExceptionHandler(IllegalStateException.class)
  38. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  39. protected ResponseEntity<RestError> handleIllegalStateException(IllegalStateException illegalStateException) {
  40. return new ResponseEntity<>(new RestError(illegalStateException.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
  41. }
  42. @ExceptionHandler(BindException.class)
  43. @ResponseStatus(HttpStatus.BAD_REQUEST)
  44. protected ResponseEntity<RestError> handleBindException(BindException bindException) {
  45. String validationErrors = bindException.getFieldErrors().stream()
  46. .map(RestResponseEntityExceptionHandler::handleFieldError)
  47. .collect(Collectors.joining());
  48. return new ResponseEntity<>(new RestError(validationErrors), HttpStatus.BAD_REQUEST);
  49. }
  50. private static String handleFieldError(FieldError fieldError) {
  51. String fieldName = fieldError.getField();
  52. String rejectedValueAsString = Optional.ofNullable(fieldError.getRejectedValue()).map(Object::toString).orElse("{}");
  53. String defaultMessage = fieldError.getDefaultMessage();
  54. return String.format("Value %s for field %s was rejected. Error: %s", rejectedValueAsString, fieldName, defaultMessage);
  55. }
  56. @ExceptionHandler({ServerException.class, ForbiddenException.class, UnauthorizedException.class, BadRequestException.class})
  57. protected ResponseEntity<RestError> handleServerException(ServerException serverException) {
  58. return new ResponseEntity<>(new RestError(serverException.getMessage()),
  59. Optional.ofNullable(HttpStatus.resolve(serverException.httpCode())).orElse(HttpStatus.INTERNAL_SERVER_ERROR));
  60. }
  61. }