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);
}
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);
}
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;
}
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);
}
Aggregations