-
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.
7. Обработка исключений в Spring REST [#505112]
- Loading branch information
1 parent
656b2ac
commit 0eb1888
Showing
4 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/ru/job4j/api/handler/GlobalExceptionHandler.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,26 @@ | ||
package ru.job4j.api.handler; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(DataIntegrityViolationException.class) | ||
public ResponseEntity<Object> handleDataIntegrityViolationException(Exception e, HttpServletRequest request) { | ||
Map<String, String> details = new HashMap<>(); | ||
details.put("message", e.getMessage()); | ||
details.put("type", String.valueOf(e.getClass())); | ||
details.put("timestamp", String.valueOf(LocalDateTime.now())); | ||
details.put("path", request.getRequestURI()); | ||
return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON).body(details); | ||
} | ||
} |
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 @@ | ||
--liquibase formatted sql | ||
--changeset vadimstrya:002_ddl_user_auth_email_unique | ||
|
||
alter table user_auth | ||
add constraint user_auth_email_unq unique (email); |
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