Skip to content

Commit c30c178

Browse files
committed
Allow to throw an exception from Module.configure fix #552
1 parent 51fc677 commit c30c178

10 files changed

Lines changed: 108 additions & 165 deletions

File tree

jooby-assets/src/main/java/org/jooby/assets/Assets.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import com.typesafe.config.Config;
3131
import com.typesafe.config.ConfigFactory;
3232

33-
import javaslang.control.Try;
34-
3533
/**
3634
* <h1>assets</h1>
3735
* <p>
@@ -302,38 +300,35 @@
302300
public class Assets implements Jooby.Module {
303301

304302
@Override
305-
public void configure(final Env env, final Config config, final Binder binder) {
306-
Try.run(() -> {
307-
boolean dev = "dev".equals(env.name());
308-
ClassLoader loader = getClass().getClassLoader();
309-
Config conf = conf(dev, loader, config);
310-
String cpath = config.getString("application.path");
311-
AssetCompiler compiler = new AssetCompiler(loader, conf);
312-
313-
Router routes = env.router();
314-
routes.use("*", "*", new AssetVars(compiler, cpath)).name("/assets/vars");
315-
// live compiler?
316-
boolean watch = conf.hasPath("assets.watch") ? conf.getBoolean("assets.watch") : dev;
317-
if (watch) {
318-
LiveCompiler liveCompiler = new LiveCompiler(conf, compiler);
319-
env.onStart(liveCompiler::start);
320-
env.onStop(liveCompiler::stop);
321-
routes.use("*", "*", liveCompiler).name("/assets/compiler");
322-
}
303+
public void configure(final Env env, final Config config, final Binder binder) throws Throwable {
304+
boolean dev = "dev".equals(env.name());
305+
ClassLoader loader = getClass().getClassLoader();
306+
Config conf = conf(dev, loader, config);
307+
String cpath = config.getString("application.path");
308+
AssetCompiler compiler = new AssetCompiler(loader, conf);
323309

324-
AssetHandler handler = dev
325-
? new AssetHandlerWithCompiler("/", compiler)
326-
: new AssetHandler("/");
310+
Router routes = env.router();
311+
routes.use("*", "*", new AssetVars(compiler, cpath)).name("/assets/vars");
312+
// live compiler?
313+
boolean watch = conf.hasPath("assets.watch") ? conf.getBoolean("assets.watch") : dev;
314+
if (watch) {
315+
LiveCompiler liveCompiler = new LiveCompiler(conf, compiler);
316+
env.onStart(liveCompiler::start);
317+
env.onStop(liveCompiler::stop);
318+
routes.use("*", "*", liveCompiler).name("/assets/compiler");
319+
}
327320

328-
handler.etag(conf.getBoolean("assets.etag"))
329-
.cdn(conf.getString("assets.cdn"))
330-
.lastModified(conf.getBoolean("assets.lastModified"));
321+
AssetHandler handler = dev
322+
? new AssetHandlerWithCompiler("/", compiler)
323+
: new AssetHandler("/");
331324

332-
handler.maxAge(conf.getString("assets.cache.maxAge"));
325+
handler.etag(conf.getBoolean("assets.etag"))
326+
.cdn(conf.getString("assets.cdn"))
327+
.lastModified(conf.getBoolean("assets.lastModified"));
333328

334-
compiler.patterns().forEach(pattern -> routes.get(pattern, handler));
329+
handler.maxAge(conf.getString("assets.cache.maxAge"));
335330

336-
}).get();
331+
compiler.patterns().forEach(pattern -> routes.get(pattern, handler));
337332
}
338333

339334
private Config conf(final boolean dev, final ClassLoader loader, final Config conf) {

jooby-camel/src/main/java/org/jooby/camel/Camel.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public Camel routes(final Configurer<RouteBuilder> routes) {
316316
}
317317

318318
@Override
319-
public void configure(final Env env, final Config config, final Binder binder) {
319+
public void configure(final Env env, final Config config, final Binder binder) throws Throwable {
320320
Config $camel = config.getConfig("camel");
321321
DefaultCamelContext ctx = configure(new DefaultCamelContext(), $camel
322322
.withoutPath("shutdown")
@@ -350,24 +350,14 @@ public void configure(final Env env, final Config config, final Binder binder) {
350350
* Components etc..
351351
*/
352352
if (configurer != null) {
353-
try {
354-
configurer.configure(ctx, config);
355-
} catch (RuntimeException ex) {
356-
throw ex;
357-
} catch (Exception ex) {
358-
throw new IllegalStateException("Context configurer resulted in error", ex);
359-
}
353+
configurer.configure(ctx, config);
360354
}
361355

362356
/**
363357
* Routes
364358
*/
365359
if (routes != null) {
366-
try {
367-
routes(routes, ctx, config);
368-
} catch (Exception ex) {
369-
throw new IllegalStateException("context.addRoutes(RouteBuilder) resulted in error", ex);
370-
}
360+
routes(routes, ctx, config);
371361
}
372362

373363
/**

jooby-camel/src/test/java/org/jooby/camel/CamelTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ public void withConfigurerErr() throws Exception {
640640
}
641641

642642
@SuppressWarnings("unchecked")
643-
@Test(expected = IllegalStateException.class)
643+
@Test(expected = IOException.class)
644644
public void withConfigurerCheckedErr() throws Exception {
645645
Config camel = defConfig;
646646
new MockUnit(Env.class, Config.class, Binder.class)
@@ -861,7 +861,7 @@ public void withRoutes() throws Exception {
861861
}
862862

863863
@SuppressWarnings("unchecked")
864-
@Test(expected = IllegalStateException.class)
864+
@Test(expected = Exception.class)
865865
public void withRoutesErr() throws Exception {
866866
Config camel = defConfig;
867867
new MockUnit(Env.class, Config.class, Binder.class)

jooby-ftl/src/main/java/org/jooby/ftl/Ftl.java

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -169,40 +169,35 @@ public Ftl doWith(final BiConsumer<Configuration, Config> configurer) {
169169
}
170170

171171
@Override
172-
public void configure(final Env env, final Config config, final Binder binder) {
173-
try {
174-
Configuration freemarker = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
175-
log.debug("Freemarker: {}", Configuration.getVersion());
176-
freemarker.setSettings(properties(config));
177-
freemarker.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader(), prefix));
178-
179-
// cache
180-
if ("dev".equals(env.name()) || config.getString("freemarker.cache").isEmpty()) {
181-
// noop cache
182-
freemarker.setCacheStorage(NullCacheStorage.INSTANCE);
183-
} else {
184-
freemarker.setCacheStorage(
185-
new GuavaCacheStorage(
186-
CacheBuilder
187-
.from(config.getString("freemarker.cache"))
188-
.build()
189-
));
190-
}
191-
192-
if (configurer != null) {
193-
configurer.accept(freemarker, config);
194-
}
195-
196-
binder.bind(Configuration.class).toInstance(freemarker);
197-
198-
Engine engine = new Engine(freemarker, suffix, new XssDirective(env));
199-
200-
Multibinder.newSetBinder(binder, Renderer.class)
201-
.addBinding().toInstance(engine);
202-
203-
} catch (TemplateException ex) {
204-
throw new IllegalStateException("Freemarker configuration results in error", ex);
172+
public void configure(final Env env, final Config config, final Binder binder)
173+
throws TemplateException {
174+
Configuration freemarker = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
175+
log.debug("Freemarker: {}", Configuration.getVersion());
176+
freemarker.setSettings(properties(config));
177+
freemarker.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader(), prefix));
178+
179+
// cache
180+
if ("dev".equals(env.name()) || config.getString("freemarker.cache").isEmpty()) {
181+
// noop cache
182+
freemarker.setCacheStorage(NullCacheStorage.INSTANCE);
183+
} else {
184+
freemarker.setCacheStorage(
185+
new GuavaCacheStorage(
186+
CacheBuilder
187+
.from(config.getString("freemarker.cache"))
188+
.build()));
205189
}
190+
191+
if (configurer != null) {
192+
configurer.accept(freemarker, config);
193+
}
194+
195+
binder.bind(Configuration.class).toInstance(freemarker);
196+
197+
Engine engine = new Engine(freemarker, suffix, new XssDirective(env));
198+
199+
Multibinder.newSetBinder(binder, Renderer.class)
200+
.addBinding().toInstance(engine);
206201
}
207202

208203
@Override

jooby-ftl/src/test/java/org/jooby/ftl/FtlTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void defaultsNoDev() throws Exception {
170170
});
171171
}
172172

173-
@Test(expected = IllegalStateException.class)
173+
@Test(expected = TemplateException.class)
174174
public void err() throws Exception {
175175

176176
Properties props = new Properties();

jooby-scanner/src/main/java/org/jooby/scanner/Scanner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ public void configure(final Env env, final Config conf, final Binder binder) {
261261
.filter(once)
262262
.map(loadClass)
263263
.filter(C)
264-
.forEach(klass -> ((Jooby.Module) newObject(klass)).configure(env, conf, binder));
264+
.forEach(klass -> Try
265+
.run(() -> ((Jooby.Module) newObject(klass)).configure(env, conf, binder)).get());
265266

266267
/** Apps: */
267268
result.getNamesOfSubclassesOf(Jooby.class)

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
import org.slf4j.LoggerFactory;
132132

133133
import com.google.common.base.Joiner;
134+
import com.google.common.base.Throwables;
134135
import com.google.common.collect.ImmutableList;
135136
import com.google.common.collect.ImmutableMap;
136137
import com.google.common.collect.ImmutableSet;
@@ -524,7 +525,7 @@ default Config config() {
524525
* @param conf The current config object. Not null.
525526
* @param binder A guice binder. Not null.
526527
*/
527-
void configure(Env env, Config conf, Binder binder);
528+
void configure(Env env, Config conf, Binder binder) throws Throwable;
528529

529530
}
530531

@@ -2427,7 +2428,7 @@ private static List<Object> processEnvDep(final Set<Object> src, final Env env)
24272428
}
24282429

24292430
private Injector bootstrap(final Config args,
2430-
final Consumer<List<Route.Definition>> rcallback) throws Exception {
2431+
final Consumer<List<Route.Definition>> rcallback) throws Throwable {
24312432
Config appconf = ConfigFactory.parseResources("application.conf");
24322433
Config initconf = srcconf == null ? appconf : srcconf.withFallback(appconf);
24332434
List<Config> modconf = modconf(this.bag);
@@ -2575,18 +2576,21 @@ private Injector bootstrap(final Config args,
25752576

25762577
/** modules, routes, parsers, renderers and websockets */
25772578
Set<Object> routeClasses = new HashSet<>();
2578-
bag.forEach(it -> bindService(
2579-
this.bag,
2580-
finalConfig,
2581-
finalEnv,
2582-
rm,
2583-
binder,
2584-
definitions,
2585-
sockets,
2586-
ehandlers,
2587-
parsers,
2588-
renderers,
2589-
routeClasses).accept(it));
2579+
for (Object it : bag) {
2580+
Try.run(() -> bindService(
2581+
this.bag,
2582+
finalConfig,
2583+
finalEnv,
2584+
rm,
2585+
binder,
2586+
definitions,
2587+
sockets,
2588+
ehandlers,
2589+
parsers,
2590+
renderers,
2591+
routeClasses).accept(it))
2592+
.getOrElseThrow(Throwables::propagate);
2593+
}
25902594

25912595
parsers.addBinding().toInstance(new DateParser(dateFormat));
25922596
parsers.addBinding().toInstance(new LocalDateParser(dateTimeFormatter));
@@ -2711,7 +2715,7 @@ private static Provider<Session.Definition> session(final Config $session,
27112715
};
27122716
}
27132717

2714-
private static Consumer<? super Object> bindService(final Set<Object> src,
2718+
private static CheckedConsumer<? super Object> bindService(final Set<Object> src,
27152719
final Config conf,
27162720
final Env env,
27172721
final RouteMetadata rm,
@@ -2729,18 +2733,20 @@ private static Consumer<? super Object> bindService(final Set<Object> src,
27292733
int to = src.size();
27302734
// collect any route a module might add
27312735
if (to > from) {
2732-
normalize(new ArrayList<>(src).subList(from, to), env, rm, null)
2733-
.forEach(e -> bindService(src,
2734-
conf,
2735-
env,
2736-
rm,
2737-
binder,
2738-
definitions,
2739-
sockets,
2740-
ehandlers,
2741-
parsers,
2742-
renderers,
2743-
routeClasses).accept(e));
2736+
List<Object> elements = normalize(new ArrayList<>(src).subList(from, to), env, rm, null);
2737+
for (Object e : elements) {
2738+
bindService(src,
2739+
conf,
2740+
env,
2741+
rm,
2742+
binder,
2743+
definitions,
2744+
sockets,
2745+
ehandlers,
2746+
parsers,
2747+
renderers,
2748+
routeClasses).accept(e);
2749+
}
27442750
}
27452751
} else if (it instanceof Route.Definition) {
27462752
Route.Definition rdef = (Definition) it;
@@ -3025,14 +3031,11 @@ private Config defaultConfig(final Config config) {
30253031
* @param env Application env.
30263032
* @param config The configuration object.
30273033
* @param binder A Guice binder.
3034+
* @throws Throwable If module bootstrap fails.
30283035
*/
30293036
private static void install(final Jooby.Module module, final Env env, final Config config,
3030-
final Binder binder) {
3031-
try {
3032-
module.configure(env, config, binder);
3033-
} catch (Exception ex) {
3034-
throw new IllegalStateException("Error found on module: " + module.getClass().getName(), ex);
3035-
}
3037+
final Binder binder) throws Throwable {
3038+
module.configure(env, config, binder);
30363039
}
30373040

30383041
/**

jooby/src/main/java/org/jooby/internal/ServerLookup.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,18 @@ public class ServerLookup implements Module {
3232
private Jooby.Module delegate = null;
3333

3434
@Override
35-
public void configure(final Env env, final Config config, final Binder binder) {
35+
public void configure(final Env env, final Config config, final Binder binder) throws Throwable {
3636
if (config.hasPath("server.module")) {
37-
try {
38-
delegate = (Jooby.Module) getClass().getClassLoader()
39-
.loadClass(config.getString("server.module")).newInstance();
40-
delegate.configure(env, config, binder);
41-
} catch (Exception ex) {
42-
throw new IllegalStateException("No " + Server.class.getName()
43-
+ " implementation was found.", ex);
44-
}
37+
delegate = (Jooby.Module) getClass().getClassLoader()
38+
.loadClass(config.getString("server.module")).newInstance();
39+
delegate.configure(env, config, binder);
4540
}
4641
}
4742

4843
@Override
4944
public Config config() {
5045
return ConfigFactory.parseResources(Server.class, "server.conf")
51-
.withFallback(ConfigFactory.parseResources(Server.class, "server-defaults.conf"));
46+
.withFallback(ConfigFactory.parseResources(Server.class, "server-defaults.conf"));
5247
}
5348

5449
}

0 commit comments

Comments
 (0)