From 32f836b5bee6018b38454428c9fba8842bbe4f46 Mon Sep 17 00:00:00 2001 From: Dmytro Varukha <132067460+DmytroV95@users.noreply.github.com> Date: Tue, 3 Oct 2023 11:56:21 +0300 Subject: [PATCH] [GLOBAL EXCEPTION HANDLER] Created global exception handler * created car controller * fixed checkstyle * added global exception handler * deleted empty line in CarController --- .../controller/CarController.java | 1 - .../CustomGlobalExceptionHandler.java | 79 +++++++++++++++++++ .../exception/EntityNotFoundException.java | 7 ++ .../exception/ErrorResponseDto.java | 9 +++ .../exception/RegistrationException.java | 7 ++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/project/carsharingapp/exception/CustomGlobalExceptionHandler.java create mode 100644 src/main/java/com/project/carsharingapp/exception/EntityNotFoundException.java create mode 100644 src/main/java/com/project/carsharingapp/exception/ErrorResponseDto.java create mode 100644 src/main/java/com/project/carsharingapp/exception/RegistrationException.java diff --git a/src/main/java/com/project/carsharingapp/controller/CarController.java b/src/main/java/com/project/carsharingapp/controller/CarController.java index 9f3a307..fcbe3f1 100644 --- a/src/main/java/com/project/carsharingapp/controller/CarController.java +++ b/src/main/java/com/project/carsharingapp/controller/CarController.java @@ -24,7 +24,6 @@ @RestController @RequestMapping(value = "/cars") public class CarController { - @PostMapping @ResponseStatus(HttpStatus.CREATED) @Operation(summary = "Save new car", diff --git a/src/main/java/com/project/carsharingapp/exception/CustomGlobalExceptionHandler.java b/src/main/java/com/project/carsharingapp/exception/CustomGlobalExceptionHandler.java new file mode 100644 index 0000000..9947cce --- /dev/null +++ b/src/main/java/com/project/carsharingapp/exception/CustomGlobalExceptionHandler.java @@ -0,0 +1,79 @@ +package com.project.carsharingapp.exception; + +import java.time.LocalDateTime; +import java.util.List; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@ControllerAdvice +public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler { + @Override + protected ResponseEntity handleMethodArgumentNotValid( + MethodArgumentNotValidException ex, + HttpHeaders headers, + HttpStatusCode status, + WebRequest request + ) { + List errors = ex.getBindingResult().getAllErrors() + .stream() + .map(this::getErrorMessage) + .toList(); + ErrorResponseDto response = new ErrorResponseDto( + LocalDateTime.now(), + HttpStatus.BAD_REQUEST, + errors.toArray(new String[0]) + ); + return new ResponseEntity<>(response, headers, HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(RegistrationException.class) + protected ResponseEntity handleRegistrationException( + RegistrationException exception) { + ErrorResponseDto response = new ErrorResponseDto( + LocalDateTime.now(), + HttpStatus.BAD_REQUEST, + new String[]{exception.getMessage()} + ); + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(RuntimeException.class) + protected ResponseEntity handleAllErrors( + RuntimeException exception) { + ErrorResponseDto response = new ErrorResponseDto( + LocalDateTime.now(), + HttpStatus.INTERNAL_SERVER_ERROR, + new String[]{exception.getMessage()} + ); + return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); + } + + @ExceptionHandler(EntityNotFoundException.class) + protected ResponseEntity handleAllErrors( + EntityNotFoundException exception) { + ErrorResponseDto response = new ErrorResponseDto( + LocalDateTime.now(), + HttpStatus.NOT_FOUND, + new String[]{exception.getMessage()} + ); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } + + private String getErrorMessage(ObjectError e) { + if (e instanceof FieldError) { + String field = ((FieldError) e).getField(); + String message = e.getDefaultMessage(); + return field + " " + message; + } + return e.getDefaultMessage(); + } +} diff --git a/src/main/java/com/project/carsharingapp/exception/EntityNotFoundException.java b/src/main/java/com/project/carsharingapp/exception/EntityNotFoundException.java new file mode 100644 index 0000000..a16ebd2 --- /dev/null +++ b/src/main/java/com/project/carsharingapp/exception/EntityNotFoundException.java @@ -0,0 +1,7 @@ +package com.project.carsharingapp.exception; + +public class EntityNotFoundException extends RuntimeException { + public EntityNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/project/carsharingapp/exception/ErrorResponseDto.java b/src/main/java/com/project/carsharingapp/exception/ErrorResponseDto.java new file mode 100644 index 0000000..2ca9480 --- /dev/null +++ b/src/main/java/com/project/carsharingapp/exception/ErrorResponseDto.java @@ -0,0 +1,9 @@ +package com.project.carsharingapp.exception; + +import java.time.LocalDateTime; +import org.springframework.http.HttpStatus; + +public record ErrorResponseDto(LocalDateTime timestamp, + HttpStatus status, + String[] errors){ +} diff --git a/src/main/java/com/project/carsharingapp/exception/RegistrationException.java b/src/main/java/com/project/carsharingapp/exception/RegistrationException.java new file mode 100644 index 0000000..a8a210b --- /dev/null +++ b/src/main/java/com/project/carsharingapp/exception/RegistrationException.java @@ -0,0 +1,7 @@ +package com.project.carsharingapp.exception; + +public class RegistrationException extends RuntimeException { + public RegistrationException(String message) { + super(message); + } +}