Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-computation</artifactId>
<version>1.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
Expand Down
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.voltageinit.server;

import com.powsybl.ws.commons.error.ServerNameProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
@Component
public class PropertyServerNameProvider implements ServerNameProvider {

private final String name;

public PropertyServerNameProvider(@Value("${spring.application.name:voltage-init-server}") String name) {
this.name = name;
}

@Override
public String serverName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -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.voltageinit.server.error;

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 <hugo.marcelin at rte-france.com>
*/
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends AbstractBaseRestExceptionHandler<ComputationException, ComputationBusinessErrorCode> {

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;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.gridsuite.voltageinit.server.error;

import com.powsybl.ws.commons.error.BusinessErrorCode;

public enum VoltageInitBusinessErrorCode implements BusinessErrorCode {
MISSING_FILTER("voltageInit.missingFilter");

private final String code;

VoltageInitBusinessErrorCode(String code) {
this.code = code;
}

public String value() {
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@
*/
package org.gridsuite.voltageinit.server.error;

import com.powsybl.ws.commons.error.AbstractBusinessException;
import lombok.NonNull;

import java.util.Objects;

/**
* @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>}
*/
public class VoltageInitException extends RuntimeException {
public class VoltageInitException extends AbstractBusinessException {

private final VoltageInitBusinessErrorCode errorCode;

public VoltageInitException(String message) {
public VoltageInitException(VoltageInitBusinessErrorCode errorCode, String message) {
super(Objects.requireNonNull(message, "message must not be null"));
this.errorCode = Objects.requireNonNull(errorCode, "errorCode must not be null");
}

@Override
public @NonNull VoltageInitBusinessErrorCode getBusinessErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ protected Map<String, String> getSpecificMsgHeaders(ObjectMapper objectMapper) {
return specificMsgHeaders;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gridsuite.filter.utils.expertfilter.FieldType;
import org.gridsuite.filter.utils.expertfilter.OperatorType;
import org.gridsuite.voltageinit.server.dto.parameters.FilterEquipments;
import org.gridsuite.voltageinit.server.error.VoltageInitBusinessErrorCode;
import org.gridsuite.voltageinit.server.error.VoltageInitException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void ensureFiltersExist(Map<UUID, String> filterNamesByUuid) {
.filter(filterId -> !validFilters.contains(filterId))
.toList();
if (!missingFilters.isEmpty()) {
throw new VoltageInitException(buildMissingFiltersMessage(missingFilters, filterNamesByUuid));
throw new VoltageInitException(VoltageInitBusinessErrorCode.MISSING_FILTER, buildMissingFiltersMessage(missingFilters, filterNamesByUuid));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.voltageinit.server;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
class PropertyServerNameProviderTest {

@Test
void returnsProvidedName() {
PropertyServerNameProvider provider = new PropertyServerNameProvider("custom-server");
assertThat(provider.serverName()).isEqualTo("custom-server");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.voltageinit.server;

import org.gridsuite.voltageinit.server.error.VoltageInitBusinessErrorCode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.gridsuite.voltageinit.utils.assertions.Assertions.assertThat;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
class VoltageInitBusinessErrorCodeTest {
@ParameterizedTest
@EnumSource(VoltageInitBusinessErrorCode.class)
void valueMatchesEnumName(VoltageInitBusinessErrorCode code) {
assertThat(code.value()).startsWith("voltageInit.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.gridsuite.voltageinit.server.entities.parameters.FilterEquipmentsEmbeddable;
import org.gridsuite.voltageinit.server.entities.parameters.VoltageInitParametersEntity;
import org.gridsuite.voltageinit.server.entities.parameters.VoltageLimitEntity;
import org.gridsuite.voltageinit.server.error.VoltageInitBusinessErrorCode;
import org.gridsuite.voltageinit.server.error.VoltageInitException;
import org.gridsuite.voltageinit.server.service.VoltageInitRunContext;
import org.gridsuite.voltageinit.server.util.EquipmentsSelectionType;
Expand Down Expand Up @@ -211,7 +212,7 @@ void buildOpenReacParametersThrowsWhenFilterMissing() {
.withResourceBundles("i18n.reports")
.withMessageTemplate(COMPUTATION_TYPE).build());

Mockito.doThrow(new VoltageInitException(FilterService.FILTERS_NOT_FOUND + " [" + FILTER_1 + "]"))
Mockito.doThrow(new VoltageInitException(VoltageInitBusinessErrorCode.MISSING_FILTER, FilterService.FILTERS_NOT_FOUND + " [" + FILTER_1 + "]"))
.when(filterService)
.ensureFiltersExist(Mockito.anyMap());

Expand Down
Loading