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