use of net.starlark.java.eval.EvalException in project copybara by google.
the class SkylarkTransformation method transform.
@Override
public TransformationStatus transform(TransformWork work) throws IOException, ValidationException, RepoException {
SkylarkConsole skylarkConsole = new SkylarkConsole(work.getConsole());
TransformWork skylarkWork = work.withConsole(skylarkConsole).withParams(params);
TransformationStatus status = TransformationStatus.success();
try (Mutability mu = Mutability.create("dynamic_transform")) {
StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
thread.setPrintHandler(printHandler);
Object result = Starlark.call(thread, function, ImmutableList.of(skylarkWork), /*kwargs=*/
ImmutableMap.of());
result = result == Starlark.NONE ? TransformationStatus.success() : result;
checkCondition(result instanceof TransformationStatus, "Dynamic transforms functions should return nothing or objects of type %s, but '%s'" + " returned: %s", TransformationStatus.STARLARK_TYPE_NAME, describe(), result);
status = (TransformationStatus) result;
} catch (EvalException e) {
if (e.getCause() instanceof EmptyChangeException) {
throw ((EmptyChangeException) e.getCause());
}
if (e.getCause() instanceof RepoException) {
throw new RepoException(String.format("Error while executing the skylark transformation %s: %s", describe(), e.getMessageWithStack()), e);
}
throw new ValidationException(String.format("Error while executing the skylark transformation %s: %s", describe(), e.getMessageWithStack()), e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("This should not happen.", e);
} finally {
work.updateFrom(skylarkWork);
}
checkCondition(skylarkConsole.getErrorCount() == 0, "%d error(s) while executing %s", skylarkConsole.getErrorCount(), describe());
return status;
}
use of net.starlark.java.eval.EvalException in project copybara by google.
the class SkylarkUtilTest method testCheckNotEmpty_null.
@Test
public void testCheckNotEmpty_null() throws Exception {
EvalException thrown = assertThrows(EvalException.class, () -> SkylarkUtil.checkNotEmpty(null, "foo"));
assertThat(thrown).hasMessageThat().contains("Invalid empty field 'foo'");
}
use of net.starlark.java.eval.EvalException in project copybara by google.
the class StarlarkAction method run.
@Override
public <T extends SkylarkContext<T>> void run(ActionContext<T> context) throws ValidationException, RepoException {
SkylarkContext<T> actionContext = context.withParams(params);
try (Mutability mu = Mutability.create("dynamic_action")) {
StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
thread.setPrintHandler(printHandler);
Object result = Starlark.call(thread, function, ImmutableList.of(actionContext), /*kwargs=*/
ImmutableMap.of());
context.onFinish(result, actionContext);
} catch (EvalException e) {
Throwable cause = e.getCause();
String error = String.format("Error while executing the skylark transformation %s: %s.", function.getName(), e.getMessageWithStack());
if (cause instanceof RepoException) {
throw new RepoException(error, cause);
}
throw new ValidationException(error, cause);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("This should not happen.", e);
}
}
use of net.starlark.java.eval.EvalException in project copybara by google.
the class GerritEndpoint method getChange.
@StarlarkMethod(name = "get_change", doc = "Retrieve a Gerrit change.", parameters = { @Param(name = "id", named = true, doc = "The change id or change number."), @Param(name = "include_results", named = true, allowedTypes = { @ParamType(type = Sequence.class, generic1 = String.class) }, doc = "" + "What to include in the response. See " + "https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html" + "#query-options", positional = false, defaultValue = "['LABELS']") })
public ChangeInfo getChange(String id, Sequence<?> includeResults) throws EvalException {
try {
ChangeInfo changeInfo = doGetChange(id, getIncludeResults(includeResults));
ValidationException.checkCondition(!changeInfo.isMoreChanges(), "Pagination is not supported yet.");
return changeInfo;
} catch (RepoException | ValidationException | RuntimeException e) {
throw new EvalException("Error getting change: " + e.getMessage(), e);
}
}
use of net.starlark.java.eval.EvalException in project copybara by google.
the class GerritEndpoint method deleteVote.
@StarlarkMethod(name = "delete_vote", doc = "Delete a label vote from an account owner on a Gerrit change.\n", parameters = { @Param(name = "change_id", named = true, doc = "The Gerrit change id."), @Param(name = "account_id", named = true, doc = "The account owner who votes on label_id. Use 'me' or 'self' " + "if the account owner makes this api call"), @Param(name = "label_id", named = true, doc = "The name of the label.") })
public void deleteVote(String changeId, String accountId, String labelId) throws EvalException {
try {
GerritApi gerritApi = apiSupplier.load(console);
gerritApi.deleteVote(changeId, accountId, labelId, new DeleteVoteInput(NotifyType.NONE));
} catch (RepoException | ValidationException | RuntimeException e) {
throw new EvalException("Error calling delete_vote: " + e.getMessage(), e);
}
}
Aggregations