-
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.
- Loading branch information
1 parent
92a893a
commit 99a874d
Showing
3 changed files
with
63 additions
and
28 deletions.
There are no files selected for viewing
65 changes: 37 additions & 28 deletions
65
src/main/java/mate/academy/bookstore/handler/BookControllerExceptionHandler.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 |
---|---|---|
@@ -1,60 +1,69 @@ | ||
package mate.academy.bookstore.handler; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.List; | ||
import mate.academy.bookstore.exception.EntityNotFoundException; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.data.mapping.PropertyReferenceException; | ||
import org.springframework.http.HttpStatus; | ||
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.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ControllerAdvice | ||
public class BookControllerExceptionHandler { | ||
@ExceptionHandler(EntityNotFoundException.class) | ||
@ResponseBody | ||
public ResponseEntity<Object> handleEntityNotFoundException(EntityNotFoundException ex) { | ||
return getResponseEntity(HttpStatus.NOT_FOUND, ex.getMessage()); | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public ResponseData handleEntityNotFoundException(EntityNotFoundException ex) { | ||
return new ResponseData( | ||
HttpStatus.NOT_FOUND, | ||
null, | ||
List.of(new ErrorMessage("", ex.getMessage())) | ||
); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseBody | ||
public ResponseEntity<Object> handleValidationExceptions(MethodArgumentNotValidException ex) { | ||
Map<String, String> errors = new HashMap<>(); | ||
ex.getBindingResult().getAllErrors().forEach(error -> { | ||
Map.Entry<String, String> errorMessage = getErrorMessage(error); | ||
errors.put(errorMessage.getKey(), errorMessage.getValue()); | ||
}); | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public ResponseData handleValidationExceptions(MethodArgumentNotValidException ex) { | ||
List<ErrorMessage> errors = ex.getBindingResult().getAllErrors().stream() | ||
.map(this::getErrorMessage) | ||
.toList(); | ||
return new ResponseData(HttpStatus.BAD_REQUEST, null, errors); | ||
} | ||
|
||
return getResponseEntity(HttpStatus.BAD_REQUEST, errors); | ||
@ExceptionHandler(PropertyReferenceException.class) | ||
@ResponseBody | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public ResponseData handlePropertyReferenceException(PropertyReferenceException ex) { | ||
return new ResponseData( | ||
HttpStatus.BAD_REQUEST, | ||
null, | ||
List.of(new ErrorMessage("", ex.getMessage())) | ||
); | ||
} | ||
|
||
@ExceptionHandler(DataIntegrityViolationException.class) | ||
@ResponseBody | ||
public ResponseEntity<Object> handleDataIntegrityViolationException( | ||
@ResponseStatus(HttpStatus.CONFLICT) | ||
public ResponseData handleDataIntegrityViolationException( | ||
DataIntegrityViolationException ex) { | ||
return getResponseEntity(HttpStatus.CONFLICT, ex.getMessage()); | ||
return new ResponseData( | ||
HttpStatus.CONFLICT, | ||
null, | ||
List.of(new ErrorMessage("", ex.getMessage())) | ||
); | ||
} | ||
|
||
private Map.Entry<String, String> getErrorMessage(ObjectError e) { | ||
private ErrorMessage getErrorMessage(ObjectError e) { | ||
String field = ""; | ||
if (e instanceof FieldError fieldError) { | ||
String field = fieldError.getField(); | ||
String message = e.getDefaultMessage(); | ||
return Map.entry(field, message); | ||
field = fieldError.getField(); | ||
} | ||
return Map.entry("", e.getDefaultMessage()); | ||
} | ||
|
||
private ResponseEntity<Object> getResponseEntity(HttpStatus status, Object errors) { | ||
Map<String, Object> body = new LinkedHashMap<>(); | ||
body.put("success", status.is2xxSuccessful()); | ||
body.put("errors", errors); | ||
return new ResponseEntity<>(body, status); | ||
return new ErrorMessage(field, e.getDefaultMessage()); | ||
} | ||
} | ||
|
4 changes: 4 additions & 0 deletions
4
src/main/java/mate/academy/bookstore/handler/ErrorMessage.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,4 @@ | ||
package mate.academy.bookstore.handler; | ||
|
||
public record ErrorMessage(String field, String message) { | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/mate/academy/bookstore/handler/ResponseData.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,22 @@ | ||
package mate.academy.bookstore.handler; | ||
|
||
import java.util.List; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class ResponseData { | ||
private boolean success; | ||
|
||
private int status; | ||
private Object data; | ||
private List<ErrorMessage> errors; | ||
|
||
public ResponseData(HttpStatus status, Object data, List<ErrorMessage> errors) { | ||
this.data = data; | ||
this.errors = errors; | ||
this.status = status.value(); | ||
this.success = status.is2xxSuccessful(); | ||
} | ||
} | ||
|