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; 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.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import fr.univnantes.webandcloud.api.responses.BadRequest;
import fr.univnantes.webandcloud.api.responses.ResponseError; import fr.univnantes.webandcloud.api.responses.ResponseError;
/** /**
...@@ -19,20 +19,20 @@ import fr.univnantes.webandcloud.api.responses.ResponseError; ...@@ -19,20 +19,20 @@ import fr.univnantes.webandcloud.api.responses.ResponseError;
@RestController @RestController
public class OwnErrorController implements ErrorController { public class OwnErrorController implements ErrorController {
/** private Logger logger = LoggerFactory.getLogger(OwnErrorController.class);
* Handler of Exception Throw
* public ResponseEntity<ResponseError> makeResponse(@NonNull ResponseError responseError) {
* @param exception Exception throw return new ResponseEntity<>(responseError, responseError.status);
* @return error }
*/
@ExceptionHandler(Exception.class) @ExceptionHandler(ResponseError.class)
public ResponseEntity<ResponseError> handleError(@NonNull Exception exception) { public ResponseEntity<ResponseError> handleError(@NonNull ResponseError exception) {
if (exception instanceof ResponseError) return makeResponse(exception);
return new ResponseEntity<>((ResponseError) exception, ((ResponseError) exception).status); }
exception.printStackTrace();
if (exception instanceof HttpMessageConversionException) @ExceptionHandler(RuntimeException.class)
return new ResponseEntity<>(new BadRequest("Wrong format provided"), HttpStatus.BAD_REQUEST); public ResponseEntity<ResponseError> handleError(@NonNull RuntimeException exception) {
return new ResponseEntity<>(new ResponseError("Internal Error", HttpStatus.INTERNAL_SERVER_ERROR), logger.error(exception.getMessage());
HttpStatus.INTERNAL_SERVER_ERROR); return makeResponse(new ResponseError("Internal Error occured", HttpStatus.INTERNAL_SERVER_ERROR));
} }
} }
...@@ -7,7 +7,7 @@ import org.springframework.http.HttpStatus; ...@@ -7,7 +7,7 @@ import org.springframework.http.HttpStatus;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "Response of Error") @Schema(description = "Response of Error")
@JsonIgnoreProperties({ "cause", "stackTrace", "localizedMessage", "suppressed" }) @JsonIgnoreProperties({ "cause", "stackTrace", "localizedMessage", "suppressed", "message" })
public class ResponseError extends Exception { public class ResponseError extends Exception {
public final String reason; public final String reason;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter