Skip to content

Commit 82305f9

Browse files
committed
Replace direct stack trace output with managed error handling
1 parent adeb9a8 commit 82305f9

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/main/java/io/fabric8/maven/docker/assembly/DockerAssemblyManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ private File createChangedFilesTarBall(File archive, File archiveDir) throws Moj
429429
} catch (IOException e) {
430430
throw new MojoExecutionException("Cannot create archive " + archive, e);
431431
} catch (RuntimeException e) {
432-
e.printStackTrace();
433432
throw e;
434433
}
435434
}

src/main/java/io/fabric8/maven/docker/util/AnsiLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public AnsiLogger(Log log, boolean useColor, String verbose, boolean batchMode,
8484
try {
8585
initializePrintWriter();
8686
} catch (FileNotFoundException e) {
87-
e.printStackTrace();
87+
log.error(prefix + "Cannot write log output to " + outputFile, e);
8888
}
8989
}
9090

src/test/java/io/fabric8/maven/docker/assembly/DockerAssemblyManagerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,25 @@ void testArchiveCreationDockerfileNoAssembly() throws MojoExecutionException, No
243243
verifyArchiveManager();
244244
}
245245

246+
@Test
247+
void archiveCreationPropagatesRuntimeException() throws IOException, NoSuchArchiverException {
248+
MojoParameters mojoParams = mockMojoParams(mockMavenProject());
249+
BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder()
250+
.dockerFile(DockerAssemblyManagerTest.class.getResource("/docker/Dockerfile.test").getPath())
251+
.build();
252+
buildImageConfiguration.initAndValidate(logger);
253+
RuntimeException failure = new IllegalStateException("Archive creation failed");
254+
255+
Mockito.doReturn(tarArchiver).when(archiverManager).getArchiver("tar");
256+
Mockito.doThrow(failure).when(tarArchiver).createArchive();
257+
258+
RuntimeException thrown = Assertions.assertThrows(RuntimeException.class,
259+
() -> assemblyManager.createDockerTarArchive(
260+
"test_image", mojoParams, buildImageConfiguration, logger, null));
261+
262+
Assertions.assertSame(failure, thrown);
263+
}
264+
246265
private void verifyArchiveManager() {
247266
List<FileSet> fileSets = getFileSetsToVerify(2);
248267
Assertions.assertEquals("build", fileSets.get(0).getDirectory().getName());

src/test/java/io/fabric8/maven/docker/util/AnsiLoggerTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,27 @@
2323
import org.fusesource.jansi.Ansi;
2424
import org.junit.jupiter.api.Assertions;
2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.io.TempDir;
27+
28+
import java.io.FileNotFoundException;
29+
import java.nio.file.Path;
2630

2731
/**
2832
* @author roland
2933
* @since 07/10/16
3034
*/
3135
class AnsiLoggerTest {
3236

37+
@Test
38+
void logOutputInitializationFailureUsesMavenLogger(@TempDir Path temporaryDirectory) {
39+
TestLog testLog = new TestLog();
40+
41+
new AnsiLogger(testLog, false, null, false, "T>", temporaryDirectory.toFile());
42+
43+
Assertions.assertEquals("T>Cannot write log output to " + temporaryDirectory, testLog.getMessage());
44+
Assertions.assertInstanceOf(FileNotFoundException.class, testLog.getThrowable());
45+
}
46+
3347
@Test
3448
void emphasizeDebug() {
3549
TestLog testLog = new TestLog() {
@@ -198,6 +212,7 @@ void emphasizeError() {
198212

199213
private class TestLog extends DefaultLog {
200214
private String message;
215+
private Throwable throwable;
201216

202217
public TestLog() {
203218
super(new ConsoleLogger());
@@ -227,13 +242,25 @@ public void error(CharSequence content) {
227242
super.error(content);
228243
}
229244

245+
@Override
246+
public void error(CharSequence content, Throwable error) {
247+
this.message = content.toString();
248+
this.throwable = error;
249+
super.error(content, error);
250+
}
251+
230252
void reset() {
231253
message = null;
254+
throwable = null;
232255
}
233256

234257
public String getMessage() {
235258
return message;
236259
}
260+
261+
public Throwable getThrowable() {
262+
return throwable;
263+
}
237264
}
238265

239266
}

0 commit comments

Comments
 (0)