aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-webapi-v2
diff options
context:
space:
mode:
authorSteve Marion <steve.marion@sonarsource.com>2024-12-19 11:46:07 +0100
committersonartech <sonartech@sonarsource.com>2024-12-19 20:03:04 +0000
commit8e787f57cfc0883790d651e7587e02021e576438 (patch)
treeb0101976df44165818eb824b345722c6328b1fcf /server/sonar-webserver-webapi-v2
parent8fe712960e07591f6cc2f8aa2e164a6eb9ad67fa (diff)
downloadsonarqube-8e787f57cfc0883790d651e7587e02021e576438.tar.gz
sonarqube-8e787f57cfc0883790d651e7587e02021e576438.zip
SONAR-24044 Lower logging in Exception Handler methods.
Diffstat (limited to 'server/sonar-webserver-webapi-v2')
-rw-r--r--server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandler.java54
-rw-r--r--server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandlerTest.java32
2 files changed, 43 insertions, 43 deletions
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandler.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandler.java
index 6340bb8e46b..a045d70365f 100644
--- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandler.java
+++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandler.java
@@ -56,61 +56,65 @@ public class RestResponseEntityExceptionHandler {
@ExceptionHandler(HttpMessageNotReadableException.class)
protected ResponseEntity<RestError> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
- LOGGER.error(ErrorMessages.INVALID_REQUEST_FORMAT.getMessage(), ex);
+ LOGGER.warn(ErrorMessages.INVALID_REQUEST_FORMAT.getMessage(), ex);
return buildResponse(HttpStatus.BAD_REQUEST, ErrorMessages.INVALID_REQUEST_FORMAT);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<RestError> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
- LOGGER.error(ErrorMessages.VALIDATION_ERROR.getMessage(), ex);
String validationErrors = ex.getFieldErrors().stream()
.map(RestResponseEntityExceptionHandler::handleFieldError)
.collect(Collectors.joining());
+ LOGGER.atInfo()
+ .setMessage(ErrorMessages.VALIDATION_ERROR.getMessage() + "\n" + validationErrors)
+ .log();
return buildResponse(HttpStatus.BAD_REQUEST, validationErrors);
}
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
protected ResponseEntity<RestError> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
- LOGGER.error(ErrorMessages.INVALID_PARAMETER_TYPE.getMessage(), ex);
+ LOGGER.warn(ErrorMessages.INVALID_PARAMETER_TYPE.getMessage(), ex);
return buildResponse(HttpStatus.BAD_REQUEST, ErrorMessages.INVALID_PARAMETER_TYPE);
}
@ExceptionHandler(ConversionFailedException.class)
protected ResponseEntity<RestError> handleConversionFailedException(ConversionFailedException ex) {
- LOGGER.error(ErrorMessages.CONVERSION_FAILED.getMessage(), ex);
+ LOGGER.info(ErrorMessages.CONVERSION_FAILED.getMessage(), ex);
return buildResponse(HttpStatus.BAD_REQUEST, ErrorMessages.CONVERSION_FAILED);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<RestError> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
- LOGGER.error(ErrorMessages.METHOD_NOT_SUPPORTED.getMessage(), ex);
+ LOGGER.warn(ErrorMessages.METHOD_NOT_SUPPORTED.getMessage(), ex);
return buildResponse(HttpStatus.METHOD_NOT_ALLOWED, ErrorMessages.METHOD_NOT_SUPPORTED);
}
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
protected ResponseEntity<RestError> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException ex) {
- LOGGER.error(ErrorMessages.UNSUPPORTED_MEDIA_TYPE.getMessage(), ex);
+ LOGGER.warn(ErrorMessages.UNSUPPORTED_MEDIA_TYPE.getMessage(), ex);
return buildResponse(HttpStatus.UNSUPPORTED_MEDIA_TYPE, ErrorMessages.UNSUPPORTED_MEDIA_TYPE);
}
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
protected ResponseEntity<RestError> handleHttpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException ex) {
- LOGGER.error(ErrorMessages.UNACCEPTABLE_MEDIA_TYPE.getMessage(), ex);
+ LOGGER.warn(ErrorMessages.UNACCEPTABLE_MEDIA_TYPE.getMessage(), ex);
return buildResponse(HttpStatus.NOT_ACCEPTABLE, ErrorMessages.UNACCEPTABLE_MEDIA_TYPE);
}
@ExceptionHandler(IllegalArgumentException.class)
protected ResponseEntity<RestError> handleIllegalArgumentException(IllegalArgumentException ex) {
- LOGGER.error(ErrorMessages.INVALID_INPUT_PROVIDED.getMessage(), ex);
+ LOGGER.warn(ErrorMessages.INVALID_INPUT_PROVIDED.getMessage(), ex);
return buildResponse(HttpStatus.BAD_REQUEST, ex.getMessage());
}
@ExceptionHandler(BindException.class)
protected ResponseEntity<RestError> handleBindException(BindException ex) {
- LOGGER.error(ErrorMessages.BIND_ERROR.getMessage(), ex);
String validationErrors = ex.getFieldErrors().stream()
.map(RestResponseEntityExceptionHandler::handleFieldError)
.collect(Collectors.joining());
+ LOGGER.atInfo()
+ .setMessage(ErrorMessages.BIND_ERROR.getMessage() + "\n" + validationErrors)
+ .log();
return buildResponse(HttpStatus.BAD_REQUEST, validationErrors);
}
@@ -119,10 +123,16 @@ public class RestResponseEntityExceptionHandler {
ServletRequestBindingException.class,
})
protected ResponseEntity<RestError> handleBadRequests(Exception ex) {
- LOGGER.error(ErrorMessages.BAD_REQUEST.getMessage(), ex);
+ LOGGER.warn(ErrorMessages.BAD_REQUEST.getMessage(), ex);
return buildResponse(HttpStatus.BAD_REQUEST, ErrorMessages.BAD_REQUEST);
}
+ @ExceptionHandler(MaxUploadSizeExceededException.class)
+ protected ResponseEntity<RestError> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex) {
+ LOGGER.warn(ErrorMessages.SIZE_EXCEEDED.getMessage(), ex);
+ return buildResponse(HttpStatus.PAYLOAD_TOO_LARGE, ErrorMessages.SIZE_EXCEEDED);
+ }
+
// endregion client
// region security
@@ -133,6 +143,12 @@ public class RestResponseEntityExceptionHandler {
return buildResponse(HttpStatus.FORBIDDEN, ErrorMessages.ACCESS_DENIED);
}
+ @ExceptionHandler(AuthenticationException.class)
+ protected ResponseEntity<RestError> handleAuthenticationException(AuthenticationException ex) {
+ LOGGER.warn(ex.getPublicMessage());
+ return buildResponse(HttpStatus.UNAUTHORIZED, ErrorMessages.AUTHENTICATION_FAILED);
+ }
+
// endregion security
// region server
@@ -158,34 +174,21 @@ public class RestResponseEntityExceptionHandler {
return buildResponse(HttpStatus.SERVICE_UNAVAILABLE, ErrorMessages.REQUEST_TIMEOUT);
}
- @ExceptionHandler(MaxUploadSizeExceededException.class)
- protected ResponseEntity<RestError> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex) {
- LOGGER.error(ErrorMessages.SIZE_EXCEEDED.getMessage(), ex);
- return buildResponse(HttpStatus.PAYLOAD_TOO_LARGE, ErrorMessages.SIZE_EXCEEDED);
- }
-
- @ExceptionHandler(AuthenticationException.class)
- protected ResponseEntity<RestError> handleAuthenticationException(AuthenticationException ex) {
- LOGGER.error(ErrorMessages.AUTHENTICATION_FAILED.getMessage(), ex);
- return buildResponse(HttpStatus.UNAUTHORIZED, ErrorMessages.AUTHENTICATION_FAILED);
- }
-
@ExceptionHandler(ServerException.class)
protected ResponseEntity<RestError> handleServerException(ServerException ex) {
final HttpStatus httpStatus = Optional.ofNullable(HttpStatus.resolve(ex.httpCode())).orElse(HttpStatus.INTERNAL_SERVER_ERROR);
final String errorMessage = Optional.ofNullable(ex.getMessage()).orElse(ErrorMessages.INTERNAL_SERVER_ERROR.getMessage());
- LOGGER.error(errorMessage, ex);
return buildResponse(httpStatus, errorMessage);
}
- // endregion server
-
@ExceptionHandler({Exception.class})
protected ResponseEntity<RestError> handleUnhandledException(Exception ex) {
LOGGER.error(ErrorMessages.UNEXPECTED_ERROR.getMessage(), ex);
return buildResponse(HttpStatus.INTERNAL_SERVER_ERROR, ErrorMessages.UNEXPECTED_ERROR);
}
+ // endregion server
+
private static ResponseEntity<RestError> buildResponse(HttpStatus httpStatus, ErrorMessages errorMessages) {
return buildResponse(httpStatus, errorMessages.getMessage());
}
@@ -200,5 +203,4 @@ public class RestResponseEntityExceptionHandler {
String defaultMessage = fieldError.getDefaultMessage();
return String.format("Value %s for field %s was rejected. Error: %s.", rejectedValueAsString, fieldName, defaultMessage);
}
-
}
diff --git a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandlerTest.java b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandlerTest.java
index 23a0b946670..b8ab5be891f 100644
--- a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandlerTest.java
+++ b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/common/RestResponseEntityExceptionHandlerTest.java
@@ -85,7 +85,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.INVALID_REQUEST_FORMAT.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.INVALID_REQUEST_FORMAT.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.INVALID_REQUEST_FORMAT.getMessage());
}
@Test
@@ -95,16 +95,17 @@ class RestResponseEntityExceptionHandlerTest {
when(fieldError.getDefaultMessage()).thenReturn("<defaultMessage>");
when(fieldError.getField()).thenReturn("<field>");
+ when(fieldError.getRejectedValue()).thenReturn("<rejectedValue>");
when(ex.getFieldErrors()).thenReturn(List.of(fieldError));
ResponseEntity<RestError> response = underTest.handleMethodArgumentNotValidException(ex);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(response.getBody()).isNotNull();
- assertThat(response.getBody().message()).isEqualTo("Value {} for field <field> was rejected. Error: <defaultMessage>." /* ErrorMessages.VALIDATION_ERROR.getMessage() */);
+ assertThat(response.getBody().message()).isEqualTo("Value <rejectedValue> for field <field> was rejected. Error: <defaultMessage>." /* ErrorMessages.VALIDATION_ERROR.getMessage() */);
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.VALIDATION_ERROR.getMessage());
+ assertThat(logs.logs(Level.INFO)).anyMatch(log -> log.startsWith(ErrorMessages.VALIDATION_ERROR.getMessage()));
}
@Test
@@ -121,7 +122,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.INVALID_PARAMETER_TYPE.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.INVALID_PARAMETER_TYPE.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.INVALID_PARAMETER_TYPE.getMessage());
}
@Test
@@ -135,7 +136,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.CONVERSION_FAILED.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.CONVERSION_FAILED.getMessage());
+ assertThat(logs.logs(Level.INFO)).contains(ErrorMessages.CONVERSION_FAILED.getMessage());
}
@Test
@@ -149,7 +150,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.METHOD_NOT_SUPPORTED.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.METHOD_NOT_SUPPORTED.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.METHOD_NOT_SUPPORTED.getMessage());
}
@Test
@@ -163,7 +164,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.UNSUPPORTED_MEDIA_TYPE.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.UNSUPPORTED_MEDIA_TYPE.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.UNSUPPORTED_MEDIA_TYPE.getMessage());
}
@Test
@@ -177,7 +178,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.UNACCEPTABLE_MEDIA_TYPE.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.UNACCEPTABLE_MEDIA_TYPE.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.UNACCEPTABLE_MEDIA_TYPE.getMessage());
}
@Test
@@ -191,7 +192,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ex.getMessage() /* ErrorMessages.INVALID_INPUT_PROVIDED.getMessage() */);
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.INVALID_INPUT_PROVIDED.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.INVALID_INPUT_PROVIDED.getMessage());
}
@Test
@@ -206,7 +207,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).contains("Value {} for field field was rejected. Error: Field error.");
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.BIND_ERROR.getMessage());
+ assertThat(logs.logs(Level.INFO)).anyMatch(log -> log.startsWith(ErrorMessages.BIND_ERROR.getMessage()));
}
@ParameterizedTest
@@ -219,7 +220,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.BAD_REQUEST.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.BAD_REQUEST.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.BAD_REQUEST.getMessage());
}
static Stream<Arguments> badRequestsProvider() {
@@ -250,7 +251,7 @@ class RestResponseEntityExceptionHandlerTest {
var ex = AuthenticationException.newBuilder()
.setSource(AuthenticationEvent.Source.sso())
.setLogin("mockLogin")
- .setPublicMessage("Authentication failed")
+ .setPublicMessage("Authentication failed.")
.build();
ResponseEntity<RestError> response = underTest.handleAuthenticationException(ex);
@@ -259,7 +260,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.AUTHENTICATION_FAILED.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.AUTHENTICATION_FAILED.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.AUTHENTICATION_FAILED.getMessage());
}
// endregion security
@@ -321,7 +322,7 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getBody().message()).isEqualTo(ErrorMessages.SIZE_EXCEEDED.getMessage());
// Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ErrorMessages.SIZE_EXCEEDED.getMessage());
+ assertThat(logs.logs(Level.WARN)).contains(ErrorMessages.SIZE_EXCEEDED.getMessage());
}
@ParameterizedTest
@@ -332,9 +333,6 @@ class RestResponseEntityExceptionHandlerTest {
assertThat(response.getStatusCode()).isEqualTo(expectedStatus);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().message()).isEqualTo(ex.getMessage());
-
- // Verify logging
- assertThat(logs.logs(Level.ERROR)).contains(ex.getMessage());
}
static Stream<Arguments> serverExceptionsProvider() {