diff --git a/pom.xml b/pom.xml index 4e23f46..7b5d373 100644 --- a/pom.xml +++ b/pom.xml @@ -123,6 +123,7 @@ org.gridsuite gridsuite-computation + 1.6.0-SNAPSHOT com.powsybl diff --git a/src/main/java/org/gridsuite/loadflow/server/PropertyServerNameProvider.java b/src/main/java/org/gridsuite/loadflow/server/PropertyServerNameProvider.java new file mode 100644 index 0000000..25f566f --- /dev/null +++ b/src/main/java/org/gridsuite/loadflow/server/PropertyServerNameProvider.java @@ -0,0 +1,28 @@ +/** + * 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.loadflow.server; + +import com.powsybl.ws.commons.error.ServerNameProvider; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * @author Hugo Marcellin + */ +@Component +public class PropertyServerNameProvider implements ServerNameProvider { + private final String name; + + public PropertyServerNameProvider(@Value("${spring.application.name:loadflow-server}") String name) { + this.name = name; + } + + @Override + public String serverName() { + return name; + } +} diff --git a/src/main/java/org/gridsuite/loadflow/server/RestResponseEntityExceptionHandler.java b/src/main/java/org/gridsuite/loadflow/server/RestResponseEntityExceptionHandler.java new file mode 100644 index 0000000..fec9015 --- /dev/null +++ b/src/main/java/org/gridsuite/loadflow/server/RestResponseEntityExceptionHandler.java @@ -0,0 +1,43 @@ +/** + * 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.loadflow.server; + +import com.powsybl.ws.commons.error.AbstractBaseRestExceptionHandler; +import com.powsybl.ws.commons.error.ServerNameProvider; +import lombok.NonNull; +import org.gridsuite.computation.ComputationBusinessErrorCode; +import org.gridsuite.computation.ComputationException; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; + +/** + * @author Hugo Marcellin + */ +@ControllerAdvice +public class RestResponseEntityExceptionHandler extends AbstractBaseRestExceptionHandler { + + protected RestResponseEntityExceptionHandler(ServerNameProvider serverNameProvider) { + super(serverNameProvider); + } + + @Override + protected @NonNull ComputationBusinessErrorCode getBusinessCode(ComputationException e) { + return e.getBusinessErrorCode(); + } + + @Override + protected HttpStatus mapStatus(ComputationBusinessErrorCode businessErrorCode) { + return switch (businessErrorCode) { + case RESULT_NOT_FOUND, NETWORK_NOT_FOUND, PARAMETERS_NOT_FOUND -> HttpStatus.NOT_FOUND; + case INVALID_FILTER_FORMAT, + INVALID_SORT_FORMAT, + INVALID_FILTER -> HttpStatus.BAD_REQUEST; + default -> HttpStatus.INTERNAL_SERVER_ERROR; + }; + } +} diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LimitReductionService.java b/src/main/java/org/gridsuite/loadflow/server/service/LimitReductionService.java index f7cb997..02e3f44 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LimitReductionService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LimitReductionService.java @@ -9,6 +9,7 @@ import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.Range; +import org.gridsuite.computation.ComputationBusinessErrorCode; import org.gridsuite.computation.ComputationException; import org.gridsuite.loadflow.server.dto.parameters.LimitReductionsByVoltageLevel; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -67,41 +68,41 @@ private List getLimitReductionsByD private void assertValidConfig(List> values) { if (voltageLevels.isEmpty()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "No configuration for voltage levels"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "No configuration for voltage levels"); } if (limitDurations.isEmpty()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "No configuration for limit durations"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "No configuration for limit durations"); } if (values.isEmpty() || values.get(0).isEmpty()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "No values provided"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "No values provided"); } int nbValuesByVl = values.get(0).size(); if (values.stream().anyMatch(valuesByVl -> valuesByVl.size() != nbValuesByVl)) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Number of values for a voltage level is incorrect"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Number of values for a voltage level is incorrect"); } if (voltageLevels.size() < values.size()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Too many values provided for voltage levels"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Too many values provided for voltage levels"); } if (voltageLevels.size() > values.size()) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Not enough values provided for voltage levels"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Not enough values provided for voltage levels"); } if (limitDurations.size() < nbValuesByVl - 1) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Too many values provided for limit durations"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Too many values provided for limit durations"); } if (limitDurations.size() > nbValuesByVl - 1) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Not enough values provided for limit durations"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Not enough values provided for limit durations"); } values.forEach(valuesByVl -> { if (valuesByVl.stream().anyMatch(v -> !Range.of(0.0, 1.0).contains(v))) { - throw new ComputationException(ComputationException.Type.LIMIT_REDUCTION_CONFIG_ERROR, "Value not between 0 and 1"); + throw new ComputationException(ComputationBusinessErrorCode.LIMIT_REDUCTION_CONFIG_ERROR, "Value not between 0 and 1"); } }); } diff --git a/src/test/java/org/gridsuite/loadflow/server/PropertyServerNameProviderTest.java b/src/test/java/org/gridsuite/loadflow/server/PropertyServerNameProviderTest.java new file mode 100644 index 0000000..f91f71b --- /dev/null +++ b/src/test/java/org/gridsuite/loadflow/server/PropertyServerNameProviderTest.java @@ -0,0 +1,22 @@ +/** + * 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.loadflow.server; + +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Mohamed Ben-rejeb {@literal } + */ +class PropertyServerNameProviderTest { + + @Test + void returnsProvidedName() { + PropertyServerNameProvider provider = new PropertyServerNameProvider("custom-server"); + assertThat(provider.serverName()).isEqualTo("custom-server"); + } +}