From 080aeef5c807bc47c5e00340b05fd91b95d467c0 Mon Sep 17 00:00:00 2001 From: EgorSivenko Date: Fri, 19 Apr 2024 14:24:19 +0300 Subject: [PATCH] Updated error response fields and exception handling style --- .../urlshortener/exception/ErrorResponse.java | 18 +++---- .../exception/GlobalExceptionHandler.java | 50 +++++++++++-------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/linkurlshorter/urlshortener/exception/ErrorResponse.java b/src/main/java/com/linkurlshorter/urlshortener/exception/ErrorResponse.java index cf45b43..d61c002 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/exception/ErrorResponse.java +++ b/src/main/java/com/linkurlshorter/urlshortener/exception/ErrorResponse.java @@ -1,19 +1,15 @@ package com.linkurlshorter.urlshortener.exception; -import lombok.AllArgsConstructor; -import lombok.Data; - import java.time.LocalDateTime; + /** * Class representing the error response object. * * @author Vlas Pototskyi */ -@Data -@AllArgsConstructor -public class ErrorResponse { - private LocalDateTime localDateTime; - private int statusCode; - private String message; - private String exceptionMessage; -} +public record ErrorResponse( + LocalDateTime dateTime, + int statusCode, + String message, + String path +) {} diff --git a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java index 8de426b..c44821b 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java @@ -5,7 +5,7 @@ import com.linkurlshorter.urlshortener.user.NoUserFoundByEmailException; import com.linkurlshorter.urlshortener.user.NoUserFoundByIdException; import com.linkurlshorter.urlshortener.user.NullEmailException; -import org.apache.coyote.BadRequestException; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.AuthenticationException; @@ -27,13 +27,16 @@ 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 exception method argument validation error + * @param ex method argument validation error * @return {@link ResponseEntity} object with the appropriate status and error message */ - @ExceptionHandler({MethodArgumentNotValidException.class, BadRequestException.class}) - public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException exception) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, "Validation failed!", - Objects.requireNonNull(exception.getFieldError()).getDefaultMessage()); + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleMethodArgumentNotValid( + MethodArgumentNotValidException ex, HttpServletRequest request) { + ErrorResponse errorResponse = buildErrorResponse( + HttpStatus.BAD_REQUEST, + Objects.requireNonNull(ex.getFieldError()).getDefaultMessage(), + request.getRequestURI()); return ResponseEntity.badRequest().body(errorResponse); } @@ -45,9 +48,10 @@ public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotVali * @return {@link ResponseEntity} object with the corresponding status and error message */ @ExceptionHandler(NullEmailException.class) - public ResponseEntity handleNullEmailException(NullEmailException ex) { + public ResponseEntity handleNullEmailException( + NullEmailException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, - "Email provided is null, so request can not be processed!", ex.getMessage()); + ex.getMessage(), request.getRequestURI()); return ResponseEntity.badRequest().body(errorResponse); } @@ -58,9 +62,10 @@ public ResponseEntity handleNullEmailException(NullEmailException ex) { * @return {@link ResponseEntity} containing the error response for authentication failure */ @ExceptionHandler(AuthenticationException.class) - public ResponseEntity handleAuthenticationException(AuthenticationException ex) { + public ResponseEntity handleAuthenticationException( + AuthenticationException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.UNAUTHORIZED, - "Authentication failed!", ex.getMessage()); + ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse); } @@ -71,9 +76,10 @@ public ResponseEntity handleAuthenticationException(AuthenticationExcept * @return {@link ResponseEntity} containing the error response for email already taken */ @ExceptionHandler(EmailAlreadyTakenException.class) - public ResponseEntity handleEmailAlreadyTakenException(EmailAlreadyTakenException ex) { + public ResponseEntity handleEmailAlreadyTakenException( + EmailAlreadyTakenException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, - "Email already taken!", ex.getMessage()); + ex.getMessage(), request.getRequestURI()); return ResponseEntity.badRequest().body(errorResponse); } @@ -84,23 +90,25 @@ public ResponseEntity handleEmailAlreadyTakenException(EmailAlreadyTaken * @param ex missing resource exception * @return {@link ResponseEntity} object with the corresponding status and error message */ - @ExceptionHandler({NoSuchEmailFoundException.class, NoUserFoundByEmailException.class, NoUserFoundByIdException.class}) - public ResponseEntity handleNotFoundExceptions(Exception ex) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, "Email Not Found!", - ex.getMessage()); + @ExceptionHandler({NoSuchEmailFoundException.class, + NoUserFoundByEmailException.class, NoUserFoundByIdException.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 exceptionMessage exception message + * @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 exceptionMessage) { - return new ErrorResponse(LocalDateTime.now(), status.value(), message, exceptionMessage); + private ErrorResponse buildErrorResponse(HttpStatus status, String message, String requestURI) { + return new ErrorResponse(LocalDateTime.now(), status.value(), message, requestURI); } }