]> source.dussan.org Git - sonarqube.git/blob
13cacd3eefe7b2db9a359d3af154afea3a98cb31
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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
22 import com.fasterxml.jackson.databind.exc.InvalidFormatException;
23 import java.util.Optional;
24 import java.util.stream.Collectors;
25 import org.sonar.server.exceptions.BadRequestException;
26 import org.sonar.server.exceptions.ForbiddenException;
27 import org.sonar.server.exceptions.NotFoundException;
28 import org.sonar.server.exceptions.ServerException;
29 import org.sonar.server.exceptions.UnauthorizedException;
30 import org.sonar.server.v2.api.model.RestError;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.http.converter.HttpMessageNotReadableException;
34 import org.springframework.validation.BindException;
35 import org.springframework.validation.FieldError;
36 import org.springframework.web.bind.annotation.ExceptionHandler;
37 import org.springframework.web.bind.annotation.ResponseStatus;
38 import org.springframework.web.bind.annotation.RestControllerAdvice;
39
40 @RestControllerAdvice
41 public class RestResponseEntityExceptionHandler {
42
43   @ExceptionHandler({IllegalStateException.class})
44   @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
45   protected ResponseEntity<RestError> handleIllegalStateException(IllegalStateException illegalStateException) {
46     return new ResponseEntity<>(new RestError(illegalStateException.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
47   }
48
49   @ExceptionHandler({IllegalArgumentException.class})
50   @ResponseStatus(HttpStatus.BAD_REQUEST)
51   protected ResponseEntity<RestError> handleIllegalArgumentException(IllegalArgumentException illegalArgumentException) {
52     return new ResponseEntity<>(new RestError(illegalArgumentException.getMessage()), HttpStatus.BAD_REQUEST);
53   }
54
55   @ExceptionHandler(BindException.class)
56   @ResponseStatus(HttpStatus.BAD_REQUEST)
57   protected ResponseEntity<RestError> handleBindException(BindException bindException) {
58     String validationErrors = bindException.getFieldErrors().stream()
59       .map(RestResponseEntityExceptionHandler::handleFieldError)
60       .collect(Collectors.joining());
61     return new ResponseEntity<>(new RestError(validationErrors), HttpStatus.BAD_REQUEST);
62   }
63
64   private static String handleFieldError(FieldError fieldError) {
65     String fieldName = fieldError.getField();
66     String rejectedValueAsString = Optional.ofNullable(fieldError.getRejectedValue()).map(Object::toString).orElse("{}");
67     String defaultMessage = fieldError.getDefaultMessage();
68     return String.format("Value %s for field %s was rejected. Error: %s.", rejectedValueAsString, fieldName, defaultMessage);
69   }
70
71   @ExceptionHandler({ServerException.class, ForbiddenException.class, UnauthorizedException.class, BadRequestException.class})
72   protected ResponseEntity<RestError> handleServerException(ServerException serverException) {
73     return new ResponseEntity<>(new RestError(serverException.getMessage()),
74       Optional.ofNullable(HttpStatus.resolve(serverException.httpCode())).orElse(HttpStatus.INTERNAL_SERVER_ERROR));
75   }
76
77   @ExceptionHandler({NotFoundException.class})
78   @ResponseStatus(HttpStatus.NOT_FOUND)
79   protected ResponseEntity<RestError> handleNotFoundException(NotFoundException notFoundException) {
80     return new ResponseEntity<>(new RestError(notFoundException.getMessage()), HttpStatus.NOT_FOUND);
81   }
82
83   @ExceptionHandler({HttpMessageNotReadableException.class})
84   @ResponseStatus(HttpStatus.BAD_REQUEST)
85   protected ResponseEntity<RestError> handleHttpMessageNotReadableException(HttpMessageNotReadableException httpMessageNotReadableException) {
86     String exceptionMessage = getExceptionMessage(httpMessageNotReadableException);
87     return new ResponseEntity<>(new RestError(exceptionMessage), HttpStatus.BAD_REQUEST);
88   }
89
90   private static String getExceptionMessage(HttpMessageNotReadableException httpMessageNotReadableException) {
91     if (httpMessageNotReadableException.getRootCause() instanceof InvalidFormatException invalidFormatException) {
92       return invalidFormatException.getOriginalMessage();
93     }
94     return Optional.ofNullable(httpMessageNotReadableException.getMessage()).orElse("");
95   }
96
97 }