diff --git a/pom.xml b/pom.xml index e91cd0d..a5e3e02 100644 --- a/pom.xml +++ b/pom.xml @@ -43,7 +43,7 @@ 2.25.0 - 1.28.1 + 1.30.0 1.6.0 diff --git a/src/main/java/org/gridsuite/computation/ComputationBusinessErrorCode.java b/src/main/java/org/gridsuite/computation/ComputationBusinessErrorCode.java new file mode 100644 index 0000000..cb78a28 --- /dev/null +++ b/src/main/java/org/gridsuite/computation/ComputationBusinessErrorCode.java @@ -0,0 +1,32 @@ +/** + * 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.computation; + +import com.powsybl.ws.commons.error.BusinessErrorCode; + +public enum ComputationBusinessErrorCode implements BusinessErrorCode { + RESULT_NOT_FOUND("computation.resultNotFound"), + INVALID_FILTER_FORMAT("computation.invalidFilterFormat"), + INVALID_SORT_FORMAT("computation.invalidSortFormat"), + INVALID_FILTER("computation.invalidFilter"), + NETWORK_NOT_FOUND("computation.networkNotFound"), + PARAMETERS_NOT_FOUND("computation.parametersNotFound"), + FILE_EXPORT_ERROR("computation.fileExportError"), + EVALUATE_FILTER_FAILED("computation.evaluateFilterFailed"), + LIMIT_REDUCTION_CONFIG_ERROR("computation.limitReductionConfigError"), + SPECIFIC("computation.specific"),; + + private final String code; + + ComputationBusinessErrorCode(String code) { + this.code = code; + } + + public String value() { + return code; + } +} diff --git a/src/main/java/org/gridsuite/computation/ComputationException.java b/src/main/java/org/gridsuite/computation/ComputationException.java index c19157f..07675bc 100644 --- a/src/main/java/org/gridsuite/computation/ComputationException.java +++ b/src/main/java/org/gridsuite/computation/ComputationException.java @@ -6,7 +6,9 @@ */ package org.gridsuite.computation; +import com.powsybl.ws.commons.error.AbstractBusinessException; import lombok.Getter; +import lombok.NonNull; import java.util.Objects; @@ -14,50 +16,28 @@ * @author Anis Touri */ @Getter -public class ComputationException extends RuntimeException { - public enum Type { - RESULT_NOT_FOUND("Result not found."), - INVALID_FILTER_FORMAT("The filter format is invalid."), - INVALID_SORT_FORMAT("The sort format is invalid"), - INVALID_FILTER("Invalid filter"), - NETWORK_NOT_FOUND("Network not found"), - PARAMETERS_NOT_FOUND("Computation parameters not found"), - FILE_EXPORT_ERROR("Error exporting the file"), - EVALUATE_FILTER_FAILED("Error evaluating the file"), - LIMIT_REDUCTION_CONFIG_ERROR("Error int the configuration of the limit reduction"), - SPECIFIC("Unknown error during the computation"); - - private final String defaultMessage; - - Type(String defaultMessage) { - this.defaultMessage = defaultMessage; - } - } - - private final Type exceptionType; +public class ComputationException extends AbstractBusinessException { - public ComputationException(Type exceptionType) { - super(Objects.requireNonNull(exceptionType.defaultMessage)); - this.exceptionType = Objects.requireNonNull(exceptionType); - } + private final ComputationBusinessErrorCode errorCode; public ComputationException(String message) { super(message); - this.exceptionType = Type.SPECIFIC; + this.errorCode = ComputationBusinessErrorCode.SPECIFIC; } public ComputationException(String message, Throwable cause) { super(message, cause); - this.exceptionType = Type.SPECIFIC; + this.errorCode = ComputationBusinessErrorCode.SPECIFIC; } - public ComputationException(Type exceptionType, String message) { - super(message); - this.exceptionType = Objects.requireNonNull(exceptionType); + @NonNull + @Override + public ComputationBusinessErrorCode getBusinessErrorCode() { + return errorCode; } - public ComputationException(Type exceptionType, String message, Throwable cause) { - super(message, cause); - this.exceptionType = Objects.requireNonNull(exceptionType); + public ComputationException(ComputationBusinessErrorCode errorCode, String message) { + super(message); + this.errorCode = Objects.requireNonNull(errorCode); } } diff --git a/src/main/java/org/gridsuite/computation/utils/FilterUtils.java b/src/main/java/org/gridsuite/computation/utils/FilterUtils.java index b503df5..300cd60 100644 --- a/src/main/java/org/gridsuite/computation/utils/FilterUtils.java +++ b/src/main/java/org/gridsuite/computation/utils/FilterUtils.java @@ -16,6 +16,8 @@ import java.util.List; +import static org.gridsuite.computation.ComputationBusinessErrorCode.INVALID_FILTER_FORMAT; + /** * @author maissa Souissi */ @@ -32,7 +34,7 @@ private static T fromStringToDTO(String jsonString, ObjectMapper objectMappe try { return objectMapper.readValue(jsonString, typeReference); } catch (JsonProcessingException e) { - throw new ComputationException(ComputationException.Type.INVALID_FILTER_FORMAT); + throw new ComputationException(INVALID_FILTER_FORMAT, e.getMessage()); } } diff --git a/src/test/java/org/gridsuite/computation/ComputationBusinessErrorCodeTest.java b/src/test/java/org/gridsuite/computation/ComputationBusinessErrorCodeTest.java new file mode 100644 index 0000000..7fb2d3c --- /dev/null +++ b/src/test/java/org/gridsuite/computation/ComputationBusinessErrorCodeTest.java @@ -0,0 +1,20 @@ +/** + * 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.computation; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class ComputationBusinessErrorCodeTest { + @ParameterizedTest + @EnumSource(ComputationBusinessErrorCode.class) + void valueMatchesEnumName(ComputationBusinessErrorCode code) { + assertThat(code.value()).startsWith("computation."); + } +} diff --git a/src/test/java/org/gridsuite/computation/ComputationExceptionTest.java b/src/test/java/org/gridsuite/computation/ComputationExceptionTest.java index fb9b670..6528b9a 100644 --- a/src/test/java/org/gridsuite/computation/ComputationExceptionTest.java +++ b/src/test/java/org/gridsuite/computation/ComputationExceptionTest.java @@ -19,6 +19,7 @@ class ComputationExceptionTest { void testMessageConstructor() { var e = new ComputationException("test"); assertEquals("test", e.getMessage()); + assertEquals(ComputationBusinessErrorCode.SPECIFIC, e.getBusinessErrorCode()); } @Test @@ -28,4 +29,11 @@ void testMessageAndThrowableConstructor() { assertEquals("test", e.getMessage()); assertEquals(cause, e.getCause()); } + + @Test + void testBusinessErrorCodeConstructor() { + var e = new ComputationException(ComputationBusinessErrorCode.PARAMETERS_NOT_FOUND, "test"); + assertEquals("test", e.getMessage()); + assertEquals(ComputationBusinessErrorCode.PARAMETERS_NOT_FOUND, e.getBusinessErrorCode()); + } }