From b63277e583bd01ff08cc187eefcd46235bcae81d Mon Sep 17 00:00:00 2001 From: Jermy Li Date: Mon, 3 Jun 2019 05:05:23 -0500 Subject: [PATCH] improve error message with readable HugeType (#546) Change-Id: I633eee63fe9458cdc910f977fccec8fd0109f1d2 --- .../src/main/java/com/baidu/hugegraph/api/API.java | 3 ++- .../main/java/com/baidu/hugegraph/HugeGraph.java | 8 ++++---- .../hugegraph/backend/tx/AbstractTransaction.java | 3 ++- .../hugegraph/backend/tx/SchemaTransaction.java | 8 +++++--- .../hugegraph/exception/ExistedException.java | 5 +++++ .../com/baidu/hugegraph/schema/SchemaManager.java | 2 +- .../hugegraph/schema/builder/EdgeLabelBuilder.java | 5 +++-- .../schema/builder/IndexLabelBuilder.java | 14 ++++++++------ .../schema/builder/PropertyKeyBuilder.java | 5 +++-- .../schema/builder/VertexLabelBuilder.java | 5 +++-- .../java/com/baidu/hugegraph/type/HugeType.java | 4 ++++ 11 files changed, 40 insertions(+), 22 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java index 48aa9fefc4..eb46c97bf3 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java @@ -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)); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java index 8471574150..cf531c0b9e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -411,7 +411,7 @@ public Iterator 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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/AbstractTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/AbstractTransaction.java index eb5c6c12e0..48674ac100 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/AbstractTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/AbstractTransaction.java @@ -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; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java index 4dc29cef74..996fa4bab3 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java @@ -280,7 +280,8 @@ protected void addSchema(SchemaElement schema) { } protected 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) { @@ -300,7 +301,8 @@ protected T getSchema(HugeType type, Id id) { */ protected 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); @@ -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); } } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/exception/ExistedException.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/exception/ExistedException.java index ced6e56f1e..b4024f1457 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/exception/ExistedException.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/exception/ExistedException.java @@ -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); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaManager.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaManager.java index a99ac287dc..b469a909d1 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaManager.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaManager.java @@ -113,7 +113,7 @@ public List 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); } } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java index 266c40c403..b43402aa33 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java @@ -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; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java index a33652215e..cc97c17b35 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java @@ -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(); @@ -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; } @@ -301,9 +302,10 @@ private void checkFields(Set 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", diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/PropertyKeyBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/PropertyKeyBuilder.java index 9e6393b0f5..fdfc0a7417 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/PropertyKeyBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/PropertyKeyBuilder.java @@ -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); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java index 46eb1c0caf..fe2a6ed052 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java @@ -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(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/HugeType.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/HugeType.java index 7ffcc09050..9c49e0dc07 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/HugeType.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/HugeType.java @@ -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 ||