-
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
aeb6c83
commit 97cd8e4
Showing
17 changed files
with
213 additions
and
132 deletions.
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
57 changes: 57 additions & 0 deletions
57
src/main/java/mate/academy/bookstore/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,57 @@ | ||
package mate.academy.bookstore.exception; | ||
|
||
import java.util.List; | ||
import mate.academy.bookstore.response.ErrorResponse; | ||
import mate.academy.bookstore.response.ResponseHandler; | ||
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.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class CustomGlobalExceptionHandler { | ||
@ExceptionHandler(EntityNotFoundException.class) | ||
public ResponseEntity<ErrorResponse> handleEntityNotFoundException( | ||
EntityNotFoundException ex) { | ||
return ResponseHandler.getErrorResponse( | ||
ex.getMessage(), HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ErrorResponse> handleValidationExceptions( | ||
MethodArgumentNotValidException ex) { | ||
List<ErrorResponse.Message> errors = ex.getBindingResult().getAllErrors() | ||
.stream() | ||
.map(this::getErrorMessage) | ||
.toList(); | ||
return ResponseHandler.getErrorResponse( | ||
errors, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(PropertyReferenceException.class) | ||
public ResponseEntity<ErrorResponse> handlePropertyReferenceException( | ||
PropertyReferenceException ex) { | ||
return ResponseHandler.getErrorResponse( | ||
ex.getMessage(), HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(DataIntegrityViolationException.class) | ||
public ResponseEntity<ErrorResponse> handleDataIntegrityViolationException( | ||
DataIntegrityViolationException ex) { | ||
return ResponseHandler.getErrorResponse( | ||
ex.getMessage(), HttpStatus.CONFLICT); | ||
} | ||
|
||
private ErrorResponse.Message getErrorMessage(ObjectError e) { | ||
String field = null; | ||
if (e instanceof FieldError fieldError) { | ||
field = fieldError.getField(); | ||
} | ||
return new ErrorResponse.Message(field, e.getDefaultMessage()); | ||
} | ||
} |
69 changes: 0 additions & 69 deletions
69
src/main/java/mate/academy/bookstore/handler/CustomGlobalExceptionHandler.java
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
src/main/java/mate/academy/bookstore/handler/ErrorMessage.java
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/main/java/mate/academy/bookstore/handler/ErrorResponseData.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
src/main/java/mate/academy/bookstore/handler/ResponseData.java
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/main/java/mate/academy/bookstore/handler/SuccessResponseData.java
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/main/java/mate/academy/bookstore/response/ErrorResponse.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,18 @@ | ||
package mate.academy.bookstore.response; | ||
|
||
import java.util.List; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class ErrorResponse extends GeneralResponse { | ||
private List<Message> errors; | ||
|
||
public record Message(String field, String message) { | ||
} | ||
|
||
public ErrorResponse(List<Message> errors, HttpStatus status) { | ||
super(status); | ||
this.errors = errors; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/mate/academy/bookstore/response/GeneralResponse.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,21 @@ | ||
package mate.academy.bookstore.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import java.util.Date; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public abstract class GeneralResponse { | ||
private boolean success; | ||
private int status; | ||
|
||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") | ||
private Date timestamp; | ||
|
||
public GeneralResponse(HttpStatus status) { | ||
this.timestamp = new Date(); | ||
this.status = status.value(); | ||
this.success = status.is2xxSuccessful(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/mate/academy/bookstore/response/ResponseHandler.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,32 @@ | ||
package mate.academy.bookstore.response; | ||
|
||
import java.util.List; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
public class ResponseHandler { | ||
public static ResponseEntity<ErrorResponse> getErrorResponse( | ||
String message, HttpStatus status) { | ||
return new ResponseEntity<>( | ||
new ErrorResponse(List.of(new ErrorResponse.Message(null, message)), status), | ||
status); | ||
} | ||
|
||
public static ResponseEntity<ErrorResponse> getErrorResponse( | ||
List<ErrorResponse.Message> messages, HttpStatus status) { | ||
return new ResponseEntity<>( | ||
new ErrorResponse(messages, status), | ||
status); | ||
} | ||
|
||
public static <T> ResponseEntity<SuccessResponse<T>> getSuccessResponse(T data) { | ||
return getSuccessResponse(data, HttpStatus.OK); | ||
} | ||
|
||
public static <T> ResponseEntity<SuccessResponse<T>> getSuccessResponse( | ||
T data, HttpStatus status) { | ||
return new ResponseEntity<>( | ||
new SuccessResponse<>(data, status), | ||
status); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/bookstore/response/SuccessResponse.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,14 @@ | ||
package mate.academy.bookstore.response; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class SuccessResponse<T> extends GeneralResponse { | ||
private T data; | ||
|
||
public SuccessResponse(T data, HttpStatus status) { | ||
super(status); | ||
this.data = data; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/mate/academy/bookstore/service/LocaleService.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,5 @@ | ||
package mate.academy.bookstore.service; | ||
|
||
public interface LocaleService { | ||
String getMessage(String code); | ||
} |
Oops, something went wrong.