-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added data validation and global exception handler #4
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
16 changes: 16 additions & 0 deletions
16
src/main/java/mate/academy/bookstore/dto/CreateBookRequestDto.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,13 +1,29 @@ | ||
package mate.academy.bookstore.dto; | ||
|
||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
import java.math.BigDecimal; | ||
import lombok.Data; | ||
import org.hibernate.validator.constraints.ISBN; | ||
|
||
@Data | ||
public class CreateBookRequestDto { | ||
@NotNull | ||
@NotEmpty | ||
@Size(max = 255) | ||
private String title; | ||
@NotNull | ||
@NotEmpty | ||
@Size(max = 255) | ||
private String author; | ||
@NotNull | ||
@NotEmpty | ||
@ISBN | ||
private String isbn; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use ISBN annotation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isbn has other validation rule except of not null (see Oleg's suggestion) |
||
@NotNull | ||
@Min(0) | ||
private BigDecimal price; | ||
private String description; | ||
private String coverImage; | ||
|
46 changes: 46 additions & 0 deletions
46
src/main/java/mate/academy/bookstore/exceptions/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,46 @@ | ||
package mate.academy.bookstore.exceptions; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
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.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 | ||
) { | ||
Map<String, Object> body = new LinkedHashMap<>(); | ||
body.put("timestamp", LocalDateTime.now()); | ||
body.put("status", HttpStatus.UNPROCESSABLE_ENTITY); | ||
List<String> errors = ex.getBindingResult().getAllErrors().stream() | ||
.map(this::getErrorMessage) | ||
.toList(); | ||
body.put("errors", errors); | ||
return super.handleMethodArgumentNotValid(ex, headers, status, request); | ||
} | ||
|
||
private String getErrorMessage(ObjectError e) { | ||
if (e instanceof FieldError) { | ||
String field = ((FieldError) e).getField(); | ||
String message = e.getDefaultMessage(); | ||
return field + " " + message; | ||
} | ||
return e.getDefaultMessage(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate the size of title to not allow empty titles and to not exceed the maximum size set by DB schema, the same for author field