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; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; import fr.triplea.demovote.dto.JsonErrorResponse; import fr.triplea.demovote.security.jwt.RefreshTokenException; @RestControllerAdvice public class GlobalExceptionHandler { //@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 handleTokenRefreshException(RefreshTokenException ex) { LOG.error(ex.getMessage()); JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.FORBIDDEN.value(), ex.getMessage()); return new ResponseEntity<>(jer, HttpStatus.FORBIDDEN); } @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ResponseEntity handleAllExceptions(Exception ex) { 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); } @ExceptionHandler(NullPointerException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseEntity handleNullPointerException(NullPointerException ex) { LOG.error(ex.getMessage()); JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.BAD_REQUEST.value(), ex.getMessage()); return new ResponseEntity<>(jer, HttpStatus.BAD_REQUEST); } @ExceptionHandler(ResourceNotFoundException.class) public ResponseEntity handleNotFound(ResourceNotFoundException ex) { LOG.error(ex.getMessage()); JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.NOT_FOUND.value(), ex.getMessage()); return new ResponseEntity<>(jer, HttpStatus.NOT_FOUND); } }