Search in sources :

Example 6 with LocationAwareException

use of org.gradle.internal.exceptions.LocationAwareException in project gradle by gradle.

the class DefaultExceptionAnalyserTest method wrapsArbitraryFailureWithLocationInformation.

@Test
public void wrapsArbitraryFailureWithLocationInformation() {
    Throwable failure = new RuntimeException();
    failure.setStackTrace(toArray(element, otherElement, callerElement));
    DefaultExceptionAnalyser analyser = analyser();
    notifyAnalyser(analyser, source);
    Throwable transformedFailure = analyser.transform(failure);
    assertThat(transformedFailure, instanceOf(LocationAwareException.class));
    LocationAwareException gse = (LocationAwareException) transformedFailure;
    assertThat(gse.getSourceDisplayName(), equalTo(source.getDisplayName()));
    assertThat(gse.getLineNumber(), equalTo(7));
    assertThat(gse.getCause(), sameInstance(failure));
}
Also used : LocationAwareException(org.gradle.internal.exceptions.LocationAwareException) Test(org.junit.Test)

Example 7 with LocationAwareException

use of org.gradle.internal.exceptions.LocationAwareException in project gradle by gradle.

the class DefaultExceptionAnalyser method transform.

public Throwable transform(Throwable exception) {
    Throwable actualException = findDeepestRootException(exception);
    if (actualException instanceof LocationAwareException) {
        return actualException;
    }
    ScriptSource source = null;
    Integer lineNumber = null;
    // TODO: remove these special cases
    if (actualException instanceof ScriptCompilationException) {
        ScriptCompilationException scriptCompilationException = (ScriptCompilationException) actualException;
        source = scriptCompilationException.getScriptSource();
        lineNumber = scriptCompilationException.getLineNumber();
    }
    if (source == null) {
        for (Throwable currentException = actualException; currentException != null; currentException = currentException.getCause()) {
            for (StackTraceElement element : currentException.getStackTrace()) {
                if (element.getLineNumber() >= 0 && scripts.containsKey(element.getFileName())) {
                    source = scripts.get(element.getFileName());
                    lineNumber = element.getLineNumber();
                    break;
                }
            }
        }
    }
    return new LocationAwareException(actualException, source, lineNumber);
}
Also used : LocationAwareException(org.gradle.internal.exceptions.LocationAwareException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) ScriptSource(org.gradle.groovy.scripts.ScriptSource)

Example 8 with LocationAwareException

use of org.gradle.internal.exceptions.LocationAwareException in project gradle by gradle.

the class GradlePluginLord method getGradleExceptionMessage.

/**
     * This code was copied from BuildExceptionReporter.reportBuildFailure in gradle's source, then modified slightly to compensate for the fact that we're not driven by options or logging things to a
     * logger object.
     */
public static String getGradleExceptionMessage(Throwable failure, ShowStacktrace stackTraceLevel) {
    if (failure == null) {
        return "";
    }
    Formatter formatter = new Formatter();
    formatter.format("%nBuild failed.%n");
    if (stackTraceLevel == ShowStacktrace.INTERNAL_EXCEPTIONS) {
        formatter.format("Use the stack trace options to get more details.");
    }
    formatter.format("%n");
    if (failure instanceof LocationAwareException) {
        LocationAwareException scriptException = (LocationAwareException) failure;
        formatter.format("%s%n%n", scriptException.getLocation());
        formatter.format("%s", scriptException.getCause().getMessage());
        for (Throwable cause : scriptException.getReportableCauses()) {
            formatter.format("%nCause: %s", getMessage(cause));
        }
    } else {
        formatter.format("%s", getMessage(failure));
    }
    if (stackTraceLevel != ShowStacktrace.INTERNAL_EXCEPTIONS) {
        formatter.format("%n%nException is:\n");
        if (stackTraceLevel == ShowStacktrace.ALWAYS_FULL) {
            return formatter.toString() + getStackTraceAsText(failure);
        }
        return formatter.toString() + getStackTraceAsText(StackTraceUtils.deepSanitize(failure));
    }
    return formatter.toString();
}
Also used : LocationAwareException(org.gradle.internal.exceptions.LocationAwareException)

Example 9 with LocationAwareException

use of org.gradle.internal.exceptions.LocationAwareException 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)

Example 10 with LocationAwareException

use of org.gradle.internal.exceptions.LocationAwareException in project gradle by gradle.

the class DefaultExceptionAnalyserTest method usesDeepestScriptExceptionException.

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

Aggregations

LocationAwareException (org.gradle.internal.exceptions.LocationAwareException)16 Test (org.junit.Test)10 GradleScriptException (org.gradle.api.GradleScriptException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Collection (java.util.Collection)1 GradleException (org.gradle.api.GradleException)1 InvalidPluginException (org.gradle.api.plugins.InvalidPluginException)1 UnknownPluginException (org.gradle.api.plugins.UnknownPluginException)1 TaskExecutionException (org.gradle.api.tasks.TaskExecutionException)1 ScriptCompilationException (org.gradle.groovy.scripts.ScriptCompilationException)1 ScriptSource (org.gradle.groovy.scripts.ScriptSource)1 ReportedException (org.gradle.initialization.ReportedException)1 LinePrefixingStyledTextOutput (org.gradle.internal.logging.text.LinePrefixingStyledTextOutput)1 DefaultPluginRequest (org.gradle.plugin.management.internal.DefaultPluginRequest)1 InvalidPluginRequestException (org.gradle.plugin.management.internal.InvalidPluginRequestException)1 PluginRequestInternal (org.gradle.plugin.management.internal.PluginRequestInternal)1 PluginId (org.gradle.plugin.use.PluginId)1 PluginResolutionResult (org.gradle.plugin.use.resolve.internal.PluginResolutionResult)1