Search in sources :

Example 1 with EvalException

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;
}
Also used : TransformationStatus(com.google.copybara.TransformationStatus) ValidationException(com.google.copybara.exception.ValidationException) NonReversibleValidationException(com.google.copybara.exception.NonReversibleValidationException) StarlarkThread(net.starlark.java.eval.StarlarkThread) TransformWork(com.google.copybara.TransformWork) Mutability(net.starlark.java.eval.Mutability) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) EvalException(net.starlark.java.eval.EvalException) RepoException(com.google.copybara.exception.RepoException)

Example 2 with EvalException

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'");
}
Also used : EvalException(net.starlark.java.eval.EvalException) Test(org.junit.Test)

Example 3 with EvalException

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);
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) StarlarkThread(net.starlark.java.eval.StarlarkThread) Mutability(net.starlark.java.eval.Mutability) EvalException(net.starlark.java.eval.EvalException) RepoException(com.google.copybara.exception.RepoException)

Example 4 with EvalException

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);
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ChangeInfo(com.google.copybara.git.gerritapi.ChangeInfo) RepoException(com.google.copybara.exception.RepoException) EvalException(net.starlark.java.eval.EvalException) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Example 5 with EvalException

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);
    }
}
Also used : DeleteVoteInput(com.google.copybara.git.gerritapi.DeleteVoteInput) ValidationException(com.google.copybara.exception.ValidationException) RepoException(com.google.copybara.exception.RepoException) EvalException(net.starlark.java.eval.EvalException) GerritApi(com.google.copybara.git.gerritapi.GerritApi) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Aggregations

EvalException (net.starlark.java.eval.EvalException)9 RepoException (com.google.copybara.exception.RepoException)4 ValidationException (com.google.copybara.exception.ValidationException)4 Test (org.junit.Test)4 StarlarkMethod (net.starlark.java.annot.StarlarkMethod)2 Mutability (net.starlark.java.eval.Mutability)2 StarlarkThread (net.starlark.java.eval.StarlarkThread)2 TransformWork (com.google.copybara.TransformWork)1 TransformationStatus (com.google.copybara.TransformationStatus)1 WriterContext (com.google.copybara.WriterContext)1 Author (com.google.copybara.authoring.Author)1 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)1 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)1 ChangeInfo (com.google.copybara.git.gerritapi.ChangeInfo)1 DeleteVoteInput (com.google.copybara.git.gerritapi.DeleteVoteInput)1 GerritApi (com.google.copybara.git.gerritapi.GerritApi)1 DummyRevision (com.google.copybara.testing.DummyRevision)1