Skip to content

Commit

Permalink
Merge pull request #96 from nastiausenko/refactor/exception-handling
Browse files Browse the repository at this point in the history
General code cleanup in `GlobalExceptionHandler`
  • Loading branch information
IvanShalaev1990 authored Apr 24, 2024
2 parents c1b8dba + 3908f40 commit f416ea8
Showing 1 changed file with 23 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Object> handleMethodArgumentNotValid(
public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(
MethodArgumentNotValidException ex, HttpServletRequest request) {
ErrorResponse errorResponse = buildErrorResponse(
HttpStatus.BAD_REQUEST,
Expand All @@ -40,100 +39,49 @@ public ResponseEntity<Object> 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<Object> handleAuthenticationException(
public ResponseEntity<ErrorResponse> 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<Object> handleEmailAlreadyTakenException(
EmailAlreadyTakenException ex, HttpServletRequest request) {
@ExceptionHandler({EmailAlreadyTakenException.class, LinkStatusException.class,
DeletedLinkException.class, InactiveLinkException.class})
public ResponseEntity<ErrorResponse> 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<Object> handleNotFoundExceptions(
@ExceptionHandler({NoSuchEmailFoundException.class, NoUserFoundByEmailException.class,
NoUserFoundByIdException.class, NoLinkFoundByShortLinkException.class})
public ResponseEntity<ErrorResponse> 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<Object> handleForbiddenException(
public ResponseEntity<ErrorResponse> 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<Object> 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<Object> 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<Object> handleInternalServerLinkException(
public ResponseEntity<ErrorResponse> 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);
}
}

0 comments on commit f416ea8

Please sign in to comment.