-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
4 changed files
with
65 additions
and
2 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
9 changes: 9 additions & 0 deletions
9
MainService/src/main/java/ru/espada/ep/iptip/exceptions/custom/ForbiddenException.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,9 @@ | ||
package ru.espada.ep.iptip.exceptions.custom; | ||
|
||
public class ForbiddenException extends RuntimeException { | ||
|
||
public ForbiddenException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...ervice/src/main/java/ru/espada/ep/iptip/exceptions/handlers/ForbiddenExceptionAdvice.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,39 @@ | ||
package ru.espada.ep.iptip.exceptions.handlers; | ||
|
||
import jakarta.validation.ConstraintViolationException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import ru.espada.ep.iptip.exceptions.custom.ForbiddenException; | ||
import ru.espada.ep.iptip.exceptions.custom.LocalizationException; | ||
import ru.espada.ep.iptip.exceptions.response.OperationStatus; | ||
import ru.espada.ep.iptip.localization.LocalizationService; | ||
|
||
@ControllerAdvice | ||
public class ForbiddenExceptionAdvice { | ||
|
||
private LocalizationService localizationService; | ||
|
||
@Autowired | ||
public void setLocalizationService(LocalizationService localizationService) { | ||
this.localizationService = localizationService; | ||
} | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<?> handleForbiddenException(ForbiddenException e) { | ||
String message = e.getMessage(); | ||
try { | ||
message = localizationService.getLocalizedMessage(message); | ||
} catch (LocalizationException ignored) { | ||
} | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body( | ||
OperationStatus.builder() | ||
.status(false) | ||
.message(message) | ||
.build() | ||
); | ||
} | ||
|
||
} |
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