-
Notifications
You must be signed in to change notification settings - Fork 54
@W-11404189@ Adding error message for unreachable code #747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.salesforce.config; | ||
|
|
||
| /** | ||
| * Contains error message constants that will be displayed to users. TODO: move all other | ||
| * user-facing messages here. | ||
| */ | ||
| public final class UserFacingErrorMessages { | ||
|
|
||
| /** UserActionException * */ | ||
|
|
||
| // format: filename,defined type, line number | ||
| public static final String UNREACHABLE_CODE = | ||
| "Please remove unreachable code to proceed with analysis: %s,%s:%d"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,10 @@ | ||
| package com.salesforce.graph.build; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import com.salesforce.apex.jorje.ASTConstants; | ||
| import com.salesforce.apex.jorje.JorjeNode; | ||
| import com.salesforce.collections.CollectionUtil; | ||
| import com.salesforce.exception.UnexpectedException; | ||
| import com.salesforce.graph.Schema; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
|
|
@@ -27,10 +25,6 @@ | |
| @SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod") | ||
| abstract class AbstractApexVertexBuilder { | ||
| private static final Logger LOGGER = LogManager.getLogger(AbstractApexVertexBuilder.class); | ||
| protected static final Set<String> ROOT_VERTICES = | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this to GremlinUtil to avoid two-way reference between both the classes. |
||
| ImmutableSet.<String>builder() | ||
| .addAll(Arrays.asList(ASTConstants.NodeType.ROOT_VERTICES)) | ||
| .build(); | ||
| protected final GraphTraversalSource g; | ||
|
|
||
| protected AbstractApexVertexBuilder(GraphTraversalSource g) { | ||
|
|
@@ -66,7 +60,7 @@ private void buildVertices(JorjeNode node, Vertex vNodeParam, String fileName) { | |
| // The engine assumes that only certain types of nodes can be roots. We need to enforce | ||
| // that assumption and | ||
| // fail noisily if it's violated. | ||
| if (!ROOT_VERTICES.contains(vNode.label())) { | ||
| if (!GremlinUtil.ROOT_VERTICES.contains(vNode.label())) { | ||
| throw new UnexpectedException("Unexpected root vertex of type " + vNode.label()); | ||
| } | ||
| vNode.property(Schema.FILE_NAME, fileName); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| package com.salesforce.graph.build; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import com.salesforce.apex.jorje.ASTConstants; | ||
| import com.salesforce.exception.UnexpectedException; | ||
| import com.salesforce.graph.Schema; | ||
| import java.util.Arrays; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import org.apache.tinkerpop.gremlin.process.traversal.Order; | ||
| import org.apache.tinkerpop.gremlin.process.traversal.Scope; | ||
| import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; | ||
| import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; | ||
| import org.apache.tinkerpop.gremlin.structure.T; | ||
| import org.apache.tinkerpop.gremlin.structure.Vertex; | ||
|
|
||
|
|
@@ -17,6 +22,11 @@ | |
| * and labels. | ||
| */ | ||
| public final class GremlinUtil { | ||
| protected static final Set<String> ROOT_VERTICES = | ||
| ImmutableSet.<String>builder() | ||
| .addAll(Arrays.asList(ASTConstants.NodeType.ROOT_VERTICES)) | ||
| .build(); | ||
|
|
||
| public static Long getId(Map<Object, Object> vertexProperties) { | ||
| return (Long) vertexProperties.get(T.id); | ||
| } | ||
|
|
@@ -105,5 +115,19 @@ public static Optional<Vertex> getParent(GraphTraversalSource g, Vertex vertex) | |
| } | ||
| } | ||
|
|
||
| public static String getFileName(GraphTraversalSource g, Vertex vertex) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New method to get filename from a vertex. |
||
| if (ROOT_VERTICES.contains(vertex.label())) { | ||
| return vertex.value(Schema.FILE_NAME); | ||
| } | ||
| List<Vertex> verticesWithFileName = | ||
| g.V(vertex).repeat(__.in(Schema.CHILD)).until(__.has(Schema.FILE_NAME)).toList(); | ||
|
|
||
| if (verticesWithFileName.isEmpty()) { | ||
| return "UNKNOWN_FILENAME"; | ||
| } | ||
|
|
||
| return verticesWithFileName.get(0).value(Schema.FILE_NAME); | ||
| } | ||
|
|
||
| private GremlinUtil() {} | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,10 @@ | |
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import com.salesforce.apex.jorje.ASTConstants.NodeType; | ||
| import com.salesforce.config.UserFacingErrorMessages; | ||
| import com.salesforce.exception.TodoException; | ||
| import com.salesforce.exception.UnexpectedException; | ||
| import com.salesforce.exception.UserActionException; | ||
| import com.salesforce.graph.Schema; | ||
| import com.salesforce.graph.vertex.SFVertexFactory; | ||
| import java.util.ArrayList; | ||
|
|
@@ -165,7 +167,7 @@ private void _visit(Vertex vertex, Vertex parent, boolean lastChild) { | |
| currentInnerScope.pop(); | ||
| nextVertexInOuterScope.pop(); | ||
| } | ||
| } catch (TodoException | UnexpectedException ex) { | ||
| } catch (UserActionException | TodoException | UnexpectedException ex) { | ||
| throw ex; | ||
| } catch (Exception ex) { | ||
| throw new UnexpectedException(g, vertex, ex); | ||
|
|
@@ -592,7 +594,13 @@ private void addEdgeFromPreviousSibling(Vertex vertex) { | |
|
|
||
| private void addEdge(String name, Vertex from, Vertex to) { | ||
| if (NodeType.TERMINAL_VERTEX_LABELS.contains(from.label())) { | ||
| throw new UnexpectedException("Vertex is terminal. vertex=" + vertexToString(from)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replacing the cryptic message. |
||
| // Ask user to fix unreachable code | ||
| throw new UserActionException( | ||
| String.format( | ||
| UserFacingErrorMessages.UNREACHABLE_CODE, | ||
| GremlinUtil.getFileName(g, to), | ||
| to.value(Schema.DEFINING_TYPE), | ||
| to.value(Schema.BEGIN_LINE))); | ||
| } | ||
|
|
||
| if (LOGGER.isTraceEnabled()) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a simple constants file at the moment, but as we move more messages to here, would be easier to switch to a different mechanism.