Skip to content

Commit 652835b

Browse files
committed
rework exception
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
1 parent c661668 commit 652835b

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/persistence/PersistenceResource.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.openhab.core.io.rest.RESTConstants;
5353
import org.openhab.core.io.rest.RESTResource;
5454
import org.openhab.core.io.rest.core.config.ConfigurationService;
55-
import org.openhab.core.io.rest.core.persistence.PersistenceItemNotFoundException;
5655
import org.openhab.core.items.Item;
5756
import org.openhab.core.items.ItemNotFoundException;
5857
import org.openhab.core.items.ItemRegistry;
@@ -65,6 +64,7 @@
6564
import org.openhab.core.persistence.ModifiablePersistenceService;
6665
import org.openhab.core.persistence.PersistenceItemConfiguration;
6766
import org.openhab.core.persistence.PersistenceItemInfo;
67+
import org.openhab.core.persistence.PersistenceItemNotFoundException;
6868
import org.openhab.core.persistence.PersistenceManager;
6969
import org.openhab.core.persistence.PersistenceService;
7070
import org.openhab.core.persistence.PersistenceServiceProblem;
@@ -625,11 +625,15 @@ private List<PersistenceServiceDTO> getPersistenceServiceList(Locale locale) {
625625

626626
private Response getServiceItemListDTO(@Nullable String serviceId, @Nullable String itemName) {
627627
// If serviceId is null, then use the default service
628-
PersistenceService service;
629628
String effectiveServiceId = serviceId != null ? serviceId : persistenceServiceRegistry.getDefaultId();
630-
service = effectiveServiceId != null ? persistenceServiceRegistry.get(effectiveServiceId) : null;
631629

632-
if (effectiveServiceId == null || service == null) {
630+
if (effectiveServiceId == null) {
631+
logger.debug("No default persistence service.");
632+
return JSONResponse.createErrorResponse(Status.NOT_FOUND, "No default persistence service.");
633+
}
634+
635+
PersistenceService service = persistenceServiceRegistry.get(effectiveServiceId);
636+
if (service == null) {
633637
logger.debug("Persistence service not found '{}'.", effectiveServiceId);
634638
return JSONResponse.createErrorResponse(Status.NOT_FOUND,
635639
"Persistence service not found: " + effectiveServiceId);
@@ -666,7 +670,7 @@ protected Set<PersistenceItemInfoDTO> createDTO(QueryablePersistenceService qSer
666670
String alias = itemToAlias.get(itemName);
667671
PersistenceItemInfo singleItemInfo = qService.getItemInfo(itemName, alias);
668672
if (singleItemInfo == null) {
669-
throw new PersistenceItemNotFoundException(itemName, serviceId);
673+
throw new PersistenceItemNotFoundException(serviceId, itemName, alias);
670674
}
671675
itemInfo = Set.of(singleItemInfo);
672676
} else {

bundles/org.openhab.core.io.rest.core/src/test/java/org/openhab/core/io/rest/core/internal/persistence/PersistenceResourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.openhab.core.io.rest.LocaleService;
4545
import org.openhab.core.io.rest.core.config.ConfigurationService;
4646
import org.openhab.core.io.rest.core.internal.persistence.PersistenceResource.PersistenceItemInfoDTO;
47-
import org.openhab.core.io.rest.core.persistence.PersistenceItemNotFoundException;
4847
import org.openhab.core.items.Item;
4948
import org.openhab.core.items.ItemNotFoundException;
5049
import org.openhab.core.items.ItemRegistry;
@@ -54,6 +53,7 @@
5453
import org.openhab.core.persistence.HistoricItem;
5554
import org.openhab.core.persistence.ModifiablePersistenceService;
5655
import org.openhab.core.persistence.PersistenceItemInfo;
56+
import org.openhab.core.persistence.PersistenceItemNotFoundException;
5757
import org.openhab.core.persistence.PersistenceServiceRegistry;
5858
import org.openhab.core.persistence.dto.ItemHistoryDTO;
5959
import org.openhab.core.persistence.dto.ItemHistoryDTO.HistoryDataBean;

bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/persistence/PersistenceItemNotFoundException.java renamed to bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/PersistenceItemNotFoundException.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@
1010
*
1111
* SPDX-License-Identifier: EPL-2.0
1212
*/
13-
package org.openhab.core.io.rest.core.persistence;
13+
package org.openhab.core.persistence;
1414

1515
import java.io.Serial;
1616

1717
import org.eclipse.jdt.annotation.NonNullByDefault;
18+
import org.eclipse.jdt.annotation.Nullable;
1819

1920
/**
20-
* This exception is thrown by the {@link PersistenceResource} if an item could not be found in a persistence service.
21+
* This exception is thrown if an item cannot be found in a persistence service.
2122
*
2223
* @author Mark Herwege - Initial contribution
2324
*/
2425
@NonNullByDefault
2526
public class PersistenceItemNotFoundException extends Exception {
2627

27-
public PersistenceItemNotFoundException(String name, String serviceId) {
28-
super("Item '" + name + "' could not be found in persistence service '" + serviceId + "'");
28+
public PersistenceItemNotFoundException(String serviceId, String name) {
29+
this(serviceId, name, null);
30+
}
31+
32+
public PersistenceItemNotFoundException(String serviceId, String name, @Nullable String alias) {
33+
super("Item '" + name + "' " + (alias != null ? "with alias '" + alias + "' " : "")
34+
+ "could not be found in persistence service '" + serviceId + "'");
2935
}
3036

3137
@Serial

0 commit comments

Comments
 (0)