Skip to content

Commit 4097c7e

Browse files
committed
use Error push/pop and throw Errors over Exceptions
1 parent 2570e4d commit 4097c7e

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/EntryBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ protected void translate(SimpleField field, JsonNode node, Entry target) {
7979
String attributeName = fieldNameTranslator.translateFieldName(field.getFullPath());
8080

8181
if(LdapConstant.ATTRIBUTE_DN.equalsIgnoreCase(attributeName)){
82-
throw new IllegalArgumentException(
83-
"'dn' should not be included as it's value will be derived from the metadata.basedn and" +
84-
" the metadata.uniqueattr. Including the 'dn' as an insert attribute is confusing.");
82+
throw Error.get("Invalid Field Definition", "'" + LdapConstant.ATTRIBUTE_DN + "' should not be included as its value will be"
83+
+ " derived from the metadata.basedn and"
84+
+ " the metadata.uniqueattr. Including the '" + LdapConstant.ATTRIBUTE_DN
85+
+ "' as an insert attribute is confusing.");
8586
}
8687
else if(LightblueUtil.isFieldObjectType(attributeName)
8788
|| LightblueUtil.isFieldAnArrayCount(attributeName, getEntityMetadata().getFields())){

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/LdapCRUDController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
128128
Path uniqueFieldPath = fieldNameTranslator.translateAttributeName(store.getUniqueAttribute());
129129
JsonNode uniqueNode = document.get(uniqueFieldPath);
130130
if(uniqueNode == null){
131-
throw new IllegalArgumentException(store.getUniqueAttribute() + " is a required field");
131+
throw Error.get("Required Field", store.getUniqueAttribute());
132132
}
133133

134134
String dn = createDN(store, uniqueNode.asText());

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/TranslatorFromJson.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.redhat.lightblue.util.Error;
3737
import com.redhat.lightblue.util.JsonDoc;
3838
import com.redhat.lightblue.util.JsonNodeCursor;
39-
import com.redhat.lightblue.util.Path;
4039

4140
/**
4241
* Defines a class that translates lightblue json nodes into
@@ -95,12 +94,11 @@ public void translate(JsonDoc document, T target){
9594
* @param target - T
9695
*/
9796
protected void translate(JsonNodeCursor cursor, T target){
98-
Path path = cursor.getCurrentPath();
9997
JsonNode node = cursor.getCurrentNode();
100-
FieldTreeNode fieldNode = md.resolve(path);
98+
FieldTreeNode fieldNode = md.resolve(cursor.getCurrentPath());
10199

102100
if (fieldNode == null) {
103-
throw new NullPointerException("No Metadata field found for: " + path.toString());
101+
throw Error.get("Metadata Not Found", cursor.getCurrentPath().toString());
104102
}
105103

106104
Error.push(fieldNode.getFullPath().getLast());
@@ -118,7 +116,7 @@ else if (fieldNode instanceof ReferenceField) {
118116
translate((ReferenceField) fieldNode, node, target);
119117
}
120118
else{
121-
throw new UnsupportedOperationException("Field type is not supported: " + fieldNode.getClass().getName());
119+
throw Error.get("Unsupported Feature: " + fieldNode.getClass().getName(), fieldNode.getFullPath().toString());
122120
}
123121

124122
Error.pop();
@@ -143,7 +141,7 @@ else if(arrayElement instanceof ObjectArrayElement){
143141
translateObjectArray(field, cursor, target);
144142
}
145143
else{
146-
throw new UnsupportedOperationException("ArrayElement type is not supported: " + arrayElement.getClass().getName());
144+
throw Error.get("Unsupported Feature: " + arrayElement.getClass().getName(), field.getFullPath().toString());
147145
}
148146

149147
cursor.parent();

lightblue-ldap-crud/src/test/java/com/redhat/lightblue/crud/ldap/EntryBuilderTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import javax.xml.bind.DatatypeConverter;
3434

35+
import org.junit.After;
3536
import org.junit.Rule;
3637
import org.junit.Test;
3738
import org.junit.rules.ExpectedException;
@@ -49,6 +50,7 @@
4950
import com.redhat.lightblue.metadata.EntityMetadata;
5051
import com.redhat.lightblue.metadata.types.DateType;
5152
import com.redhat.lightblue.test.MetadataUtil;
53+
import com.redhat.lightblue.util.Error;
5254
import com.redhat.lightblue.util.JsonDoc;
5355
import com.unboundid.ldap.sdk.Entry;
5456
import com.unboundid.util.StaticUtils;
@@ -81,6 +83,11 @@ public static class SpecializedTests {
8183
@Rule
8284
public ExpectedException expectedEx = ExpectedException.none();
8385

86+
@After
87+
public void after(){
88+
Error.reset();
89+
}
90+
8491
@Test
8592
public void testFieldIsObjectType() throws Exception{
8693
Entry entry = buildEntry(LightblueUtil.FIELD_OBJECT_TYPE, "{\"type\": \"string\"}", quote("someEntity"));
@@ -134,8 +141,14 @@ public void testObjectArrayField_ThrowsException() throws Exception{
134141
"1," + quote(arrayFieldName) + ":[{\"someField\":\"hello\"}]");
135142
}
136143

137-
@Test(expected = IllegalArgumentException.class)
144+
/**
145+
* DN fields should never be defined as they are technically not attributes.
146+
*/
147+
@Test
138148
public void testFieldIsDN() throws Exception{
149+
expectedEx.expect(com.redhat.lightblue.util.Error.class);
150+
expectedEx.expectMessage("{\"objectType\":\"error\",\"context\":\"dn=uid=someuid,dc=example,dc=com/dn\",\"errorCode\":\"Invalid Field Definition\",\"msg\":\"'dn' should not be included as its value will be derived from the metadata.basedn and the metadata.uniqueattr. Including the 'dn' as an insert attribute is confusing.\"}");
151+
139152
buildEntry(LdapConstant.ATTRIBUTE_DN, "{\"type\": \"string\"}", quote("uid=someuid,dc=example,dc=com"));
140153
}
141154

@@ -167,6 +180,11 @@ public static Collection<Object[]> data() {
167180
});
168181
}
169182

183+
@After
184+
public void after(){
185+
Error.reset();
186+
}
187+
170188
private final String fieldName = "testfield";
171189
private final String metadataType;
172190
private final String crudValue;

0 commit comments

Comments
 (0)