Skip to content

Commit

Permalink
refactored the exceptionhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelPelegrina committed Apr 12, 2024
1 parent dd93298 commit 689d12f
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.java_school.final_task.exception.user.InactiveUserException;
import com.java_school.final_task.exception.user.InsufficientPermissionsException;
import com.java_school.final_task.exception.user.UserDoesNotExistException;
import com.lowagie.text.DocumentException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
Expand All @@ -14,6 +15,15 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

// TODO
// - Reduce information that is send to the frontend, especially regarding user data, like name and password not
// matching as that already implies that the introduced user exists
// - Introduce logging to keep track of exceptions

/**
* Global exception handler for handling business logic exceptions thrown within the application.
* It provides centralized exception handling across all @Controller classes.
*/
@RestControllerAdvice
public class BusinessLogicExceptionHandler {
@ExceptionHandler(AccessDeniedException.class)
Expand All @@ -25,7 +35,13 @@ public ResponseEntity<Object> handleAccessDenied(AccessDeniedException e) {
@ExceptionHandler(BadCredentialsException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public ResponseEntity<Object> handleBadCredentials(BadCredentialsException e) {
return ResponseEntity.status(409).body(e.getMessage());
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
}

@ExceptionHandler(DocumentException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<Object> handleDocumentException(DocumentException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}

@ExceptionHandler(EmailAlreadyUsedException.class)
Expand All @@ -49,30 +65,30 @@ public ResponseEntity<Object> handleInactiveUserException(InactiveUserException
@ExceptionHandler(InsufficientPermissionsException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseEntity<Object> handleInsufficientPermissionsException(InsufficientPermissionsException e) {
return ResponseEntity.status(403).body(e.getMessage());
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getMessage());
}

@ExceptionHandler(ProductNotAvailableException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ResponseEntity<Object> handleProductNotAvailable(ProductNotAvailableException e) {
return ResponseEntity.status(405).body(e.getMessage());
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(e.getMessage());
}

@ExceptionHandler(ProductOutOfStockException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ResponseEntity<Object> handleProductOutOfStock(ProductOutOfStockException e) {
return ResponseEntity.status(405).body(e.getMessage());
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(e.getMessage());
}

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<Object> handleNotFound(ResourceNotFoundException e) {
return ResponseEntity.status(404).body(e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
}

@ExceptionHandler(UserDoesNotExistException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<Object> handleUserDoesNotExist(UserDoesNotExistException e) {
return ResponseEntity.status(404).body(e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
}
}

0 comments on commit 689d12f

Please sign in to comment.