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
63 changes: 0 additions & 63 deletions src/main/java/org/gridsuite/computation/ComputationException.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.error;

import com.powsybl.ws.commons.error.BusinessErrorCode;
import com.powsybl.ws.commons.error.ServerNameProvider;
import org.springframework.http.HttpStatus;

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

public abstract class AbstractTypedComputationRestResponseEntityExceptionHandler<C extends BusinessErrorCode> extends ComputationRestResponseEntityExceptionHandler {
private final Class<C> specificComputationBusinessErrorCode;

protected AbstractTypedComputationRestResponseEntityExceptionHandler(ServerNameProvider serverNameProvider, Class<C> specificComputationBusinessErrorCode) {
super(serverNameProvider);
this.specificComputationBusinessErrorCode = specificComputationBusinessErrorCode;
}

@Override
protected HttpStatus mapStatus(BusinessErrorCode businessErrorCode) {
if (businessErrorCode instanceof ComputationBusinessErrorCode computationCode) {
return super.mapStatus(computationCode);
} else if (specificComputationBusinessErrorCode.isInstance(businessErrorCode)) {
return mapSpecificStatus(specificComputationBusinessErrorCode.cast(businessErrorCode));
}
return HttpStatus.INTERNAL_SERVER_ERROR;
}

protected abstract HttpStatus mapSpecificStatus(C code);
}
Original file line number Diff line number Diff line change
@@ -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.computation.error;

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

public enum ComputationBusinessErrorCode implements BusinessErrorCode {
RESULT_NOT_FOUND("computation.resultNotFound"),
PARAMETERS_NOT_FOUND("computation.parametersNotFound"),
INVALID_SORT_FORMAT("computation.invalidSortFormat"),
INVALID_EXPORT_PARAMS("computation.invalidExportParams"),
LIMIT_REDUCTION_CONFIG_ERROR("computation.limitReductionConfigError"),
RUNNER_ERROR("computation.runnerError");

private final String code;

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

public String value() {
return code;
}
}
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.computation.error;

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

import java.util.Objects;

/**
* @author Anis Touri <anis.touri at rte-france.com>
*/
@Getter
public class ComputationException extends AbstractBusinessException {

private final ComputationBusinessErrorCode errorCode;

@NonNull
@Override
public ComputationBusinessErrorCode getBusinessErrorCode() {
return errorCode;
}

public ComputationException(ComputationBusinessErrorCode errorCode, String message, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
}

public ComputationException(ComputationBusinessErrorCode errorCode, String message) {
super(message);
this.errorCode = Objects.requireNonNull(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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.error;

import com.powsybl.ws.commons.error.AbstractBaseRestExceptionHandler;
import com.powsybl.ws.commons.error.AbstractBusinessException;
import com.powsybl.ws.commons.error.BusinessErrorCode;
import com.powsybl.ws.commons.error.ServerNameProvider;
import lombok.NonNull;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;

import static org.gridsuite.computation.error.ComputationBusinessErrorCode.INVALID_EXPORT_PARAMS;
import static org.gridsuite.computation.error.ComputationBusinessErrorCode.INVALID_SORT_FORMAT;
import static org.gridsuite.computation.error.ComputationBusinessErrorCode.PARAMETERS_NOT_FOUND;
import static org.gridsuite.computation.error.ComputationBusinessErrorCode.RESULT_NOT_FOUND;

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

@ControllerAdvice
public class ComputationRestResponseEntityExceptionHandler extends AbstractBaseRestExceptionHandler<AbstractBusinessException, BusinessErrorCode> {

protected ComputationRestResponseEntityExceptionHandler(ServerNameProvider serverNameProvider) {
super(serverNameProvider);
}

@Override
protected @NonNull BusinessErrorCode getBusinessCode(AbstractBusinessException e) {
return e.getBusinessErrorCode();
}

@Override
protected HttpStatus mapStatus(BusinessErrorCode businessErrorCode) {
return switch (businessErrorCode) {
case RESULT_NOT_FOUND, PARAMETERS_NOT_FOUND -> HttpStatus.NOT_FOUND;
case INVALID_SORT_FORMAT, INVALID_EXPORT_PARAMS -> HttpStatus.BAD_REQUEST;
default -> HttpStatus.INTERNAL_SERVER_ERROR;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import com.powsybl.network.store.client.PreloadingStrategy;
import com.powsybl.ws.commons.ZipUtils;
import org.apache.commons.lang3.StringUtils;
import org.gridsuite.computation.ComputationException;
import org.gridsuite.computation.error.ComputationBusinessErrorCode;
import org.gridsuite.computation.error.ComputationException;
import org.gridsuite.computation.s3.ComputationS3Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -180,7 +181,7 @@ public Consumer<Message<String>> consumeRun() {
} catch (Exception e) {
resultService.delete(resultContext.getResultUuid());
this.handleNonCancellationException(resultContext, e, rootReporter);
throw new ComputationException(String.format("%s: %s", NotificationService.getFailedMessage(getComputationType()), e.getMessage()), e.getCause());
throw new ComputationException(ComputationBusinessErrorCode.RUNNER_ERROR, String.format("%s: %s", NotificationService.getFailedMessage(getComputationType()), e.getMessage()), e.getCause());
} finally {
if (Boolean.TRUE.equals(resultContext.getRunContext().getDebug())) {
processDebug(resultContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.gridsuite.computation.ComputationException;
import org.gridsuite.computation.dto.GlobalFilter;
import org.gridsuite.computation.dto.ResourceFilterDTO;

import java.io.UncheckedIOException;
import java.util.List;

/**
Expand All @@ -33,7 +33,7 @@ private static <T> 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 UncheckedIOException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.gridsuite.computation.error.ComputationBusinessErrorCode;
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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
*/
package org.gridsuite.computation;

import org.gridsuite.computation.error.ComputationException;
import org.junit.jupiter.api.Test;

import static org.gridsuite.computation.error.ComputationBusinessErrorCode.PARAMETERS_NOT_FOUND;
import static org.gridsuite.computation.error.ComputationBusinessErrorCode.RUNNER_ERROR;
import static org.junit.jupiter.api.Assertions.*;

/**
Expand All @@ -16,16 +19,18 @@
class ComputationExceptionTest {

@Test
void testMessageConstructor() {
var e = new ComputationException("test");
void testMessageAndThrowableConstructor() {
var cause = new RuntimeException("test");
var e = new ComputationException(RUNNER_ERROR, "test", cause);
assertEquals(RUNNER_ERROR, e.getBusinessErrorCode());
assertEquals("test", e.getMessage());
assertEquals(cause, e.getCause());
}

@Test
void testMessageAndThrowableConstructor() {
var cause = new RuntimeException("test");
var e = new ComputationException("test", cause);
void testBusinessErrorCodeConstructor() {
var e = new ComputationException(PARAMETERS_NOT_FOUND, "test");
assertEquals("test", e.getMessage());
assertEquals(cause, e.getCause());
assertEquals(PARAMETERS_NOT_FOUND, e.getBusinessErrorCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.WithAssertions;
import org.gridsuite.computation.dto.ReportInfos;
import org.gridsuite.computation.error.ComputationException;
import org.gridsuite.computation.s3.ComputationS3Service;
import org.gridsuite.computation.s3.S3InputStreamInfos;
import org.gridsuite.computation.service.*;
Expand Down
Loading