Skip to content

Commit 3277973

Browse files
committed
whoops module: finish + doc fix jooby-project#1618
1 parent 885ea8a commit 3277973

7 files changed

Lines changed: 124 additions & 10 deletions

File tree

‎docs/asciidoc/modules/gson.adoc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ JSON support using https://github.com/google/gson[Gson] library.
1717
import io.jooby.json.GsonModule;
1818
1919
{
20-
install(new GsonModule()); <1>
20+
install(new GsonModule()); <1>
2121
2222
get("/", ctx -> {
2323
MyObject myObject = ...;
@@ -34,10 +34,10 @@ import io.jooby.json.GsonModule;
3434
.Kotlin
3535
[source, kt, role="secondary"]
3636
----
37-
import io.jooby.json.GsonModule;
37+
import io.jooby.json.GsonModule
3838
3939
{
40-
install(GsonModule()) <1>
40+
install(GsonModule()) <1>
4141
4242
get("/") {
4343
val myObject = ...;

‎docs/asciidoc/modules/whoops.adoc‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
== Whoops
2+
3+
https://github.com/filp/whoops[Whoops] is an error handler framework for PHP. Out-of-the-box, it provides a pretty error interface
4+
that helps you debug your web projects, but at heart it's a simple yet powerful stacked error
5+
handling system.
6+
7+
=== Usage
8+
9+
1) Add the dependency:
10+
11+
[dependency, artifactId="jooby-whoops"]
12+
.
13+
14+
2) Install Whoops
15+
16+
.Java
17+
[source, java, role="primary"]
18+
----
19+
import io.jooby.whoops.WhoopsModule;
20+
21+
{
22+
install(new WhoopsModule()); <1>
23+
24+
get("/{id}", ctx -> {
25+
return ctx.path("id").intValue(); <2>
26+
});
27+
}
28+
----
29+
30+
.Kotlin
31+
[source, kt, role="secondary"]
32+
----
33+
import io.jooby.whoops.WhoopsModule
34+
35+
{
36+
install(WhoopsModule()) <1>
37+
38+
get("/{id}") {
39+
ctx.path("id").intValue() <2>
40+
}
41+
}
42+
----
43+
44+
<1> Install Whoops
45+
<2> Write some exceptional code (see next section)
46+
47+
=== Test
48+
49+
To test whoops we need to generate an exception. Our route is expecting to get an `integer` for
50+
the `id` variable.
51+
52+
- Open a browser and type `http://localhost:8080/NaN`
53+
54+
.Whoops in Action
55+
image::whoops.png[Whoops in Action]
56+
57+
=== How it works
58+
59+
Whoops only display application frames. That is:
60+
61+
A stacktrace element where we are able to find the source code. The whole stacktrace is available in the application log/console.
62+
63+
Whoops does nothing if there is no source code. An example of this is a `404` request:
64+
65+
----
66+
Not Found
67+
message: Page not found
68+
status code: 404
69+
----
70+
71+
The `404` error is generated by Jooby, not by application code. So whoops let the next exception
72+
handler (which is the default here) to deal with the exception.
73+
74+
If there is no source code to shows, whoops does nothing.
75+
76+
77+
If you have custom error handler just add them before whoops (or after). Remember whoops works
78+
like any other link:../#error-handler[Error Handler].
79+
80+
=== Production deploy
81+
82+
Because whoops only works with source code, whoops won't ever run in production deployment
83+
(unless you deploy with source code).
84+
85+
Still whoops is enabled and will try to find source code in the current application directory.
86+
87+
A better option is to set `whoops.enabled = false` in your application properties file. This will
88+
completely turn off whoops.

‎docs/images/whoops.png‎

279 KB
Loading

‎modules/jooby-bom/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
<swagger.version>2.1.1</swagger.version>
111111
<truth.version>1.0.1</truth.version>
112112
<unbescape.version>1.1.6.RELEASE</unbescape.version>
113-
<undertow.version>2.0.28.Final</undertow.version>
113+
<undertow.version>2.0.29.Final</undertow.version>
114114
<versions-maven-plugin.version>2.7</versions-maven-plugin.version>
115115
<weld.version>3.1.3.Final</weld.version>
116116
</properties>

‎modules/jooby-whoops/src/main/java/io/jooby/internal/whoops/Frame.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.File;
99
import java.nio.file.Files;
10+
import java.nio.file.Path;
1011
import java.util.Collections;
1112
import java.util.LinkedList;
1213
import java.util.List;
@@ -102,9 +103,16 @@ static Frame toFrame(final SourceLocator locator, final Throwable cause,
102103
final StackTraceElement e) {
103104
int line = Math.max(e.getLineNumber(), 1);
104105
String className = ofNullable(e.getClassName()).orElse("~unknown");
105-
String filename = ofNullable(e.getFileName()).orElse(className.replace(".", File.separator));
106-
107-
SourceLocator.Source source = locator.source(filename);
106+
String[] names = className.split("\\.");
107+
String filename = ofNullable(e.getFileName())
108+
.orElse(names[names.length - 1]);
109+
110+
StringBuilder path = new StringBuilder();
111+
Stream.of(names)
112+
.limit(names.length - 1)
113+
.forEach(it -> path.append(it).append(File.separator));
114+
path.append(names[names.length - 1]);
115+
SourceLocator.Source source = locator.source(path.toString());
108116
SourceLocator.Preview preview = source.preview(line, SAMPLE_SIZE);
109117

110118
Frame frame = new Frame();

‎modules/jooby-whoops/src/main/java/io/jooby/internal/whoops/Utils.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
16
package io.jooby.internal.whoops;
27

38
import java.util.Collection;

‎modules/jooby-whoops/src/main/java/io/jooby/whoops/WhoopsModule.java‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby.whoops;
77

8+
import com.typesafe.config.Config;
89
import io.jooby.Extension;
910
import io.jooby.Jooby;
1011
import io.jooby.internal.whoops.Whoops;
@@ -33,6 +34,8 @@
3334
* @since 2.8.0
3435
*/
3536
public class WhoopsModule implements Extension {
37+
private static final String ENABLED = "whoops.enabled";
38+
3639
private Path basedir;
3740

3841
/**
@@ -52,10 +55,20 @@ public WhoopsModule() {
5255
}
5356

5457
@Override public void install(@Nonnull Jooby application) {
55-
Whoops whoops = new Whoops(basedir, application.getLog());
58+
Config config = application.getConfig();
59+
60+
boolean enabled = config.hasPath(ENABLED)
61+
? config.getBoolean(ENABLED)
62+
: application.getEnvironment().isActive("dev", "test");
63+
64+
if (enabled) {
65+
Whoops whoops = new Whoops(basedir, application.getLog());
5666

57-
application.assets("/whoops/*", getClass().getPackage().getName().replace(".", "/"));
67+
application.assets("/whoops/*", getClass().getPackage().getName().replace(".", "/"));
5868

59-
application.error(whoops);
69+
application.error(whoops);
70+
} else {
71+
application.getLog().debug("Whoops is disabled");
72+
}
6073
}
6174
}

0 commit comments

Comments
 (0)