Nantes Université

Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider ebfbd6e9 rédigé par Féry Mathieu (Mathius)'s avatar Féry Mathieu (Mathius)
Parcourir les fichiers

feat(error): Update OwnErrorController

parent a7838dba
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #34286 réussi
package fr.univnantes.webandcloud.api.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import fr.univnantes.webandcloud.api.responses.BadRequest;
import fr.univnantes.webandcloud.api.responses.ResponseError;
/**
......@@ -19,20 +19,20 @@ import fr.univnantes.webandcloud.api.responses.ResponseError;
@RestController
public class OwnErrorController implements ErrorController {
/**
* Handler of Exception Throw
*
* @param exception Exception throw
* @return error
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ResponseError> handleError(@NonNull Exception exception) {
if (exception instanceof ResponseError)
return new ResponseEntity<>((ResponseError) exception, ((ResponseError) exception).status);
exception.printStackTrace();
if (exception instanceof HttpMessageConversionException)
return new ResponseEntity<>(new BadRequest("Wrong format provided"), HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(new ResponseError("Internal Error", HttpStatus.INTERNAL_SERVER_ERROR),
HttpStatus.INTERNAL_SERVER_ERROR);
private Logger logger = LoggerFactory.getLogger(OwnErrorController.class);
public ResponseEntity<ResponseError> makeResponse(@NonNull ResponseError responseError) {
return new ResponseEntity<>(responseError, responseError.status);
}
@ExceptionHandler(ResponseError.class)
public ResponseEntity<ResponseError> handleError(@NonNull ResponseError exception) {
return makeResponse(exception);
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ResponseError> handleError(@NonNull RuntimeException exception) {
logger.error(exception.getMessage());
return makeResponse(new ResponseError("Internal Error occured", HttpStatus.INTERNAL_SERVER_ERROR));
}
}
......@@ -7,7 +7,7 @@ import org.springframework.http.HttpStatus;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "Response of Error")
@JsonIgnoreProperties({ "cause", "stackTrace", "localizedMessage", "suppressed" })
@JsonIgnoreProperties({ "cause", "stackTrace", "localizedMessage", "suppressed", "message" })
public class ResponseError extends Exception {
public final String reason;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter