-
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
Global exception dev #4
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f3acbda
create customGlobalException
don-bigdad b24f2b0
fix form prev branch
don-bigdad 3faed89
change fields from notNull to NotBlank
don-bigdad 9f101e7
Merge branch 'book-jpa-dev' into global-exception-dev
don-bigdad 756da8a
work with exceptions add some specific when entity doesn`t exist in db
don-bigdad 22a6c92
mvn fix
don-bigdad 82f9f54
change method name
don-bigdad 9ef880a
Merge branch 'master' into global-exception-dev
don-bigdad 933ed11
test
don-bigdad 0b0a819
Merge remote-tracking branch 'origin/global-exception-dev' into globa…
don-bigdad a8a77b0
test
don-bigdad 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
11 changes: 7 additions & 4 deletions
11
src/main/java/com/example/springbootbookshop/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,11 +1,14 @@ | ||
package com.example.springbootbookshop.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Positive; | ||
import java.math.BigDecimal; | ||
|
||
public record CreateBookRequestDto(String title, | ||
String author, | ||
String isbn, | ||
BigDecimal price, | ||
public record CreateBookRequestDto(@NotBlank String title, | ||
@NotBlank String author, | ||
@NotBlank String isbn, | ||
@NotNull @Positive BigDecimal price, | ||
String description, | ||
String coverImage) { | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/example/springbootbookshop/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,48 @@ | ||
package com.example.springbootbookshop.exception; | ||
|
||
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.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( | ||
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. do you have any other exceptions except for does it make sense to handle them as well? |
||
MethodArgumentNotValidException ex, | ||
HttpHeaders headers, HttpStatusCode status, | ||
WebRequest request) { | ||
Map<String,Object> body = new LinkedHashMap<>(); | ||
List<String> errors = ex.getBindingResult().getAllErrors().stream() | ||
.map(this::getErrorMessage) | ||
.toList(); | ||
body.put("errors", errors); | ||
return new ResponseEntity<>(body,headers,status); | ||
} | ||
|
||
@ExceptionHandler(EntityNotFoundException.class) | ||
public ResponseEntity<Object> handleEntityNotFoundException( | ||
EntityNotFoundException ex) { | ||
System.out.println(ex.getMessage()); | ||
return new ResponseEntity<>(ex.getLocalizedMessage(), HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
private String getErrorMessage(ObjectError e) { | ||
if (e instanceof FieldError) { | ||
String field = ((FieldError) e).getField(); | ||
String message = e.getDefaultMessage(); | ||
return String.format("%s %s",field,message); | ||
} | ||
return e.getDefaultMessage(); | ||
} | ||
} |
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
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.
there is no need to specify the version here