Reworking the exception handling.

This commit is contained in:
Florian THIERRY
2024-03-14 09:31:14 +01:00
parent a872a9fe33
commit 5c5304ff98
2 changed files with 40 additions and 70 deletions

View File

@@ -2,7 +2,6 @@ package org.codiki.exposition.configuration;
import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.FORBIDDEN; import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND; import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNAUTHORIZED; import static org.springframework.http.HttpStatus.UNAUTHORIZED;
import org.codiki.domain.category.exception.CategoryDeletionException; import org.codiki.domain.category.exception.CategoryDeletionException;
@@ -17,82 +16,52 @@ import org.codiki.domain.picture.exception.PictureUploadException;
import org.codiki.domain.publication.exception.PublicationEditionException; import org.codiki.domain.publication.exception.PublicationEditionException;
import org.codiki.domain.publication.exception.PublicationNotFoundException; import org.codiki.domain.publication.exception.PublicationNotFoundException;
import org.codiki.domain.publication.exception.PublicationUpdateForbiddenException; import org.codiki.domain.publication.exception.PublicationUpdateForbiddenException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice @RestControllerAdvice
public class GlobalControllerExceptionHandler { public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({
@ResponseStatus(BAD_REQUEST) CategoryDeletionException.class,
@ExceptionHandler(LoginFailureException.class) CategoryEditionException.class,
public void handleLoginFailureException() { CategoryNotFoundException.class,
// Do nothing. LoginFailureException.class,
PublicationEditionException.class,
PictureUploadException.class
})
public ProblemDetail handleBadRequestExceptions(Exception exception) {
return buildProblemDetail(BAD_REQUEST, exception);
} }
@ResponseStatus(NOT_FOUND) @ExceptionHandler({
@ExceptionHandler(UserDoesNotExistException.class) UserDoesNotExistException.class,
public void handleUserDoesNotExistException() { RefreshTokenDoesNotExistException.class,
// Do nothing. PublicationNotFoundException.class,
PictureNotFoundException.class
})
public ProblemDetail handleNotFoundExceptions(Exception exception) {
return buildProblemDetail(NOT_FOUND, exception);
} }
@ResponseStatus(NOT_FOUND) @ExceptionHandler({
@ExceptionHandler(RefreshTokenDoesNotExistException.class) RefreshTokenExpiredException.class
public void handleRefreshTokenDoesNotExistException() { })
// Do nothing. public ProblemDetail handleUnauthorizedExceptions(Exception exception) {
return buildProblemDetail(UNAUTHORIZED, exception);
} }
@ResponseStatus(UNAUTHORIZED) @ExceptionHandler({
@ExceptionHandler(RefreshTokenExpiredException.class) PublicationUpdateForbiddenException.class
public void handleRefreshTokenExpiredException() { })
// Do nothing. public ProblemDetail handleForbiddenExceptions(Exception exception) {
return buildProblemDetail(FORBIDDEN, exception);
} }
@ResponseStatus(BAD_REQUEST) private static ProblemDetail buildProblemDetail(HttpStatus forbidden, Exception exception) {
@ExceptionHandler(CategoryNotFoundException.class) return ProblemDetail.forStatusAndDetail(forbidden, exception.getMessage());
public void handleCategoryNotFoundException() {
// Do nothing.
} }
@ResponseStatus(BAD_REQUEST)
@ExceptionHandler(PublicationEditionException.class)
public void handlePublicationEditionException() {
// Do nothing.
}
@ResponseStatus(NOT_FOUND)
@ExceptionHandler(PublicationNotFoundException.class)
public void handlePublicationNotFoundException() {
// Do nothing.
}
@ResponseStatus(FORBIDDEN)
@ExceptionHandler(PublicationUpdateForbiddenException.class)
public void handlePublicationUpdateForbiddenException() {
// Do nothing.
}
@ResponseStatus(BAD_REQUEST)
@ExceptionHandler(CategoryEditionException.class)
public void handleCategoryEditionException() {
// Do nothing.
}
@ResponseStatus(BAD_REQUEST)
@ExceptionHandler(CategoryDeletionException.class)
public void handleCategoryDeletionException() {
// Do nothing.
}
@ResponseStatus(BAD_REQUEST)
@ExceptionHandler(PictureUploadException.class)
public void handlePictureUploadException() {
// Do nothing.
}
@ResponseStatus(NOT_FOUND)
@ExceptionHandler(PictureNotFoundException.class)
public void handlePictureNotFoundException() {
// Do nothing.
}
} }

View File

@@ -6,6 +6,7 @@ import static org.springframework.http.HttpMethod.OPTIONS;
import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpMethod.PUT; import static org.springframework.http.HttpMethod.PUT;
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS; import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
import org.codiki.domain.user.model.UserRole;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer; import org.springframework.security.config.Customizer;
@@ -55,15 +56,15 @@ public class SecurityConfiguration {
.requestMatchers( .requestMatchers(
POST, POST,
"/api/categories" "/api/categories"
).hasRole("ADMIN") ).hasRole(UserRole.ADMIN.name())
.requestMatchers( .requestMatchers(
PUT, PUT,
"/api/categories/{categoryId}" "/api/categories/{categoryId}"
).hasRole("ADMIN") ).hasRole(UserRole.ADMIN.name())
.requestMatchers( .requestMatchers(
DELETE, DELETE,
"/api/categories/{categoryId}" "/api/categories/{categoryId}"
).hasRole("ADMIN") ).hasRole(UserRole.ADMIN.name())
.requestMatchers(OPTIONS).permitAll() .requestMatchers(OPTIONS).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
); );