Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit a0ace50

Browse files
committed
allow on start/stop callback to throw an error jooby-project#357
1 parent d3d0d01 commit a0ace50

File tree

18 files changed

+122
-92
lines changed

18 files changed

+122
-92
lines changed

coverage-report/src/test/java/org/jooby/js/JsApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class JsApp {
1010

11-
public static void main(final String[] args) throws Exception {
11+
public static void main(final String[] args) throws Throwable {
1212
File appjs = Paths.get("src", "test", "resources", "org", "jooby", "js", "app.js")
1313
.toFile();
1414
Jooby.run(new JsJooby().run(appjs), args);

coverage-report/src/test/java/org/jooby/js/JsAppFeature.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ public void apprsc() throws Exception {
1616
}
1717

1818
@Test
19-
public void appfile() throws Exception {
19+
public void appfile() throws Throwable {
2020
run(Paths.get("src", "test", "resources", "org", "jooby", "js", "appfile.js").toFile());
2121
}
2222

2323
private void run(final String filename) throws Exception {
2424
new JsJooby().run(new InputStreamReader(getClass().getResourceAsStream(filename))).get();
2525
}
2626

27-
private void run(final File filename) throws Exception {
27+
private void run(final File filename) throws Throwable {
2828
Jooby.main(new String[]{filename.getAbsolutePath() });
2929
}
3030

jooby-archetype/src/main/resources/archetype-resources/src/main/java/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class App extends Jooby {
1111
get("/", () -> "Hello World!");
1212
}
1313

14-
public static void main(final String[] args) throws Exception {
14+
public static void main(final String[] args) throws Throwable {
1515
run(App::new, args);
1616
}
1717

jooby-executor/src/test/java/org/jooby/exec/ExecTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.typesafe.config.ConfigFactory;
3232
import com.typesafe.config.ConfigValueFactory;
3333

34+
import javaslang.control.Try.CheckedRunnable;
35+
3436
@RunWith(PowerMockRunner.class)
3537
@PrepareForTest({Exec.class, Executors.class, ForkJoinPool.class, Thread.class })
3638
public class ExecTest {
@@ -41,7 +43,7 @@ public class ExecTest {
4143

4244
private Block onStop = unit -> {
4345
Env env = unit.get(Env.class);
44-
expect(env.onStop(unit.capture(Runnable.class))).andReturn(env);
46+
expect(env.onStop(unit.capture(CheckedRunnable.class))).andReturn(env);
4547
};
4648

4749
@Test
@@ -93,7 +95,7 @@ public void onStop() throws Exception {
9395
.run(unit -> {
9496
new Exec().configure(unit.get(Env.class), conf, unit.get(Binder.class));
9597
}, unit -> {
96-
Runnable stop = unit.captured(Runnable.class).iterator().next();
98+
CheckedRunnable stop = unit.captured(CheckedRunnable.class).iterator().next();
9799
stop.run();
98100
});
99101
}
@@ -116,7 +118,7 @@ public void onStopWithFailure() throws Exception {
116118
.run(unit -> {
117119
new Exec().configure(unit.get(Env.class), conf, unit.get(Binder.class));
118120
}, unit -> {
119-
Runnable stop = unit.captured(Runnable.class).iterator().next();
121+
CheckedRunnable stop = unit.captured(CheckedRunnable.class).iterator().next();
120122
stop.run();
121123
});
122124
}

jooby-hotreload/src/main/java/org/jooby/hotreload/AppModule.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ private void startApp() {
196196
.getDeclaredConstructors()[0].newInstance();
197197
}
198198
app.getClass().getMethod("start").invoke(app);
199-
200-
} catch (InvocationTargetException ex) {
201-
System.err.println("Error found while starting: " + mainClass);
202-
ex.printStackTrace();
203-
} catch (Exception ex) {
204-
System.err.println("Error found while starting: " + mainClass);
205-
ex.printStackTrace();
199+
} catch (Throwable ex) {
200+
System.err.println(mainClass + ".start() resulted in error");
201+
Throwable cause = ex;
202+
if (ex instanceof InvocationTargetException) {
203+
cause = ((InvocationTargetException) ex).getTargetException();
204+
}
205+
cause.printStackTrace();
206206
} finally {
207207
Thread.currentThread().setContextClassLoader(ctxLoader);
208208
}
@@ -212,11 +212,15 @@ private void startApp() {
212212
private void stopApp(final Object app) {
213213
try {
214214
app.getClass().getMethod("stop").invoke(app);
215-
} catch (Exception ex) {
216-
System.err.println("couldn't stop app");
215+
} catch (Throwable ex) {
216+
System.err.println(app.getClass().getName() + ".stop() resulted in error");
217217
ex.printStackTrace();
218218
} finally {
219-
loader.unload(module);
219+
try {
220+
loader.unload(module);
221+
} catch (Throwable ex) {
222+
// sshhhh
223+
}
220224
}
221225

222226
}

jooby-jdbc/src/test/java/org/jooby/jdbc/JdbcTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.easymock.Capture;
2424
import org.jooby.Env;
25+
import org.jooby.Jooby;
2526
import org.jooby.Routes;
2627
import org.junit.Test;
2728

@@ -35,6 +36,9 @@
3536
import com.typesafe.config.ConfigValueFactory;
3637
import com.zaxxer.hikari.HikariConfig;
3738

39+
import javaslang.control.Try.CheckedConsumer;
40+
import javaslang.control.Try.CheckedRunnable;
41+
3842
public class JdbcTest {
3943

4044
@Test(expected = IllegalArgumentException.class)
@@ -1055,22 +1059,22 @@ public Routes routes() throws UnsupportedOperationException {
10551059
}
10561060

10571061
@Override
1058-
public Env onStop(final Runnable shutdownTask) {
1062+
public Env onStop(final CheckedRunnable shutdownTask) {
10591063
throw new UnsupportedOperationException();
10601064
}
10611065

10621066
@Override
1063-
public List<Runnable> stopTasks() {
1067+
public List<CheckedConsumer<Jooby>> stopTasks() {
10641068
throw new UnsupportedOperationException();
10651069
}
10661070

10671071
@Override
1068-
public Env onStart(final Runnable task) {
1072+
public Env onStart(final CheckedRunnable task) {
10691073
throw new UnsupportedOperationException();
10701074
}
10711075

10721076
@Override
1073-
public List<Runnable> startTasks() {
1077+
public List<CheckedConsumer<Jooby>> startTasks() {
10741078
throw new UnsupportedOperationException();
10751079
}
10761080

jooby-maven-plugin/src/main/java/org/jooby/AssetMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8484
} catch (CompilationDone ex) {
8585
long end = System.currentTimeMillis();
8686
getLog().info("compilation took " + (end - start) + "ms");
87-
} catch (Exception ex) {
87+
} catch (Throwable ex) {
8888
throw new MojoFailureException("Can't compile assets for " + mainClass, ex);
8989
}
9090
}

jooby-maven-plugin/src/main/java/org/jooby/JoobyRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public JoobyRunner with(final Consumer<List<Route.Definition>> callback) {
5555
}
5656

5757
public void run(final String mainClass, final Consumer<Jooby> callback)
58-
throws Exception {
58+
throws Throwable {
5959
run(mainClass, (app, loader) -> callback.accept(app));
6060
}
6161

6262
public void run(final String mainClass, final BiConsumer<Jooby, ClassLoader> callback)
63-
throws Exception {
63+
throws Throwable {
6464
ClassLoader loader = Thread.currentThread().getContextClassLoader();
6565
try (URLClassLoader apploader = cp.toClassLoader()) {
6666
Thread.currentThread().setContextClassLoader(apploader);

jooby-maven-plugin/src/main/java/org/jooby/RouteProcessorMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
6060
process(app, srcdir, bindir);
6161
});
6262
} catch (ProcessDone ex) {
63-
} catch (Exception ex) {
63+
} catch (Throwable ex) {
6464
throw new MojoFailureException("Can't build route spec for: " + mainClass, ex);
6565
}
6666
}

jooby-metrics/src/test/java/demo/MetricsApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class MetricsApp extends Jooby {
3838
get("/", () -> "Hello metrics!");
3939
}
4040

41-
public static void main(final String[] args) throws Exception {
41+
public static void main(final String[] args) throws Throwable {
4242
new MetricsApp().start(args);
4343
}
4444
}

0 commit comments

Comments
 (0)