Skip to content

Commit

Permalink
improve error message with readable HugeType (#546)
Browse files Browse the repository at this point in the history
Change-Id: I633eee63fe9458cdc910f977fccec8fd0109f1d2
  • Loading branch information
javeme authored and zhoney committed Jun 3, 2019
1 parent abebfac commit b63277e
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 22 deletions.
3 changes: 2 additions & 1 deletion hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ protected static void checkExist(Iterator<?> iter,
String id) {
if (!iter.hasNext()) {
throw new NotFoundException(String.format(
"%s with id '%s' does not exist", type, id));
"%s with id '%s' does not exist",
type.readableName(), id));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public Iterator<Edge> adjacentEdges(Id vertexId) {

public PropertyKey propertyKey(Id id) {
PropertyKey pk = this.schemaTransaction().getPropertyKey(id);
E.checkArgument(pk != null, "Undefined property key id: '%s'", id);
E.checkArgument(pk != null, "Undefined property key with id: '%s'", id);
return pk;
}

Expand All @@ -423,7 +423,7 @@ public PropertyKey propertyKey(String name) {

public VertexLabel vertexLabel(Id id) {
VertexLabel vl = this.schemaTransaction().getVertexLabel(id);
E.checkArgument(vl != null, "Undefined vertex label id: '%s'", id);
E.checkArgument(vl != null, "Undefined vertex label with id: '%s'", id);
return vl;
}

Expand All @@ -435,7 +435,7 @@ public VertexLabel vertexLabel(String name) {

public EdgeLabel edgeLabel(Id id) {
EdgeLabel el = this.schemaTransaction().getEdgeLabel(id);
E.checkArgument(el != null, "Undefined edge label id: '%s'", id);
E.checkArgument(el != null, "Undefined edge label with id: '%s'", id);
return el;
}

Expand All @@ -447,7 +447,7 @@ public EdgeLabel edgeLabel(String name) {

public IndexLabel indexLabel(Id id) {
IndexLabel il = this.schemaTransaction().getIndexLabel(id);
E.checkArgument(il != null, "Undefined index label id: '%s'", id);
E.checkArgument(il != null, "Undefined index label with id: '%s'", id);
return il;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public BackendEntry get(HugeType type, Id id) {
BackendEntry entry = this.query(type, id);
if (entry == null) {
throw new NotFoundException(
"Not found the %s entry with id '%s'", type, id);
"Not found the %s entry with id '%s'",
type.readableName(), id);
}
return entry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ protected void addSchema(SchemaElement schema) {
}

protected <T extends SchemaElement> T getSchema(HugeType type, Id id) {
LOG.debug("SchemaTransaction get {} by id '{}'", type, id);
LOG.debug("SchemaTransaction get {} by id '{}'",
type.readableName(), id);
this.beforeRead();
BackendEntry entry = this.query(type, id);
if (entry == null) {
Expand All @@ -300,7 +301,8 @@ protected <T extends SchemaElement> T getSchema(HugeType type, Id id) {
*/
protected <T extends SchemaElement> T getSchema(HugeType type,
String name) {
LOG.debug("SchemaTransaction get {} by name '{}'", type, name);
LOG.debug("SchemaTransaction get {} by name '{}'",
type.readableName(), name);
this.beforeRead();
ConditionQuery query = new ConditionQuery(type);
query.eq(HugeKeys.NAME, name);
Expand Down Expand Up @@ -436,7 +438,7 @@ public void checkIdIfRestoringMode(HugeType type, Id id) {
"Must provide schema id if in RESTORING mode");
SchemaElement element = this.getSchema(type, id);
if (element != null) {
throw new ExistedException(type.toString() + " id", id);
throw new ExistedException(type.readableName() + " id", id);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
package com.baidu.hugegraph.exception;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.type.HugeType;

public class ExistedException extends HugeException {

private static final long serialVersionUID = 5152465646323494840L;

public ExistedException(HugeType type, Object arg) {
this(type.readableName(), arg);
}

public ExistedException(String type, Object arg) {
super("The %s '%s' has existed", type, arg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public List<IndexLabel> getIndexLabels() {
private static void checkExists(HugeType type, Object object, String name) {
if (object == null) {
throw new NotFoundException("%s with name '%s' does not exist",
type, name);
type.readableName(), name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ public EdgeLabel build() {

@Override
public EdgeLabel create() {
HugeType type = HugeType.EDGE_LABEL;
SchemaTransaction tx = this.transaction;
SchemaElement.checkName(this.name, tx.graph().configuration());
EdgeLabel edgeLabel = tx.getEdgeLabel(this.name);
if (edgeLabel != null) {
if (this.checkExist) {
throw new ExistedException("edge label", this.name);
throw new ExistedException(type, this.name);
}
return edgeLabel;
}
tx.checkIdIfRestoringMode(HugeType.EDGE_LABEL, this.id);
tx.checkIdIfRestoringMode(type, this.id);

if (this.frequency == Frequency.DEFAULT) {
this.frequency = Frequency.SINGLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ public IndexLabel build() {
*/
@Override
public IndexLabel.CreatedIndexLabel createWithTask() {
HugeType type = HugeType.INDEX_LABEL;
SchemaTransaction tx = this.transaction;
SchemaElement.checkName(this.name, tx.graph().configuration());
IndexLabel indexLabel = tx.getIndexLabel(this.name);
if (indexLabel != null) {
if (this.checkExist) {
throw new ExistedException("index label", this.name);
throw new ExistedException(type, this.name);
}
return new IndexLabel.CreatedIndexLabel(indexLabel, null);
}
tx.checkIdIfRestoringMode(HugeType.INDEX_LABEL, this.id);
tx.checkIdIfRestoringMode(type, this.id);

SchemaLabel schemaLabel = this.loadElement();

Expand Down Expand Up @@ -290,7 +291,7 @@ private SchemaLabel loadElement() {
}

E.checkArgumentNotNull(schemaLabel, "Can't find the %s with name '%s'",
this.baseType, this.baseValue);
this.baseType.readableName(), this.baseValue);
return schemaLabel;
}

Expand All @@ -301,9 +302,10 @@ private void checkFields(Set<Id> propertyIds) {
for (String field : fields) {
PropertyKey pkey = this.transaction.getPropertyKey(field);
// In general this will not happen
E.checkArgumentNotNull(pkey, "Can't build index on undefined " +
"property key '%s' for '%s': '%s'",
field, this.baseType, this.baseValue);
E.checkArgument(pkey != null,
"Can't build index on undefined property key " +
"'%s' for '%s': '%s'", field,
this.baseType.readableName(), this.baseValue);
E.checkArgument(pkey.cardinality() == Cardinality.SINGLE,
"Not allowed to build index on property key " +
"'%s' whose cardinality is list or set",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ public PropertyKey build() {

@Override
public PropertyKey create() {
HugeType type = HugeType.PROPERTY_KEY;
SchemaTransaction tx = this.transaction;
SchemaElement.checkName(this.name, tx.graph().configuration());
PropertyKey propertyKey = tx.getPropertyKey(this.name);
if (propertyKey != null) {
if (this.checkExist) {
throw new ExistedException("property key", this.name);
throw new ExistedException(type, this.name);
}
return propertyKey;
}
tx.checkIdIfRestoringMode(HugeType.PROPERTY_KEY, this.id);
tx.checkIdIfRestoringMode(type, this.id);

this.checkUserdata(Action.INSERT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,17 @@ public VertexLabel build() {

@Override
public VertexLabel create() {
HugeType type = HugeType.VERTEX_LABEL;
SchemaTransaction tx = this.transaction;
SchemaElement.checkName(this.name, tx.graph().configuration());
VertexLabel vertexLabel = tx.getVertexLabel(this.name);
if (vertexLabel != null) {
if (this.checkExist) {
throw new ExistedException("vertex label", this.name);
throw new ExistedException(type, this.name);
}
return vertexLabel;
}
tx.checkIdIfRestoringMode(HugeType.VERTEX_LABEL, this.id);
tx.checkIdIfRestoringMode(type, this.id);

this.checkProperties(Action.INSERT);
this.checkIdStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public String string() {
return this.name;
}

public String readableName() {
return this.name().replace('_', ' ').toLowerCase();
}

public boolean isSchema() {
return this == HugeType.VERTEX_LABEL ||
this == HugeType.EDGE_LABEL ||
Expand Down

0 comments on commit b63277e

Please sign in to comment.