-
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
Hw add global exception handler #7
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||
package project.bookstore.exception; | ||||||
|
||||||
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 { | ||||||
|
||||||
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.
Suggested change
|
||||||
@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.BAD_REQUEST); | ||||||
List<String> errors = ex.getBindingResult().getAllErrors().stream() | ||||||
.map(this::getErrorMessage) | ||||||
.toList(); | ||||||
body.put("error", errors); | ||||||
return new ResponseEntity<>(body, headers, status); | ||||||
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.
Suggested change
|
||||||
} | ||||||
|
||||||
private String getErrorMessage(ObjectError e) { | ||||||
if (e instanceof FieldError) { | ||||||
String fieldName = ((FieldError) e).getField(); | ||||||
String message = ((FieldError) e).getDefaultMessage(); | ||||||
return fieldName + ": " + message; | ||||||
} | ||||||
return e.getDefaultMessage(); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,6 @@ | |
|
||
public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor<Book> { | ||
|
||
boolean existsByIsbn(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. remove |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package project.bookstore.validation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Constraint(validatedBy = UniqueValidation.class) | ||
@Target({ElementType.FIELD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Unique { | ||
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. remove redundant validatot |
||
String message() default "This field is already exist!"; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package project.bookstore.validation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import project.bookstore.repository.BookRepository; | ||
|
||
public class UniqueValidation implements ConstraintValidator<Unique, String> { | ||
@Autowired | ||
private BookRepository repository; | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { | ||
return !repository.existsByIsbn(value); | ||
} | ||
} |
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.