Skip to content

Commit 22ff53d

Browse files
committed
GH-160 - Polishing.
Moved the message population for the failure analyzer to the exception to be able to give more actionable feedback and educated users on the cause of the problem.
1 parent 595a052 commit 22ff53d

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/autoconfigure/MissingRuntimeDependency.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,29 @@
1616
package org.springframework.modulith.runtime.autoconfigure;
1717

1818
/**
19-
* An Exception carrying information about a missing runtime dependency to be
20-
* analyzed by {@link MissingRuntimeDependencyFailureAnalyzer}.
19+
* An Exception carrying information about a missing runtime dependency to be analyzed by
20+
* {@link MissingRuntimeDependencyFailureAnalyzer}.
2121
*
2222
* @author Michael Weirauch
23+
* @author Oliver Drotbohm
2324
*/
24-
class MissingRuntimeDependencyException extends RuntimeException {
25+
class MissingRuntimeDependency extends RuntimeException {
2526

2627
private static final long serialVersionUID = 1L;
2728

28-
private final String dependencyName;
29+
private final String description, suggestedAction;
2930

30-
public MissingRuntimeDependencyException(String dependencyName) {
31-
this.dependencyName = dependencyName;
31+
MissingRuntimeDependency(String description, String suggestedAction) {
32+
33+
this.description = description;
34+
this.suggestedAction = suggestedAction;
35+
}
36+
37+
String getDescription() {
38+
return description;
3239
}
3340

34-
public String getDependencyName() {
35-
return dependencyName;
41+
String getSuggestedAction() {
42+
return suggestedAction;
3643
}
3744
}

spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/autoconfigure/MissingRuntimeDependencyFailureAnalyzer.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020
import org.springframework.boot.diagnostics.FailureAnalyzer;
2121

2222
/**
23-
* {@link FailureAnalyzer} for {@link MissingRuntimeDependencyException}.
23+
* {@link FailureAnalyzer} for {@link MissingRuntimeDependency}.
2424
*
2525
* @author Michael Weirauch
26+
* @author Oliver Drotbohm
2627
*/
27-
class MissingRuntimeDependencyFailureAnalyzer extends AbstractFailureAnalyzer<MissingRuntimeDependencyException> {
28+
class MissingRuntimeDependencyFailureAnalyzer extends AbstractFailureAnalyzer<MissingRuntimeDependency> {
2829

30+
/*
31+
* (non-Javadoc)
32+
* @see org.springframework.boot.diagnostics.AbstractFailureAnalyzer#analyze(java.lang.Throwable, java.lang.Throwable)
33+
*/
2934
@Override
30-
protected FailureAnalysis analyze(Throwable rootFailure, MissingRuntimeDependencyException cause) {
31-
return new FailureAnalysis(
32-
String.format("Spring Modulith requires the dependency '%s' to be on the runtime classpath.",
33-
cause.getDependencyName()),
34-
"Add the missing dependency to the runtime classpath of your project.", cause);
35+
protected FailureAnalysis analyze(Throwable rootFailure, MissingRuntimeDependency cause) {
36+
return new FailureAnalysis(cause.getDescription(), cause.getSuggestedAction(), cause);
3537
}
3638
}

spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/autoconfigure/SpringModulithRuntimeAutoConfiguration.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,10 @@
4646
* Bean.
4747
*
4848
* @author Oliver Drotbohm
49-
* @author Michael Weirauch
5049
*/
5150
@AutoConfiguration
5251
class SpringModulithRuntimeAutoConfiguration {
5352

54-
@AutoConfiguration
55-
@ConditionalOnMissingClass("com.tngtech.archunit.core.importer.ClassFileImporter")
56-
static class ArchUnitRuntimeDependencyMissingConfiguration {
57-
58-
ArchUnitRuntimeDependencyMissingConfiguration() {
59-
throw new MissingRuntimeDependencyException("archunit");
60-
}
61-
}
62-
6353
private static final Logger LOGGER = LoggerFactory.getLogger(SpringModulithRuntimeAutoConfiguration.class);
6454
private final AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
6555

@@ -179,4 +169,22 @@ private static Supplier<ApplicationModules> toSupplier(Future<ApplicationModules
179169
}
180170
};
181171
}
172+
173+
/**
174+
* Auto-configuration to react to ArchUnit missing on the runtime classpath.
175+
*
176+
* @author Michael Weirauch
177+
* @author Oliver Drotbohm
178+
*/
179+
@AutoConfiguration
180+
@ConditionalOnMissingClass("com.tngtech.archunit.core.importer.ClassFileImporter")
181+
static class ArchUnitRuntimeDependencyMissingConfiguration {
182+
183+
private static final String DESCRIPTION = "The Spring Modulith runtime support requires ArchUnit to be on the runtime classpath. This might be caused by it declared as test scope dependency, as it usually is used in tests only.";
184+
private static final String SUGGESTED_ACTION = "Add ArchUnit to your project and ensure it configured to live in the runtime classpath at least.";
185+
186+
ArchUnitRuntimeDependencyMissingConfiguration() {
187+
throw new MissingRuntimeDependency(DESCRIPTION, SUGGESTED_ACTION);
188+
}
189+
}
182190
}

spring-modulith-runtime/src/test/java/org/springframework/modulith/runtime/autoconfigure/SpringModulithRuntimeAutoConfigurationIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void bootstrapRegistersRuntimeInstances() {
5454
});
5555
}
5656

57-
@Test
57+
@Test // GH-160
5858
void missingArchUnitRuntimeDependencyEscalatesOnContextStartup() {
5959

6060
new ApplicationContextRunner()
@@ -65,8 +65,8 @@ void missingArchUnitRuntimeDependencyEscalatesOnContextStartup() {
6565

6666
assertThat(context).hasFailed();
6767
assertThat(context.getStartupFailure().getCause())
68-
.isInstanceOf(BeanInstantiationException.class)
69-
.cause().isInstanceOf(MissingRuntimeDependencyException.class);
68+
.isInstanceOf(BeanInstantiationException.class)
69+
.cause().isInstanceOf(MissingRuntimeDependency.class);
7070

7171
context.close();
7272
});

0 commit comments

Comments
 (0)