Skip to content

Commit 2dc458d

Browse files
committed
allow to rethrow bootstrap exceptions fix #576
1 parent ba68f3f commit 2dc458d

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

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

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ public EnvDep(final Predicate<String> predicate, final Consumer<Config> callback
693693

694694
private transient boolean defaultExecSet;
695695

696+
private boolean throwBootstrapException;
697+
696698
/**
697699
* Creates a new {@link Jooby} application.
698700
*/
@@ -1920,7 +1922,13 @@ public void start(final String... args) {
19201922
try {
19211923
start(args, null);
19221924
} catch (Throwable x) {
1923-
stop(Optional.of(x));
1925+
stop();
1926+
String msg = "An error occurred while starting the application:";
1927+
if (throwBootstrapException) {
1928+
throw new Err(Status.SERVICE_UNAVAILABLE, msg, x);
1929+
} else {
1930+
logger(this).error(msg, x);
1931+
}
19241932
}
19251933
}
19261934

@@ -2373,6 +2381,36 @@ public Jooby executor(final String name) {
23732381
return this;
23742382
}
23752383

2384+
/**
2385+
* If the application fails to start all the services are shutdown. Also, the exception is logged
2386+
* and usually the application is going to exit.
2387+
*
2388+
* This options turn off logging and rethrow the exception as {@link Err}. Here is an example:
2389+
*
2390+
* <pre>
2391+
* public class App extends Jooby {
2392+
* {
2393+
* throwBootstrapException();
2394+
* ...
2395+
* }
2396+
* }
2397+
*
2398+
* App app = new App();
2399+
*
2400+
* try {
2401+
* app.start();
2402+
* } catch (Err err) {
2403+
* Throwable cause = err.getCause();
2404+
* }
2405+
* </pre>
2406+
*
2407+
* @return
2408+
*/
2409+
public Jooby throwBootstrapException() {
2410+
this.throwBootstrapException = true;
2411+
return this;
2412+
}
2413+
23762414
/**
23772415
* Run app in javascript.
23782416
*
@@ -2782,13 +2820,6 @@ private static List<Config> modconf(final Collection<Object> bag) {
27822820
.collect(Collectors.toList());
27832821
}
27842822

2785-
/**
2786-
* Stop the application, close all the modules and stop the web server.
2787-
*/
2788-
public void stop() {
2789-
stop(Optional.empty());
2790-
}
2791-
27922823
/**
27932824
* Test if the application is up and running.
27942825
*
@@ -2798,12 +2829,14 @@ public boolean isStarted() {
27982829
return started.get();
27992830
}
28002831

2801-
private void stop(final Optional<Throwable> x) {
2832+
/**
2833+
* Stop the application, close all the modules and stop the web server.
2834+
*/
2835+
public void stop() {
28022836
if (started.compareAndSet(true, false)) {
28032837
Logger log = logger(this);
28042838

2805-
x.ifPresent(c -> log.error("An error occurred while starting the application:", c));
2806-
fireStop(injector, this, log, onStop);
2839+
fireStop(this, log, onStop);
28072840
if (injector != null) {
28082841
try {
28092842
injector.getInstance(Server.class).stop();
@@ -2817,7 +2850,7 @@ private void stop(final Optional<Throwable> x) {
28172850
}
28182851
}
28192852

2820-
private static void fireStop(final Injector injector, final Jooby app, final Logger log,
2853+
private static void fireStop(final Jooby app, final Logger log,
28212854
final List<CheckedConsumer<Registry>> onStop) {
28222855
// stop services
28232856
onStop.forEach(c -> Try.run(() -> c.accept(app))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jooby.issues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
5+
6+
import org.jooby.Err;
7+
import org.jooby.Jooby;
8+
import org.jooby.Status;
9+
import org.junit.Test;
10+
11+
public class Issue576 {
12+
13+
@Test
14+
public void shouldThrowBootstrapException() {
15+
IllegalStateException ies = new IllegalStateException("boot err");
16+
try {
17+
new Jooby() {
18+
{
19+
throwBootstrapException();
20+
21+
onStart(() -> {
22+
throw ies;
23+
});
24+
}
25+
}.start();
26+
fail();
27+
} catch (Err err) {
28+
assertEquals(Status.SERVICE_UNAVAILABLE.value(), err.statusCode());
29+
assertEquals(ies, err.getCause());
30+
}
31+
}
32+
33+
}

0 commit comments

Comments
 (0)