-
Notifications
You must be signed in to change notification settings - Fork 0
Standardize exception handling #213
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6b5f16f
harmonise exceptions
benrejebmoh 165632b
simplify exception handling by delegating the work to powsybl-ws-commons
benrejebmoh 74fccc8
fix legacy unit tests and cover new code
benrejebmoh 981e9e9
fix license header
benrejebmoh 1092156
adapt to the replacement in powsybl-ws-commons of unnecessary Records…
benrejebmoh 5d62d66
adapt unit tests after changes in powsybl-ws-commons
benrejebmoh 1732aa2
revert because ignoring exception was done in purpose
benrejebmoh df42381
remove optional from business code
benrejebmoh 7937da9
refactor with shorter handlers in powsyble-ws-commons
benrejebmoh 0107aa6
regroup error handling classes into a separate package
benrejebmoh 1e44097
use DIRECTORY_SOME_ELEMENTS_ARE_MISSING for strict mode
benrejebmoh f812889
Update SNAPSHOT version to v2.25.0
gridsuite-actions[bot] 0d74ba1
Homogenize permissions checks (#215)
Meklo e589b7e
Update SNAPSHOT version to v2.26.0
gridsuite-actions[bot] 10c0356
Merge remote-tracking branch 'origin/main' into poc-harmonise-exceptions
TheMaskedTurtle 018ad47
fix: throw typed exception instead of runtime
TheMaskedTurtle 98df2d4
resolve build issue
benrejebmoh addc85a
upgrade powsybl-ws-commons to 1.30.0 and use spring.application.name …
benrejebmoh da9c6f4
Merge remote-tracking branch 'origin/main' into poc-harmonise-exceptions
benrejebmoh cf07bb2
resolve conflicts with main
benrejebmoh 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 hidden or 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
56 changes: 0 additions & 56 deletions
56
src/main/java/org/gridsuite/directory/server/DirectoryException.java
This file was deleted.
Oops, something went wrong.
311 changes: 155 additions & 156 deletions
311
src/main/java/org/gridsuite/directory/server/DirectoryService.java
Large diffs are not rendered by default.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/main/java/org/gridsuite/directory/server/PropertyServerNameProvider.java
This file contains hidden or 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,29 @@ | ||
| /** | ||
| * Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package org.gridsuite.directory.server; | ||
|
|
||
| import com.powsybl.ws.commons.error.ServerNameProvider; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>} | ||
| */ | ||
| @Component | ||
| public class PropertyServerNameProvider implements ServerNameProvider { | ||
|
|
||
| private final String name; | ||
|
|
||
| public PropertyServerNameProvider(@Value("${spring.application.name:directory-server}") String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public String serverName() { | ||
| return name; | ||
| } | ||
| } |
42 changes: 0 additions & 42 deletions
42
src/main/java/org/gridsuite/directory/server/RestResponseEntityExceptionHandler.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
src/main/java/org/gridsuite/directory/server/error/DirectoryBusinessErrorCode.java
This file contains hidden or 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,33 @@ | ||
| /** | ||
| * Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package org.gridsuite.directory.server.error; | ||
|
|
||
| import com.powsybl.ws.commons.error.BusinessErrorCode; | ||
|
|
||
| /** | ||
| * @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>} | ||
| * | ||
| * Business error codes emitted by the directory service. | ||
| */ | ||
| public enum DirectoryBusinessErrorCode implements BusinessErrorCode { | ||
| DIRECTORY_PERMISSION_DENIED("directory.permissionDenied"), | ||
| DIRECTORY_ELEMENT_NAME_BLANK("directory.elementNameBlank"), | ||
| DIRECTORY_NOT_DIRECTORY("directory.notDirectory"), | ||
| DIRECTORY_ELEMENT_NAME_CONFLICT("directory.elementNameConflict"), | ||
| DIRECTORY_MOVE_IN_DESCENDANT_NOT_ALLOWED("directory.moveInDescendantNotAllowed"), | ||
| DIRECTORY_SOME_ELEMENTS_ARE_MISSING("directory.someElementsAreMissing"), | ||
| DIRECTORY_ELEMENT_NOT_FOUND("directory.elementNotFound"); | ||
| private final String code; | ||
|
|
||
| DirectoryBusinessErrorCode(String code) { | ||
| this.code = code; | ||
| } | ||
|
|
||
| public String value() { | ||
| return code; | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
src/main/java/org/gridsuite/directory/server/error/DirectoryException.java
This file contains hidden or 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,49 @@ | ||
| /** | ||
| * Copyright (c) 2021, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package org.gridsuite.directory.server.error; | ||
|
|
||
| import com.powsybl.ws.commons.error.AbstractBusinessException; | ||
| import lombok.NonNull; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com> | ||
| * @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>} | ||
| */ | ||
| public class DirectoryException extends AbstractBusinessException { | ||
|
|
||
| private final DirectoryBusinessErrorCode errorCode; | ||
|
|
||
| public DirectoryException(DirectoryBusinessErrorCode errorCode, String message) { | ||
| super(Objects.requireNonNull(message, "message must not be null")); | ||
| this.errorCode = Objects.requireNonNull(errorCode, "errorCode must not be null"); | ||
| } | ||
|
|
||
| public static DirectoryException createElementNotFound(@NonNull String type, @NonNull UUID uuid) { | ||
| return new DirectoryException(DirectoryBusinessErrorCode.DIRECTORY_ELEMENT_NOT_FOUND, | ||
| String.format("%s '%s' not found !", type, uuid)); | ||
| } | ||
|
|
||
| public static DirectoryException createElementNameAlreadyExists(@NonNull String name) { | ||
| return new DirectoryException(DirectoryBusinessErrorCode.DIRECTORY_ELEMENT_NAME_CONFLICT, | ||
| String.format("Element with the same name '%s' already exists in the directory !", name)); | ||
| } | ||
|
|
||
| public static DirectoryException of(DirectoryBusinessErrorCode errorCode, String message, Object... args) { | ||
| return new DirectoryException(errorCode, args.length == 0 ? message : String.format(message, args)); | ||
| } | ||
|
|
||
| @NotNull | ||
| @Override | ||
| public DirectoryBusinessErrorCode getBusinessErrorCode() { | ||
| return errorCode; | ||
| } | ||
|
|
||
| } |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/gridsuite/directory/server/error/RestResponseEntityExceptionHandler.java
This file contains hidden or 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,45 @@ | ||
| /** | ||
| * Copyright (c) 2021, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package org.gridsuite.directory.server.error; | ||
|
|
||
| import com.powsybl.ws.commons.error.AbstractBaseRestExceptionHandler; | ||
| import com.powsybl.ws.commons.error.ServerNameProvider; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
|
|
||
| /** | ||
| * @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com> | ||
| * @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>} | ||
| */ | ||
| @ControllerAdvice | ||
| public class RestResponseEntityExceptionHandler | ||
| extends AbstractBaseRestExceptionHandler<DirectoryException, DirectoryBusinessErrorCode> { | ||
|
|
||
| public RestResponseEntityExceptionHandler(ServerNameProvider serverNameProvider) { | ||
| super(serverNameProvider); | ||
| } | ||
|
|
||
| @NotNull | ||
| @Override | ||
| protected DirectoryBusinessErrorCode getBusinessCode(DirectoryException ex) { | ||
| return ex.getBusinessErrorCode(); | ||
| } | ||
|
|
||
| @Override | ||
| protected HttpStatus mapStatus(DirectoryBusinessErrorCode errorCode) { | ||
| return switch (errorCode) { | ||
| case DIRECTORY_ELEMENT_NOT_FOUND, DIRECTORY_SOME_ELEMENTS_ARE_MISSING -> HttpStatus.NOT_FOUND; | ||
| case DIRECTORY_ELEMENT_NAME_CONFLICT -> HttpStatus.CONFLICT; | ||
| case DIRECTORY_PERMISSION_DENIED, | ||
| DIRECTORY_ELEMENT_NAME_BLANK, | ||
| DIRECTORY_NOT_DIRECTORY, | ||
| DIRECTORY_MOVE_IN_DESCENDANT_NOT_ALLOWED -> HttpStatus.FORBIDDEN; | ||
| }; | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
25 changes: 25 additions & 0 deletions
25
src/test/java/org/gridsuite/directory/server/DirectoryBusinessErrorCodeTest.java
This file contains hidden or 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,25 @@ | ||
| /** | ||
| * Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package org.gridsuite.directory.server; | ||
|
|
||
| import org.gridsuite.directory.server.error.DirectoryBusinessErrorCode; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.EnumSource; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>} | ||
| */ | ||
| class DirectoryBusinessErrorCodeTest { | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource(DirectoryBusinessErrorCode.class) | ||
| void valueMatchesEnumName(DirectoryBusinessErrorCode code) { | ||
| assertThat(code.value()).startsWith("directory."); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/test/java/org/gridsuite/directory/server/DirectoryExceptionTest.java
This file contains hidden or 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,38 @@ | ||
| /** | ||
| * Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package org.gridsuite.directory.server; | ||
|
|
||
| import org.gridsuite.directory.server.error.DirectoryBusinessErrorCode; | ||
| import org.gridsuite.directory.server.error.DirectoryException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>} | ||
| */ | ||
| class DirectoryExceptionTest { | ||
|
|
||
| @Test | ||
| void staticFactoriesProduceExpectedMessages() { | ||
| DirectoryException notFound = DirectoryException.createElementNotFound("Folder", UUID.fromString("123e4567-e89b-12d3-a456-426614174000")); | ||
| assertThat(notFound.getMessage()).contains("Folder"); | ||
| assertThat(notFound.getBusinessErrorCode()).isEqualTo(DirectoryBusinessErrorCode.DIRECTORY_ELEMENT_NOT_FOUND); | ||
|
|
||
| DirectoryException conflict = DirectoryException.createElementNameAlreadyExists("report"); | ||
| assertThat(conflict.getMessage()).contains("report"); | ||
| assertThat(conflict.getBusinessErrorCode()).isEqualTo(DirectoryBusinessErrorCode.DIRECTORY_ELEMENT_NAME_CONFLICT); | ||
|
|
||
| DirectoryException formatted = DirectoryException.of(DirectoryBusinessErrorCode.DIRECTORY_ELEMENT_NAME_BLANK, | ||
| "Element '%s' invalid", "x"); | ||
| assertThat(formatted.getMessage()).isEqualTo("Element 'x' invalid"); | ||
| assertThat(formatted.getBusinessErrorCode()).isEqualTo(DirectoryBusinessErrorCode.DIRECTORY_ELEMENT_NAME_BLANK); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.