Skip to content

Commit d4cac3d

Browse files
authored
Migration to the new error management system (#137)
Signed-off-by: benrejebmoh <mohamed.ben-rejeb@rte-france.com>
1 parent cc1a071 commit d4cac3d

File tree

12 files changed

+305
-176
lines changed

12 files changed

+305
-176
lines changed

src/main/java/com/powsybl/caseserver/CaseController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import com.powsybl.caseserver.dto.CaseInfos;
1010
import com.powsybl.caseserver.elasticsearch.CaseInfosService;
11+
import com.powsybl.caseserver.error.CaseRuntimeException;
1112
import com.powsybl.caseserver.service.CaseObserver;
1213
import com.powsybl.caseserver.service.MetadataService;
1314
import com.powsybl.caseserver.service.CaseService;
@@ -35,7 +36,6 @@
3536
import java.util.Optional;
3637
import java.util.UUID;
3738

38-
import static com.powsybl.caseserver.CaseException.createDirectoryNotFound;
3939
import static com.powsybl.caseserver.Utils.buildHeaders;
4040

4141
/**
@@ -90,7 +90,7 @@ public ResponseEntity<CaseInfos> getCaseInfos(@PathVariable("caseUuid") UUID cas
9090
public ResponseEntity<String> getCaseFormat(@PathVariable("caseUuid") UUID caseUuid) {
9191
LOGGER.debug("getCaseFormat request received");
9292
if (!caseService.caseExists(caseUuid)) {
93-
throw createDirectoryNotFound(caseUuid);
93+
throw CaseRuntimeException.directoryNotFound(caseUuid);
9494
}
9595
String caseFormat = caseService.getFormat(caseUuid);
9696
return ResponseEntity.ok().body(caseFormat);
@@ -101,7 +101,7 @@ public ResponseEntity<String> getCaseFormat(@PathVariable("caseUuid") UUID caseU
101101
public ResponseEntity<String> getCaseName(@PathVariable("caseUuid") UUID caseUuid) {
102102
LOGGER.debug("getCaseName request received");
103103
if (!caseService.caseExists(caseUuid)) {
104-
throw createDirectoryNotFound(caseUuid);
104+
throw CaseRuntimeException.directoryNotFound(caseUuid);
105105
}
106106
String caseName = caseService.getCaseName(caseUuid);
107107
return ResponseEntity.ok().body(caseName);
@@ -194,7 +194,7 @@ public ResponseEntity<Void> disableCaseExpiration(@PathVariable("caseUuid") UUID
194194
public ResponseEntity<Void> deleteCase(@PathVariable("caseUuid") UUID caseUuid) {
195195
LOGGER.debug("deleteCase request received with parameter caseUuid = {}", caseUuid);
196196
if (!caseService.caseExists(caseUuid)) {
197-
throw createDirectoryNotFound(caseUuid);
197+
throw CaseRuntimeException.directoryNotFound(caseUuid);
198198
}
199199
caseService.deleteCase(caseUuid);
200200
return ResponseEntity.ok().build();

src/main/java/com/powsybl/caseserver/CaseException.java

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2025, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package com.powsybl.caseserver;
8+
9+
import com.powsybl.ws.commons.error.ServerNameProvider;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.stereotype.Component;
12+
13+
/**
14+
* @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>}
15+
*/
16+
@Component
17+
public class PropertyServerNameProvider implements ServerNameProvider {
18+
19+
private final String name;
20+
21+
public PropertyServerNameProvider(@Value("${spring.application.name:powsybl-case-server}") String name) {
22+
this.name = name;
23+
}
24+
25+
@Override
26+
public String serverName() {
27+
return name;
28+
}
29+
}

src/main/java/com/powsybl/caseserver/RestResponseEntityExceptionHandler.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2026, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package com.powsybl.caseserver.error;
8+
9+
import com.powsybl.ws.commons.error.BusinessErrorCode;
10+
11+
/**
12+
* @author Mohamed Ben-rejeb <mohamed.ben-rejeb at rte-france.com>
13+
*/
14+
public enum CaseBusinessErrorCode implements BusinessErrorCode {
15+
NO_AVAILABLE_IMPORTER("case.noAvailableImporter"),
16+
ILLEGAL_FILE_NAME("case.illegalFileName");
17+
private final String code;
18+
CaseBusinessErrorCode(String code) {
19+
this.code = code;
20+
}
21+
22+
public String value() {
23+
return code;
24+
}
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (c) 2026, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package com.powsybl.caseserver.error;
8+
9+
import com.powsybl.ws.commons.error.AbstractBusinessException;
10+
import lombok.Getter;
11+
import lombok.NonNull;
12+
13+
import java.nio.file.Path;
14+
import java.util.Objects;
15+
16+
/**
17+
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
18+
* @author Mohamed Ben-rejeb <mohamed.ben-rejeb at rte-france.com>
19+
*/
20+
@Getter
21+
public final class CaseBusinessException extends AbstractBusinessException {
22+
23+
private final CaseBusinessErrorCode errorCode;
24+
25+
private CaseBusinessException(CaseBusinessErrorCode errorCode, String message) {
26+
super(Objects.requireNonNull(message, "message must not be null"));
27+
this.errorCode = Objects.requireNonNull(errorCode, "errorCode must not be null");
28+
}
29+
30+
public static CaseBusinessException createIllegalCaseName(String caseName) {
31+
Objects.requireNonNull(caseName);
32+
return new CaseBusinessException(CaseBusinessErrorCode.ILLEGAL_FILE_NAME, "This is not an acceptable case name: " + caseName);
33+
}
34+
35+
public static CaseBusinessException noAvailableImporter(Path file) {
36+
Objects.requireNonNull(file);
37+
return new CaseBusinessException(CaseBusinessErrorCode.NO_AVAILABLE_IMPORTER, "No available importer found for this file: " + file);
38+
}
39+
40+
@NonNull
41+
@Override
42+
public CaseBusinessErrorCode getBusinessErrorCode() {
43+
return errorCode;
44+
}
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) 2026, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package com.powsybl.caseserver.error;
8+
9+
import com.powsybl.caseserver.PropertyServerNameProvider;
10+
import com.powsybl.ws.commons.error.AbstractBusinessExceptionHandler;
11+
import com.powsybl.ws.commons.error.PowsyblWsProblemDetail;
12+
import jakarta.servlet.http.HttpServletRequest;
13+
import lombok.NonNull;
14+
import org.springframework.http.HttpStatus;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.web.bind.annotation.ControllerAdvice;
17+
import org.springframework.web.bind.annotation.ExceptionHandler;
18+
19+
/**
20+
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
21+
* @author Mohamed Ben-rejeb <mohamed.ben-rejeb at rte-france.com>
22+
*/
23+
@ControllerAdvice
24+
public class CaseExceptionHandler extends AbstractBusinessExceptionHandler<CaseBusinessException, CaseBusinessErrorCode> {
25+
protected CaseExceptionHandler(PropertyServerNameProvider serverNameProvider) {
26+
super(serverNameProvider);
27+
}
28+
29+
@Override
30+
protected @NonNull CaseBusinessErrorCode getBusinessCode(CaseBusinessException e) {
31+
return e.getBusinessErrorCode();
32+
}
33+
34+
@ExceptionHandler(CaseBusinessException.class)
35+
protected ResponseEntity<PowsyblWsProblemDetail> handleCaseBusinessException(
36+
CaseBusinessException exception, HttpServletRequest request) {
37+
return super.handleDomainException(exception, request);
38+
}
39+
40+
@Override
41+
protected HttpStatus mapStatus(CaseBusinessErrorCode errorCode) {
42+
return switch (errorCode) {
43+
case NO_AVAILABLE_IMPORTER, ILLEGAL_FILE_NAME -> HttpStatus.UNPROCESSABLE_ENTITY;
44+
};
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) 2026, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package com.powsybl.caseserver.error;
8+
9+
import java.nio.file.Path;
10+
import java.util.Objects;
11+
import java.util.UUID;
12+
13+
/**
14+
* @author Mohamed Ben-rejeb <mohamed.ben-rejeb at rte-france.com>
15+
*/
16+
public class CaseRuntimeException extends RuntimeException {
17+
18+
public CaseRuntimeException(String message) {
19+
super(message);
20+
}
21+
22+
public CaseRuntimeException(String message, Throwable cause) {
23+
super(message, cause);
24+
}
25+
26+
public static CaseRuntimeException directoryNotFound(UUID uuid) {
27+
Objects.requireNonNull(uuid);
28+
return new CaseRuntimeException("The directory with the following uuid doesn't exist: " + uuid);
29+
}
30+
31+
public static CaseRuntimeException originalFileNotFound(UUID uuid) {
32+
Objects.requireNonNull(uuid);
33+
return new CaseRuntimeException("The original file were not retrieved in the directory with the following uuid: " + uuid);
34+
}
35+
36+
public static CaseRuntimeException tempDirectoryCreation(UUID uuid, Exception e) {
37+
Objects.requireNonNull(uuid);
38+
return new CaseRuntimeException("Error creating temporary directory: " + uuid, e);
39+
}
40+
41+
public static CaseRuntimeException initTempFile(UUID uuid, Throwable e) {
42+
Objects.requireNonNull(uuid);
43+
return new CaseRuntimeException("Error initializing temporary case file: " + uuid, e);
44+
}
45+
46+
public static CaseRuntimeException fileNotImportable(Path file, Exception e) {
47+
Objects.requireNonNull(file);
48+
return new CaseRuntimeException("This file cannot be imported: " + file + " details: " + e.getMessage());
49+
}
50+
}

0 commit comments

Comments
 (0)