Search in sources :

Example 1 with ScriptCompilationException

use of org.gradle.groovy.scripts.ScriptCompilationException 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 2 with ScriptCompilationException

use of org.gradle.groovy.scripts.ScriptCompilationException in project gradle by gradle.

the class DefaultScriptCompilationHandler method wrapCompilationFailure.

private void wrapCompilationFailure(ScriptSource source, MultipleCompilationErrorsException e) {
    // Fix the source file name displayed in the error messages
    for (Object message : e.getErrorCollector().getErrors()) {
        if (message instanceof SyntaxErrorMessage) {
            try {
                SyntaxErrorMessage syntaxErrorMessage = (SyntaxErrorMessage) message;
                Field sourceField = SyntaxErrorMessage.class.getDeclaredField("source");
                sourceField.setAccessible(true);
                SourceUnit sourceUnit = (SourceUnit) sourceField.get(syntaxErrorMessage);
                Field nameField = SourceUnit.class.getDeclaredField("name");
                nameField.setAccessible(true);
                nameField.set(sourceUnit, source.getDisplayName());
            } catch (Exception failure) {
                throw UncheckedException.throwAsUncheckedException(failure);
            }
        }
    }
    SyntaxException syntaxError = e.getErrorCollector().getSyntaxError(0);
    Integer lineNumber = syntaxError == null ? null : syntaxError.getLine();
    throw new ScriptCompilationException(String.format("Could not compile %s.", source.getDisplayName()), e, source, lineNumber);
}
Also used : Field(java.lang.reflect.Field) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) SourceUnit(org.codehaus.groovy.control.SourceUnit) UncheckedException(org.gradle.internal.UncheckedException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) GradleException(org.gradle.api.GradleException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 3 with ScriptCompilationException

use of org.gradle.groovy.scripts.ScriptCompilationException in project gradle by gradle.

the class ExceptionMetadataHelper method getMetadata.

public static Map<String, String> getMetadata(Throwable t) {
    Map<String, String> metadata = new LinkedHashMap<>();
    if (t instanceof TaskExecutionException) {
        TaskExecutionException taskExecutionException = (TaskExecutionException) t;
        String taskPath = ((TaskInternal) taskExecutionException.getTask()).getIdentityPath().getPath();
        metadata.put(METADATA_KEY_TASK_PATH, taskPath);
    }
    if (t instanceof ScriptCompilationException) {
        ScriptCompilationException sce = (ScriptCompilationException) t;
        metadata.put(METADATA_KEY_SCRIPT_FILE, sce.getScriptSource().getFileName());
        Integer sceLineNumber = sce.getLineNumber();
        if (sceLineNumber != null) {
            metadata.put(METADATA_KEY_SCRIPT_LINE_NUMBER, sceLineNumber.toString());
        }
    }
    if (t instanceof LocationAwareException) {
        LocationAwareException lae = (LocationAwareException) t;
        metadata.put(METADATA_KEY_SOURCE_DISPLAY_NAME, lae.getSourceDisplayName());
        Integer laeLineNumber = lae.getLineNumber();
        if (laeLineNumber != null) {
            metadata.put(METADATA_KEY_LINE_NUMBER, laeLineNumber.toString());
        }
        metadata.put(METADATA_KEY_LOCATION, lae.getLocation());
    }
    if (t instanceof MultiCauseException) {
        metadata.put(METADATA_KEY_IS_MULTICAUSE, String.valueOf(true));
    }
    return metadata;
}
Also used : TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) LocationAwareException(org.gradle.internal.exceptions.LocationAwareException) MultiCauseException(org.gradle.internal.exceptions.MultiCauseException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with ScriptCompilationException

use of org.gradle.groovy.scripts.ScriptCompilationException in project gradle by gradle.

the class DefaultExceptionAnalyser method transform.

private 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)

Aggregations

ScriptCompilationException (org.gradle.groovy.scripts.ScriptCompilationException)4 LocationAwareException (org.gradle.internal.exceptions.LocationAwareException)3 ScriptSource (org.gradle.groovy.scripts.ScriptSource)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Field (java.lang.reflect.Field)1 LinkedHashMap (java.util.LinkedHashMap)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)1 SourceUnit (org.codehaus.groovy.control.SourceUnit)1 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)1 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)1 GradleException (org.gradle.api.GradleException)1 TaskExecutionException (org.gradle.api.tasks.TaskExecutionException)1 UncheckedException (org.gradle.internal.UncheckedException)1 MultiCauseException (org.gradle.internal.exceptions.MultiCauseException)1