Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.engine.language.pure.grammar.api.grammarToJson.GrammarToJson;
import org.finos.legend.engine.language.pure.grammar.api.jsonToGrammar.JsonToGrammar;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.graph.RootGraphFetchTree;
import org.finos.legend.engine.shared.core.api.TestGrammar;
import org.finos.legend.engine.shared.core.api.grammar.BatchResult;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void testSimpleParsingError()
" }\n" +
" }\n" +
" }\n" +
"}#", "{\"message\":\"Unexpected token\",\"sourceInformation\":{\"endColumn\":9,\"endLine\":3,\"sourceId\":\"\",\"startColumn\":5,\"startLine\":3}}");
"}#", "Unexpected token", new SourceInformation("", 3, 5, 3, 9));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.engine.language.pure.grammar.api.grammarToJson.GrammarToJson;
import org.finos.legend.engine.language.pure.grammar.api.jsonToGrammar.JsonToGrammar;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda;
import org.finos.legend.engine.shared.core.api.TestGrammar;
import org.finos.legend.engine.shared.core.api.grammar.BatchResult;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void testSimple()
@Test
public void testSimpleParsingError()
{
testError("|1->toString(),", "{\"message\":\"no viable alternative at input '->toString(),'\",\"sourceInformation\":{\"endColumn\":15,\"endLine\":1,\"sourceId\":\"\",\"startColumn\":15,\"startLine\":1}}");
testError("|1->toString(),", "no viable alternative at input '->toString(),'", new SourceInformation("", 1, 15, 1, 15));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.eclipse.collections.api.block.function.Function2;
import org.finos.legend.engine.language.pure.grammar.api.grammarToJson.GrammarToJson;
import org.finos.legend.engine.language.pure.grammar.api.jsonToGrammar.JsonToGrammar;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.shared.core.api.TestGrammar;
import org.finos.legend.engine.shared.core.api.grammar.RenderStyle;
Expand Down Expand Up @@ -35,7 +36,7 @@ public void testSimple()
@Test
public void testSimpleError()
{
testError("Class A\n{", "{\"message\":\"Unexpected token\",\"sourceInformation\":{\"endColumn\":1,\"endLine\":2,\"sourceId\":\"\",\"startColumn\":1,\"startLine\":2}}");
testError("Class A\n{", "Unexpected token", new SourceInformation("", 2, 1, 2, 1));
}

private static final GrammarToJson grammarToJson = new GrammarToJson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.engine.language.pure.grammar.api.grammarToJson.GrammarToJson;
import org.finos.legend.engine.language.pure.grammar.api.jsonToGrammar.JsonToGrammar;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification;
import org.finos.legend.engine.shared.core.api.TestGrammar;
import org.finos.legend.engine.shared.core.api.grammar.BatchResult;
Expand All @@ -28,11 +29,10 @@ public void testSimple()
@Test
public void testSimpleError()
{
testError("1", "{\"_type\":\"integer\",\"multiplicity\":{\"lowerBound\":1,\"upperBound\":1},\"sourceInformation\":{\"endColumn\":1,\"endLine\":1,\"sourceId\":\"\",\"startColumn\":1,\"startLine\":1},\"values\":[1]}");
testError("true->func(\n" +
" 2\n" +
" 'www'\n" +
")", "{\"message\":\"Unexpected token\",\"sourceInformation\":{\"endColumn\":7,\"endLine\":3,\"sourceId\":\"\",\"startColumn\":3,\"startLine\":3}}");
")", "Unexpected token", new SourceInformation("", 3, 3, 3, 7));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-protocol-pure</artifactId>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-protocol</artifactId>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-language-pure-grammar</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.engine.language.pure.grammar.api.relationalOperationElement.RelationalOperationElementGrammarToJson;
import org.finos.legend.engine.language.pure.grammar.api.relationalOperationElement.RelationalOperationElementJsonToGrammar;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.RelationalOperationElement;
import org.finos.legend.engine.shared.core.api.TestGrammar;
import org.finos.legend.engine.shared.core.api.grammar.BatchResult;
Expand All @@ -24,7 +25,7 @@ public void testSimple()
@Test
public void testSimpleError()
{
testError("add(1,", "{\"message\":\"Unexpected token\",\"sourceInformation\":{\"endColumn\":11,\"endLine\":1,\"sourceId\":\"\",\"startColumn\":7,\"startLine\":1}}");
testError("add(1,", "Unexpected token", new SourceInformation("", 1, 7, 1, 11));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.engine.protocol.graphQL.metamodel.ExecutableDocument;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.query.graphQL.api.grammar.GraphQLGrammar;
import org.finos.legend.engine.shared.core.api.TestGrammar;
import org.finos.legend.engine.shared.core.api.grammar.BatchResult;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void testSimpleError()
" name String!\n" +
" values: [String]\n" +
" length(unit: LengthUnit = METER): Float\n" +
"}", "{\"message\":\"Unexpected token\",\"sourceInformation\":{\"endColumn\":13,\"endLine\":3,\"sourceId\":\"\",\"startColumn\":8,\"startLine\":3}}");
"}", "Unexpected token", new SourceInformation("", 3, 8, 3, 13));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class GrammarAPI
{
private static final ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();

protected <T> Response grammarToJson(String input, Function2<String, Boolean, T> func , ProfileManager<CommonProfile> pm, boolean returnSourceInfo, String spanText)
protected <T> Response grammarToJson(String input, Function2<String, Boolean, T> func, ProfileManager<CommonProfile> pm, boolean returnSourceInfo, String spanText)
{
MutableList<CommonProfile> profiles = ProfileManagerHelper.extractProfiles(pm);
try (Scope scope = GlobalTracer.get().buildSpan(spanText).startActive(true))
Expand All @@ -34,27 +34,19 @@ protected <T> Response grammarToJson(String input, Function2<String, Boolean, T>
T data = func.apply(input, returnSourceInfo);
return ManageConstantResult.manageResult(profiles, data, objectMapper);
}
catch (EngineException e)
{
if (!EngineErrorType.PARSER.equals(e.getErrorType()))
{
return ExceptionTool.exceptionManager(e, LoggingEventType.TRANSFORM_GRAMMAR_TO_JSON_ERROR, profiles);
}
return ManageConstantResult.manageResult(profiles, new ParserError(e.getMessage(), e.getSourceInformation()), objectMapper);
}
catch (Exception e)
{
return ExceptionTool.exceptionManager(e, LoggingEventType.TRANSFORM_GRAMMAR_TO_JSON_ERROR, profiles);
return ExceptionTool.exceptionManager(e, LoggingEventType.TRANSFORM_GRAMMAR_TO_JSON_ERROR, Response.Status.BAD_REQUEST, profiles);
}
}
}

protected <T> Response grammarToJsonBatch(Map<String, String> input, Function2<String, Boolean, T> func , Map<String, T> result, ProfileManager<CommonProfile> pm, boolean returnSourceInfo, String spanText)
protected <T> Response grammarToJsonBatch(Map<String, String> input, Function2<String, Boolean, T> func, Map<String, T> result, ProfileManager<CommonProfile> pm, boolean returnSourceInfo, String spanText)
{
MutableList<CommonProfile> profiles = ProfileManagerHelper.extractProfiles(pm);
try (Scope scope = GlobalTracer.get().buildSpan(spanText).startActive(true))
{
Map<String , ParserError> errors = Maps.mutable.empty();
Map<String, ParserError> errors = Maps.mutable.empty();
input.forEach((key, value) ->
{
try
Expand All @@ -81,7 +73,7 @@ protected <T> Response grammarToJsonBatch(Map<String, String> input, Function2<S
}


protected <T> Response jsonToGrammar(T graphFetchTree, RenderStyle renderStyle, Function2<T, RenderStyle, String> func, ProfileManager<CommonProfile> pm, String spanText)
protected <T> Response jsonToGrammar(T graphFetchTree, RenderStyle renderStyle, Function2<T, RenderStyle, String> func, ProfileManager<CommonProfile> pm, String spanText)
{
MutableList<CommonProfile> profiles = ProfileManagerHelper.extractProfiles(pm);
try (Scope scope = GlobalTracer.get().buildSpan(spanText).startActive(true))
Expand All @@ -100,7 +92,8 @@ protected <T> Response jsonToGrammarBatch(RenderStyle renderStyle, Map<String, T
try (Scope scope = GlobalTracer.get().buildSpan(spanText).startActive(true))
{
Map<String, Object> result = org.eclipse.collections.api.factory.Maps.mutable.empty();
MapAdapter.adapt(values).forEachKeyValue((key, value) -> {
MapAdapter.adapt(values).forEachKeyValue((key, value) ->
{
result.put(key, func.apply(value, renderStyle));
});
return ManageConstantResult.manageResult(profiles, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@ public class ExceptionTool

public static Response exceptionManager(Exception exception, LoggingEventType eventType, Iterable<? extends CommonProfile> pm)
{
return manage(eventType, pm, new ExceptionError(-1, exception));
return manage(eventType, pm, new ExceptionError(-1, exception), Response.Status.INTERNAL_SERVER_ERROR);
}

public static Response exceptionManager(String message, LoggingEventType eventType, Iterable<? extends CommonProfile> pm)
{
return manage(eventType, pm, new ExceptionError(-1, message));
return manage(eventType, pm, new ExceptionError(-1, message), Response.Status.INTERNAL_SERVER_ERROR);
}

private static Response manage(LoggingEventType eventType, Iterable<? extends CommonProfile> pm, ExceptionError error)
public static Response exceptionManager(Exception exception, LoggingEventType eventType, Response.Status status, Iterable<? extends CommonProfile> pm)
{
return manage(eventType, pm, new ExceptionError(-1, exception), status);
}

public static Response exceptionManager(String message, LoggingEventType eventType, Response.Status status, Iterable<? extends CommonProfile> pm)
{
return manage(eventType, pm, new ExceptionError(-1, message), status);
}

private static Response manage(LoggingEventType eventType, Iterable<? extends CommonProfile> pm, ExceptionError error, Response.Status status)
{
LOGGER.error(new LogInfo(pm, eventType, error).toString());
String text;
Expand All @@ -57,6 +67,6 @@ private static Response manage(LoggingEventType eventType, Iterable<? extends Co
{
GlobalTracer.get().activeSpan().setTag("error", text);
}
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON_TYPE).entity(error).build();
return Response.status(status).type(MediaType.APPLICATION_JSON_TYPE).entity(error).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.MapIterate;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.shared.core.ObjectMapperFactory;
import org.finos.legend.engine.shared.core.api.grammar.BatchResult;
import org.finos.legend.engine.shared.core.api.grammar.RenderStyle;
import org.finos.legend.engine.shared.core.operational.errorManagement.ExceptionError;

import javax.ws.rs.core.Response;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public abstract class TestGrammar<Z>
{
Expand Down Expand Up @@ -43,13 +46,20 @@ protected void test(String str, boolean returnSourceInfo)
}
}

protected void testError(String str, String expected)
protected void testError(String str, String expectedErrorMessage, SourceInformation expectedErrorSourceInformation)
{
try
{
Response result = grammarToJson().apply(str, true);
String actual = result.getEntity().toString();
assertEquals(expected, actual);
Object errorObject = result.getEntity();
assertTrue(errorObject instanceof ExceptionError);
ExceptionError error = (ExceptionError) errorObject;
assertEquals(expectedErrorMessage, error.getMessage());
assertEquals(expectedErrorSourceInformation.sourceId, error.getSourceInformation().sourceId);
assertEquals(expectedErrorSourceInformation.startLine, error.getSourceInformation().startLine);
assertEquals(expectedErrorSourceInformation.startColumn, error.getSourceInformation().startColumn);
assertEquals(expectedErrorSourceInformation.endLine, error.getSourceInformation().endLine);
assertEquals(expectedErrorSourceInformation.endColumn, error.getSourceInformation().endColumn);
}
catch (Exception e)
{
Expand Down