|
|
@@ -1,5 +1,8 @@
|
|
|
package fr.triplea.demovote.security;
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
@@ -12,13 +15,20 @@ import fr.triplea.demovote.security.jwt.RefreshTokenException;
|
|
|
@RestControllerAdvice
|
|
|
public class GlobalExceptionHandler
|
|
|
{
|
|
|
-
|
|
|
- // TODO : en mode production, masquer les requêtes SQL (ne pas donner d'indices sur le schema)
|
|
|
|
|
|
+ //@SuppressWarnings("unused")
|
|
|
+ private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
+
|
|
|
+ @Value("${production.mode}")
|
|
|
+ private boolean modeProduction;
|
|
|
+
|
|
|
+
|
|
|
@ExceptionHandler(value = RefreshTokenException.class)
|
|
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
|
|
public ResponseEntity<JsonErrorResponse> handleTokenRefreshException(RefreshTokenException ex)
|
|
|
{
|
|
|
+ LOG.error(ex.getMessage());
|
|
|
+
|
|
|
JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.FORBIDDEN.value(), ex.getMessage());
|
|
|
|
|
|
return new ResponseEntity<>(jer, HttpStatus.FORBIDDEN);
|
|
|
@@ -28,7 +38,13 @@ public class GlobalExceptionHandler
|
|
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
public ResponseEntity<JsonErrorResponse> handleAllExceptions(Exception ex)
|
|
|
{
|
|
|
- JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
|
|
|
+ LOG.error(ex.getMessage());
|
|
|
+
|
|
|
+ String message = ex.getMessage();
|
|
|
+
|
|
|
+ if (modeProduction) { if (message.contains("JDBC") || message.contains("SQL")) { message = "JDBC or SQL error, please contact the administrator to look in logs"; } }
|
|
|
+
|
|
|
+ JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
|
|
|
|
|
|
return new ResponseEntity<>(jer, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
}
|
|
|
@@ -37,6 +53,8 @@ public class GlobalExceptionHandler
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
public ResponseEntity<JsonErrorResponse> handleNullPointerException(NullPointerException ex)
|
|
|
{
|
|
|
+ LOG.error(ex.getMessage());
|
|
|
+
|
|
|
JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
|
|
|
|
|
|
return new ResponseEntity<>(jer, HttpStatus.BAD_REQUEST);
|
|
|
@@ -45,6 +63,8 @@ public class GlobalExceptionHandler
|
|
|
@ExceptionHandler(ResourceNotFoundException.class)
|
|
|
public ResponseEntity<JsonErrorResponse> handleNotFound(ResourceNotFoundException ex)
|
|
|
{
|
|
|
+ LOG.error(ex.getMessage());
|
|
|
+
|
|
|
JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.NOT_FOUND.value(), ex.getMessage());
|
|
|
|
|
|
return new ResponseEntity<>(jer, HttpStatus.NOT_FOUND);
|