Search in sources :

Example 1 with GradleScriptException

use of org.gradle.api.GradleScriptException in project gradle by gradle.

the class DefaultScriptRunnerFactoryTest method wrapsExecutionExceptionAndRestoresStateWhenScriptFails.

@Test
public void wrapsExecutionExceptionAndRestoresStateWhenScriptFails() {
    final RuntimeException failure = new RuntimeException();
    ScriptRunner<?, Void> scriptRunner = factory.create(compiledScriptMock, scriptSourceDummy, classLoaderDummy);
    expectScriptInstantiated();
    context.checking(new Expectations() {

        {
            Sequence sequence = context.sequence("seq");
            allowing(compiledScriptMock).getRunDoesSomething();
            will(returnValue(true));
            one(scriptExecutionListenerMock).scriptClassLoaded(scriptSourceDummy, Script.class);
            inSequence(sequence);
            one(scriptMock).init(target, scriptServices);
            inSequence(sequence);
            one(standardOutputCaptureMock).start();
            inSequence(sequence);
            one(scriptMock).run();
            inSequence(sequence);
            will(throwException(failure));
            one(standardOutputCaptureMock).stop();
            inSequence(sequence);
        }
    });
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    assertThat(originalClassLoader, not(sameInstance(classLoaderDummy)));
    try {
        scriptRunner.run(target, scriptServices);
        fail();
    } catch (GradleScriptException e) {
        assertThat(e.getMessage(), equalTo("A problem occurred evaluating <script-to-string>."));
        assertThat(e.getCause(), sameInstance((Throwable) failure));
    }
    assertThat(Thread.currentThread().getContextClassLoader(), sameInstance(originalClassLoader));
}
Also used : Expectations(org.jmock.Expectations) Script(org.gradle.groovy.scripts.Script) GradleScriptException(org.gradle.api.GradleScriptException) Sequence(org.jmock.Sequence) Test(org.junit.Test)

Example 2 with GradleScriptException

use of org.gradle.api.GradleScriptException in project gradle by gradle.

the class DefaultExceptionAnalyser method collectFailures.

@Override
public void collectFailures(Throwable exception, Collection<? super Throwable> failures) {
    if (exception instanceof ProjectConfigurationException) {
        ProjectConfigurationException projectConfigurationException = (ProjectConfigurationException) exception;
        List<Throwable> additionalFailures = new ArrayList<>();
        for (Throwable cause : projectConfigurationException.getCauses()) {
            // TODO: remove this special case
            if (cause instanceof GradleScriptException) {
                failures.add(transform(cause));
            } else {
                additionalFailures.add(cause);
            }
        }
        if (!additionalFailures.isEmpty()) {
            projectConfigurationException.initCauses(additionalFailures);
            failures.add(transform(projectConfigurationException));
        }
    } else if (exception instanceof ServiceCreationException) {
        failures.add(transform(new InitializationException(exception)));
    } else {
        failures.add(transform(exception));
    }
}
Also used : ServiceCreationException(org.gradle.internal.service.ServiceCreationException) ArrayList(java.util.ArrayList) GradleScriptException(org.gradle.api.GradleScriptException) ProjectConfigurationException(org.gradle.api.ProjectConfigurationException)

Example 3 with GradleScriptException

use of org.gradle.api.GradleScriptException in project gradle by gradle.

the class IdeaScalaConfigurer method findIdeaTargetVersion.

private VersionNumber findIdeaTargetVersion() {
    VersionNumber targetVersion = null;
    String targetVersionString = rootProject.getExtensions().getByType(IdeaModel.class).getTargetVersion();
    if (targetVersionString != null) {
        targetVersion = VersionNumber.parse(targetVersionString);
        if (targetVersion.equals(VersionNumber.UNKNOWN)) {
            throw new GradleScriptException("String '" + targetVersionString + "' is not a valid value for IdeaModel.targetVersion.", null);
        }
    }
    return targetVersion;
}
Also used : IdeaModel(org.gradle.plugins.ide.idea.model.IdeaModel) GradleScriptException(org.gradle.api.GradleScriptException) VersionNumber(org.gradle.util.internal.VersionNumber)

Example 4 with GradleScriptException

use of org.gradle.api.GradleScriptException in project gradle by gradle.

the class DefaultExceptionAnalyserTest method prefersLocationAwareExceptionOverScriptException.

@Test
public void prefersLocationAwareExceptionOverScriptException() {
    Throwable cause = locationAwareException(new GradleScriptException("broken", new RuntimeException()));
    Throwable failure = new TaskExecutionException(null, cause);
    DefaultExceptionAnalyser analyser = analyser();
    assertThat(analyser.transform(failure), sameInstance(cause));
}
Also used : TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) GradleScriptException(org.gradle.api.GradleScriptException) Test(org.junit.Test)

Example 5 with GradleScriptException

use of org.gradle.api.GradleScriptException in project gradle by gradle.

the class DefaultExceptionAnalyserTest method prefersScriptExceptionOverContextualException.

@Test
public void prefersScriptExceptionOverContextualException() {
    Throwable cause = new GradleScriptException("broken", new ContextualException());
    Throwable failure = new TaskExecutionException(null, cause);
    Throwable transformedFailure = analyser().transform(failure);
    assertThat(transformedFailure, instanceOf(LocationAwareException.class));
    LocationAwareException gse = (LocationAwareException) transformedFailure;
    assertThat(gse.getCause(), sameInstance(cause));
}
Also used : TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) LocationAwareException(org.gradle.internal.exceptions.LocationAwareException) GradleScriptException(org.gradle.api.GradleScriptException) Test(org.junit.Test)

Aggregations

GradleScriptException (org.gradle.api.GradleScriptException)6 Test (org.junit.Test)4 TaskExecutionException (org.gradle.api.tasks.TaskExecutionException)2 LocationAwareException (org.gradle.internal.exceptions.LocationAwareException)2 ArrayList (java.util.ArrayList)1 ProjectConfigurationException (org.gradle.api.ProjectConfigurationException)1 Script (org.gradle.groovy.scripts.Script)1 ServiceCreationException (org.gradle.internal.service.ServiceCreationException)1 IdeaModel (org.gradle.plugins.ide.idea.model.IdeaModel)1 VersionNumber (org.gradle.util.internal.VersionNumber)1 Expectations (org.jmock.Expectations)1 Sequence (org.jmock.Sequence)1