Skip to content

Commit

Permalink
Updated error response fields and exception handling style
Browse files Browse the repository at this point in the history
  • Loading branch information
egorsivenko committed Apr 19, 2024
1 parent 856d0b2 commit 080aeef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exception) {
ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, "Validation failed!",
Objects.requireNonNull(exception.getFieldError()).getDefaultMessage());
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpServletRequest request) {
ErrorResponse errorResponse = buildErrorResponse(
HttpStatus.BAD_REQUEST,
Objects.requireNonNull(ex.getFieldError()).getDefaultMessage(),
request.getRequestURI());
return ResponseEntity.badRequest().body(errorResponse);
}

Expand All @@ -45,9 +48,10 @@ public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotVali
* @return {@link ResponseEntity} object with the corresponding status and error message
*/
@ExceptionHandler(NullEmailException.class)
public ResponseEntity<Object> handleNullEmailException(NullEmailException ex) {
public ResponseEntity<Object> 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);
}

Expand All @@ -58,9 +62,10 @@ public ResponseEntity<Object> handleNullEmailException(NullEmailException ex) {
* @return {@link ResponseEntity} containing the error response for authentication failure
*/
@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<Object> handleAuthenticationException(AuthenticationException ex) {
public ResponseEntity<Object> 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);
}

Expand All @@ -71,9 +76,10 @@ public ResponseEntity<Object> handleAuthenticationException(AuthenticationExcept
* @return {@link ResponseEntity} containing the error response for email already taken
*/
@ExceptionHandler(EmailAlreadyTakenException.class)
public ResponseEntity<Object> handleEmailAlreadyTakenException(EmailAlreadyTakenException ex) {
public ResponseEntity<Object> 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);
}

Expand All @@ -84,23 +90,25 @@ public ResponseEntity<Object> 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<Object> handleNotFoundExceptions(Exception ex) {
ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, "Email Not Found!",
ex.getMessage());
@ExceptionHandler({NoSuchEmailFoundException.class,
NoUserFoundByEmailException.class, NoUserFoundByIdException.class})
public ResponseEntity<Object> 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);
}
}

0 comments on commit 080aeef

Please sign in to comment.