Skip to content

Commit

Permalink
Gracefully handle cases where a new instance cannot be added directly…
Browse files Browse the repository at this point in the history
… to the model (fixes #12)
  • Loading branch information
agarciadom committed Mar 8, 2024
1 parent 26c21cd commit dab75b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,27 @@ public void createInstance(CreateInstanceRequest request, StreamObserver<ModelEl
Method mCreateInstance = factory.getClass().getMethod(methodName);
MDObject mdObject = (MDObject) mCreateInstance.invoke(factory);
if (mdObject instanceof PackageableElement) {
return findRootElement(request.getRootElementHyperlink(), project).flatMapRight((root) -> {
final Either<StatusRuntimeException, EObject> errorOrRoot = findRootElement(request.getRootElementHyperlink(), project);
return errorOrRoot.flatMapRight((root) -> {
final Element rootElement = (Element) root;
try {
ModelElementsManager.getInstance().addElement((Element) mdObject, (Element) root);
if (rootElement.canAdd(mdObject)) {
ModelElementsManager.getInstance().addElement((Element) mdObject, rootElement);
}
return Either.right(encoder.encode(mdObject));
} catch (ReadOnlyElementException e) {
LOGGER.error(e.getMessage(), e);
return Either.left(Status.INVALID_ARGUMENT
.withDescription(String.format("Element with ID %s is read only", ((Element) root).getID()))
.withDescription(String.format("Element with ID %s is read only", rootElement.getID()))
.asRuntimeException());
} catch (IllegalArgumentException e) {
/*
* Some objects falsely report canAdd() = true (e.g.
* EnumerationLiteral instances): we log a warning
* and return them as is.
*/
LOGGER.warn(e.getMessage(), e);
return Either.right(encoder.encode(mdObject));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,13 @@ public void allProfiles() throws Exception {
assertTrue("There should be at least one predefined profile", (int) module.execute() > 1);
}

@Test
public void addEnumerationLiteral() throws Exception {
EolModule module = createEOLModule();
module.parse("return new EnumerationLiteral();");
assertNotNull(module.execute());
}

private void assumeTypeExists(String typeName) {
try {
m.getAllOfKind(typeName);
Expand Down

0 comments on commit dab75b8

Please sign in to comment.