diff --git a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java index 9b5c7d0..67f40d3 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java @@ -1,7 +1,12 @@ package com.linkurlshorter.urlshortener.exception; import com.linkurlshorter.urlshortener.auth.exception.EmailAlreadyTakenException; -import com.linkurlshorter.urlshortener.link.exception.*; +import com.linkurlshorter.urlshortener.link.exception.DeletedLinkException; +import com.linkurlshorter.urlshortener.link.exception.ForbiddenException; +import com.linkurlshorter.urlshortener.link.exception.InactiveLinkException; +import com.linkurlshorter.urlshortener.link.exception.InternalServerLinkException; +import com.linkurlshorter.urlshortener.link.exception.LinkStatusException; +import com.linkurlshorter.urlshortener.link.exception.NoLinkFoundByShortLinkException; import com.linkurlshorter.urlshortener.user.exception.NoSuchEmailFoundException; import com.linkurlshorter.urlshortener.user.exception.NoUserFoundByEmailException; import com.linkurlshorter.urlshortener.user.exception.NoUserFoundByIdException; @@ -23,15 +28,9 @@ */ @ControllerAdvice public class GlobalExceptionHandler { - /** - * Handles method argument validation errors and invalid request errors (400). - * Returns a response with status 400 and the corresponding error message. - * - * @param ex method argument validation error - * @return {@link ResponseEntity} object with the appropriate status and error message - */ + @ExceptionHandler(MethodArgumentNotValidException.class) - public ResponseEntity handleMethodArgumentNotValid( + public ResponseEntity handleMethodArgumentNotValidException( MethodArgumentNotValidException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse( HttpStatus.BAD_REQUEST, @@ -40,100 +39,49 @@ public ResponseEntity handleMethodArgumentNotValid( return ResponseEntity.badRequest().body(errorResponse); } - /** - * Handles AuthenticationException thrown during user authentication. - * - * @param ex the AuthenticationException thrown - * @return {@link ResponseEntity} containing the error response for authentication failure - */ @ExceptionHandler(AuthenticationException.class) - public ResponseEntity handleAuthenticationException( + public ResponseEntity handleAuthenticationException( AuthenticationException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.UNAUTHORIZED, ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse); } - /** - * Handles EmailAlreadyTakenException thrown during user registration. - * - * @param ex the EmailAlreadyTakenException thrown - * @return {@link ResponseEntity} containing the error response for email already taken - */ - @ExceptionHandler(EmailAlreadyTakenException.class) - public ResponseEntity handleEmailAlreadyTakenException( - EmailAlreadyTakenException ex, HttpServletRequest request) { + @ExceptionHandler({EmailAlreadyTakenException.class, LinkStatusException.class, + DeletedLinkException.class, InactiveLinkException.class}) + public ResponseEntity handleBadRequestExceptions( + RuntimeException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, ex.getMessage(), request.getRequestURI()); - return ResponseEntity.badRequest().body(errorResponse); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); } - /** - * Handles no resource (404) exceptions for different types of requests. - * Returns a response with a 404 status and the corresponding error message. - * - * @param ex missing resource exception - * @return {@link ResponseEntity} object with the corresponding status and error message - */ - @ExceptionHandler({NoSuchEmailFoundException.class, - NoUserFoundByEmailException.class, NoUserFoundByIdException.class, NoLinkFoundByShortLinkException.class}) - public ResponseEntity handleNotFoundExceptions( + @ExceptionHandler({NoSuchEmailFoundException.class, NoUserFoundByEmailException.class, + NoUserFoundByIdException.class, NoLinkFoundByShortLinkException.class}) + public ResponseEntity handleNotFoundExceptions( RuntimeException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } - /** - * Creates an error response object. - * - * @param status status of the error - * @param message error message - * @param requestURI request URL - * @return an {@link ErrorResponse} object with the appropriate data - */ - private ErrorResponse buildErrorResponse(HttpStatus status, String message, String requestURI) { - return new ErrorResponse(LocalDateTime.now(), status.value(), message, requestURI); - } - - /** - * Handles Forbidden (403) exceptions for different types of requests. - * Returns a response with a 403 status and the corresponding error message. - * - * @param ex forbidden exception - * @param request HttpServletRequest object representing the HTTP request - * @return {@link ResponseEntity} object with the corresponding status and error message - */ @ExceptionHandler(ForbiddenException.class) - public ResponseEntity handleForbiddenException( + public ResponseEntity handleForbiddenException( ForbiddenException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN, ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse); } - @ExceptionHandler({DeletedLinkException.class, InactiveLinkException.class}) - public ResponseEntity handleDeletedAndInactiveLinkException( - RuntimeException ex, HttpServletRequest request) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, - ex.getMessage(), request.getRequestURI()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); - } - - @ExceptionHandler(LinkStatusException.class) - public ResponseEntity handleLinkStatusException( - LinkStatusException ex, HttpServletRequest request) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, - ex.getMessage(), request.getRequestURI()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); - } - @ExceptionHandler(InternalServerLinkException.class) - public ResponseEntity handleInternalServerLinkException( + public ResponseEntity handleInternalServerLinkException( InternalServerLinkException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse); } -} + private ErrorResponse buildErrorResponse(HttpStatus status, String message, String requestURI) { + return new ErrorResponse(LocalDateTime.now(), status.value(), message, requestURI); + } +}