Skip to content

Commit bb3e626

Browse files
committed
fix(examples): enhance error handling and logging
Signed-off-by: Scott Hamrick <2623452+cshamrick@users.noreply.github.com>
1 parent c73e9ce commit bb3e626

File tree

9 files changed

+67
-65
lines changed

9 files changed

+67
-65
lines changed

examples/src/main/java/io/opentdf/platform/CreateAttribute.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Collections;
1414

1515
import java.util.Arrays;
16+
import java.util.Objects;
1617

1718
public class CreateAttribute {
1819

@@ -23,6 +24,7 @@ public static void main(String[] args) {
2324
String clientId = "opentdf";
2425
String clientSecret = "secret";
2526
String platformEndpoint = "localhost:8080";
27+
String namespaceName = "mynamespace.com";
2628

2729
SDKBuilder builder = new SDKBuilder();
2830

@@ -33,25 +35,17 @@ public static void main(String[] args) {
3335
.useInsecurePlaintextConnection(true)
3436
.build()) {
3537

36-
Namespace namespace;
37-
String namespaceName = "mynamespace.com";
38-
39-
try {
40-
namespace =
41-
ResponseMessageKt.getOrThrow(
42-
sdk.getServices()
43-
.namespaces()
44-
.getNamespaceBlocking(
45-
GetNamespaceRequest.newBuilder()
46-
.setFqn("https://" + namespaceName)
47-
.build(),
48-
Collections.emptyMap())
49-
.execute())
50-
.getNamespace();
51-
} catch (Exception e) {
52-
logger.error("Namespace '{}' not found", namespaceName);
53-
throw e;
54-
}
38+
Namespace namespace =
39+
ResponseMessageKt.getOrThrow(
40+
sdk.getServices()
41+
.namespaces()
42+
.getNamespaceBlocking(
43+
GetNamespaceRequest.newBuilder()
44+
.setFqn("https://" + namespaceName)
45+
.build(),
46+
Collections.emptyMap())
47+
.execute())
48+
.getNamespace();
5549

5650
CreateAttributeRequest createAttributeRequest =
5751
CreateAttributeRequest.newBuilder()
@@ -74,7 +68,13 @@ public static void main(String[] args) {
7468
"Successfully created attribute with ID: {}",
7569
createAttributeResponse.getAttribute().getId());
7670
} catch (Exception e) {
77-
logger.fatal("Failed to create attribute", e);
71+
if (Objects.equals(e.getMessage(), "resource not found")) {
72+
logger.error("Namespace '{}' not found", namespaceName, e);
73+
} else if (Objects.equals(e.getMessage(), "resource unique field violation")) {
74+
logger.error("Attribute already exists", e);
75+
} else {
76+
logger.error("Failed to create attribute", e);
77+
}
7878
}
7979
}
8080
}

examples/src/main/java/io/opentdf/platform/CreateNamespace.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.apache.logging.log4j.Logger;
99

1010
import java.util.Collections;
11+
import java.util.Objects;
1112

1213
public class CreateNamespace {
1314

@@ -18,6 +19,7 @@ public static void main(String[] args) {
1819
String clientId = "opentdf";
1920
String clientSecret = "secret";
2021
String platformEndpoint = "localhost:8080";
22+
String namespaceName = "mynamespace.com";
2123

2224
SDKBuilder builder = new SDKBuilder();
2325

@@ -29,7 +31,7 @@ public static void main(String[] args) {
2931
.build()) {
3032

3133
CreateNamespaceRequest createNamespaceRequest =
32-
CreateNamespaceRequest.newBuilder().setName("mynamespace.com").build();
34+
CreateNamespaceRequest.newBuilder().setName(namespaceName).build();
3335

3436
CreateNamespaceResponse createNamespaceResponse =
3537
ResponseMessageKt.getOrThrow(
@@ -43,7 +45,11 @@ public static void main(String[] args) {
4345
createNamespaceResponse.getNamespace().getId());
4446

4547
} catch (Exception e) {
46-
logger.fatal("Failed to create namespace", e);
48+
if (Objects.equals(e.getMessage(), "resource unique field violation")) {
49+
logger.error("Namespace '{}' already exists", namespaceName, e);
50+
} else {
51+
logger.error("Failed to create namespace", e);
52+
}
4753
}
4854
}
4955
}

examples/src/main/java/io/opentdf/platform/CreateSubjectConditionSet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ public static void main(String[] args) {
6060
.createSubjectConditionSetBlocking(
6161
createSubjectConditionSetRequest, Collections.emptyMap())
6262
.execute());
63+
6364
SubjectConditionSet subjectConditionSet =
6465
createSubjectConditionSetResponse.getSubjectConditionSet();
6566

6667
logger.info(
6768
"Successfully created subject condition set with ID: {}", subjectConditionSet.getId());
6869

6970
} catch (Exception e) {
70-
logger.fatal("Failed to create subject condition set", e);
71+
logger.error("Failed to create subject condition set", e);
7172
}
7273
}
7374
}

examples/src/main/java/io/opentdf/platform/CreateSubjectMapping.java

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.connectrpc.ResponseMessageKt;
44
import io.opentdf.platform.policy.Action;
55
import io.opentdf.platform.policy.Attribute;
6-
import io.opentdf.platform.policy.AttributeRuleTypeEnum;
76
import io.opentdf.platform.policy.Condition;
87
import io.opentdf.platform.policy.ConditionBooleanTypeEnum;
98
import io.opentdf.platform.policy.ConditionGroup;
@@ -12,8 +11,6 @@
1211
import io.opentdf.platform.policy.SubjectMapping;
1312
import io.opentdf.platform.policy.SubjectMappingOperatorEnum;
1413
import io.opentdf.platform.policy.SubjectSet;
15-
import io.opentdf.platform.policy.attributes.CreateAttributeRequest;
16-
import io.opentdf.platform.policy.attributes.CreateAttributeResponse;
1714
import io.opentdf.platform.policy.attributes.GetAttributeRequest;
1815
import io.opentdf.platform.policy.namespaces.GetNamespaceRequest;
1916
import io.opentdf.platform.policy.subjectmapping.CreateSubjectConditionSetRequest;
@@ -26,8 +23,8 @@
2623
import org.apache.logging.log4j.LogManager;
2724
import org.apache.logging.log4j.Logger;
2825

29-
import java.util.Arrays;
3026
import java.util.Collections;
27+
import java.util.Objects;
3128

3229
public class CreateSubjectMapping {
3330

@@ -37,6 +34,8 @@ public static void main(String[] args) {
3734
String clientId = "opentdf";
3835
String clientSecret = "secret";
3936
String platformEndpoint = "localhost:8080";
37+
String namespaceName = "mynamespace.com";
38+
String attributeName = "test-attribute";
4039

4140
SDKBuilder builder = new SDKBuilder();
4241
try (SDK sdk =
@@ -47,7 +46,6 @@ public static void main(String[] args) {
4746
.build()) {
4847

4948
Namespace namespace;
50-
String namespaceName = "mynamespace.com";
5149

5250
try {
5351
namespace =
@@ -62,12 +60,15 @@ public static void main(String[] args) {
6260
.execute())
6361
.getNamespace();
6462
} catch (Exception e) {
65-
logger.error("Namespace '{}' not found", namespaceName);
66-
throw e;
63+
if (Objects.equals(e.getMessage(), "resource not found")) {
64+
logger.error("Namespace '{}' not found", namespaceName, e);
65+
} else {
66+
logger.error("Failed to retrieve namespace '{}'", namespaceName, e);
67+
}
68+
return;
6769
}
6870

6971
Attribute attribute;
70-
String attributeName = "test-attribute";
7172
String attributeFqn = namespace.getFqn() + "/attr/" + attributeName;
7273

7374
try {
@@ -82,29 +83,13 @@ public static void main(String[] args) {
8283
.execute())
8384
.getAttribute();
8485

85-
logger.info("Found existing attribute with ID: {}", attribute.getId());
86-
8786
} catch (Exception e) {
88-
CreateAttributeRequest attributeRequest =
89-
CreateAttributeRequest.newBuilder()
90-
.setNamespaceId(namespace.getId())
91-
.setName(attributeName)
92-
.setRule(
93-
AttributeRuleTypeEnum.forNumber(
94-
AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE))
95-
.addAllValues(Arrays.asList("test1", "test2"))
96-
.build();
97-
98-
CreateAttributeResponse attributeResponse =
99-
ResponseMessageKt.getOrThrow(
100-
sdk.getServices()
101-
.attributes()
102-
.createAttributeBlocking(attributeRequest, Collections.emptyMap())
103-
.execute());
104-
105-
attribute = attributeResponse.getAttribute();
106-
107-
logger.info("Successfully created attribute with ID: {}", attribute.getId());
87+
if (Objects.equals(e.getMessage(), "resource not found")) {
88+
logger.error("Attribute '{}' not found", attributeFqn, e);
89+
} else {
90+
logger.error("Failed to retrieve attribute '{}'", attributeFqn, e);
91+
}
92+
return;
10893
}
10994

11095
CreateSubjectConditionSetRequest subjectConditionSetRequest =
@@ -155,7 +140,7 @@ public static void main(String[] args) {
155140

156141
logger.info("Successfully created subject mapping with ID: {}", subjectMapping.getId());
157142
} catch (Exception e) {
158-
logger.fatal("Failed to create subject mapping", e);
143+
logger.error("Failed to create subject mapping", e);
159144
}
160145
}
161146
}

examples/src/main/java/io/opentdf/platform/GetDecisions.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Collections;
1717

1818
import java.util.List;
19+
import java.util.stream.Collectors;
1920

2021
public class GetDecisions {
2122

@@ -64,9 +65,14 @@ public static void main(String[] args) {
6465

6566
List<DecisionResponse> decisions = getDecisionsResponse.getDecisionResponsesList();
6667

67-
logger.info("Successfully retrieved decision {}", decisions.get(0).getDecision());
68+
logger.info(
69+
"Successfully retrieved decisions: [{}]",
70+
decisions.stream()
71+
.map(DecisionResponse::getDecision)
72+
.map(DecisionResponse.Decision::toString)
73+
.collect(Collectors.joining(", ")));
6874
} catch (Exception e) {
69-
logger.fatal("Failed to get decisions", e);
75+
logger.error("Failed to get decisions", e);
7076
}
7177
}
7278
}

examples/src/main/java/io/opentdf/platform/GetEntitlements.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Collections;
1313

1414
import java.util.List;
15+
import java.util.stream.Collectors;
1516

1617
public class GetEntitlements {
1718
private static final Logger logger = LogManager.getLogger(GetEntitlements.class);
@@ -46,10 +47,13 @@ public static void main(String[] args) {
4647
List<EntityEntitlements> entitlements = getEntitlementsResponse.getEntitlementsList();
4748

4849
logger.info(
49-
"Successfully retrieved entitlements {}",
50-
entitlements.get(0).getAttributeValueFqnsList());
50+
"Successfully retrieved entitlements: [{}]",
51+
entitlements.stream()
52+
.map(EntityEntitlements::getAttributeValueFqnsList)
53+
.map(List::toString)
54+
.collect(Collectors.joining(", ")));
5155
} catch (Exception e) {
52-
logger.fatal("Failed to get entitlements", e);
56+
logger.error("Failed to get entitlements", e);
5357
}
5458
}
5559
}

examples/src/main/java/io/opentdf/platform/ListAttributes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public static void main(String[] args) {
4444
List<Attribute> attributes = listAttributesResponse.getAttributesList();
4545

4646
logger.info(
47-
"Successfully retrieved attributes {}",
47+
"Successfully retrieved attributes: [{}]",
4848
attributes.stream().map(Attribute::getFqn).collect(Collectors.joining(", ")));
4949
} catch (Exception e) {
50-
logger.fatal("Failed to list attributes", e);
50+
logger.error("Failed to list attributes", e);
5151
}
5252
}
5353
}

examples/src/main/java/io/opentdf/platform/ListNamespaces.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public static void main(String[] args) {
4242
List<Namespace> namespaces = listNamespacesResponse.getNamespacesList();
4343

4444
logger.info(
45-
"Successfully retrieved namespaces {}",
45+
"Successfully retrieved namespaces: [{}]",
4646
namespaces.stream().map(Namespace::getFqn).collect(Collectors.joining(", ")));
4747
} catch (Exception e) {
48-
logger.fatal("Failed to list namespaces", e);
48+
logger.error("Failed to list namespaces", e);
4949
}
5050
}
5151
}

examples/src/main/java/io/opentdf/platform/ListSubjectMappings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public static void main(String[] args) {
4444
List<SubjectMapping> subjectMappings = listSubjectMappingsResponse.getSubjectMappingsList();
4545

4646
logger.info(
47-
"Successfully retrieved subject mappings {}",
47+
"Successfully retrieved subject mappings: [{}]",
4848
subjectMappings.stream().map(SubjectMapping::getId).collect(Collectors.joining(", ")));
4949
} catch (Exception e) {
50-
logger.fatal("Failed to list subject mappings", e);
50+
logger.error("Failed to list subject mappings", e);
5151
}
5252
}
5353
}

0 commit comments

Comments
 (0)