-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GLOBAL EXCEPTION HANDLER] Created global exception handler
* created car controller * fixed checkstyle * added global exception handler * deleted empty line in CarController
- Loading branch information
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/project/carsharingapp/exception/CustomGlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Object> handleMethodArgumentNotValid( | ||
MethodArgumentNotValidException ex, | ||
HttpHeaders headers, | ||
HttpStatusCode status, | ||
WebRequest request | ||
) { | ||
List<String> 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<Object> 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<Object> 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<Object> 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(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/project/carsharingapp/exception/EntityNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.project.carsharingapp.exception; | ||
|
||
public class EntityNotFoundException extends RuntimeException { | ||
public EntityNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/project/carsharingapp/exception/ErrorResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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){ | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/project/carsharingapp/exception/RegistrationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.project.carsharingapp.exception; | ||
|
||
public class RegistrationException extends RuntimeException { | ||
public RegistrationException(String message) { | ||
super(message); | ||
} | ||
} |