Skip to content

Commit 83c4a1f

Browse files
committed
whoops fail with a classnotfound error fix #402
1 parent ddb50e0 commit 83c4a1f

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

jooby-whoops/pom.xml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,28 @@
3030
<configuration>
3131
<overwrite>true</overwrite>
3232
<includes>
33-
<include>org.ow2.asm:asm</include>
33+
<include>com.mitchellbosecke:pebble</include>
34+
<include>com.coverity.security:coverity-escapers</include>
3435
</includes>
3536
<rules>
37+
<!-- pebble -->
3638
<rule>
37-
<pattern>com.mitchellbosecke.pebble.*</pattern>
39+
<pattern>com.mitchellbosecke.*</pattern>
3840
<result>org.jooby.internal.pebble.$@1</result>
3941
</rule>
4042
<rule>
41-
<pattern>com.mitchellbosecke.pebble.**.*</pattern>
43+
<pattern>com.mitchellbosecke.**.*</pattern>
4244
<result>org.jooby.internal.pebble.@1.$@2</result>
4345
</rule>
46+
<!-- coverity -->
47+
<rule>
48+
<pattern>com.coverity.*</pattern>
49+
<result>org.jooby.internal.coverity.$@1</result>
50+
</rule>
51+
<rule>
52+
<pattern>com.coverity.**.*</pattern>
53+
<result>org.jooby.internal.coverity.@1.$@2</result>
54+
</rule>
4455
<keep>
4556
<pattern>org.jooby.**</pattern>
4657
</keep>
@@ -74,6 +85,7 @@
7485
<dependency>
7586
<groupId>com.mitchellbosecke</groupId>
7687
<artifactId>pebble</artifactId>
88+
<optional>true</optional>
7789
</dependency>
7890

7991
<!-- Test dependencies -->

jooby-whoops/src/main/java/org/jooby/whoops/Whoops.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
* <code>prod-like</code> environments:
129129
* </p>
130130
*
131-
* <pre>{@code
131+
* <pre>{@code
132132
* {
133133
* on("dev", () -> {
134134
* use(new Whoops());
@@ -155,14 +155,32 @@ public class Whoops implements Jooby.Module {
155155
/** The logging system. */
156156
private final Logger log = LoggerFactory.getLogger(getClass());
157157

158+
private final int maxFrameSize;
159+
160+
/**
161+
* Creates a new {@link Whoops} module.
162+
*
163+
* @param maxFrameSize Max number of frame to show in the pretty error page.
164+
*/
165+
public Whoops(final int maxFrameSize) {
166+
this.maxFrameSize = maxFrameSize;
167+
}
168+
169+
/**
170+
* Creates a new {@link Whoops} module with max frame size of 8.
171+
*/
172+
public Whoops() {
173+
this(8);
174+
}
175+
158176
@Override
159177
public void configure(final Env env, final Config conf, final Binder binder) {
160178
boolean whoops = conf.hasPath("whoops.enabled")
161179
? conf.getBoolean("whoops.enabled")
162180
: "dev".equals(env.name());
163181
if (whoops) {
164182
ClassLoader loader = env.routes().getClass().getClassLoader();
165-
Handler handler = prettyPage(loader, SourceLocator.local(), log);
183+
Handler handler = prettyPage(loader, SourceLocator.local(), maxFrameSize, log);
166184
env.routes().err(tryPage(handler, log));
167185
}
168186
}
@@ -174,7 +192,7 @@ static Handler tryPage(final Handler handler, final Logger log) {
174192
}
175193

176194
private static Handler prettyPage(final ClassLoader loader, final SourceLocator locator,
177-
final Logger log) {
195+
final int maxStackSize, final Logger log) {
178196
String css = readString(loader, "css/whoops.base.css");
179197
String clipboard = readString(loader, "js/clipboard.min.js");
180198
String js = readString(loader, "js/whoops.base.js");
@@ -234,6 +252,9 @@ private static Handler prettyPage(final ClassLoader loader, final SourceLocator
234252

235253
frames.addAll(frames(loader, locator, head));
236254

255+
// truncate frames
256+
frames = frames.subList(0, Math.min(maxStackSize, frames.size()));
257+
237258
Map<String, Object> context = ImmutableMap.<String, Object> builder()
238259
.put("stylesheet", css)
239260
.put("zepto", zepto)
@@ -252,7 +273,7 @@ private static Handler prettyPage(final ClassLoader loader, final SourceLocator
252273

253274
log.error("execution of: {}{} resulted in exception\nRoute:\n{}\n\nStacktrace:",
254275
req.method(), req.path(), req.route().print(6), err);
255-
rsp.send(writer.toString());
276+
rsp.type(MediaType.html).send(writer.toString());
256277
}
257278
};
258279
}
@@ -334,8 +355,9 @@ static String locationOf(final Class clazz) {
334355
}
335356
String cfile = clazz.getName().replace(".", "/") + ".class";
336357
String relativePath = path.replace(cfile, "");
337-
return new File(System.getProperty("user.dir")).toPath()
338-
.relativize(Paths.get(relativePath))
358+
return new File(System.getProperty("user.dir"))
359+
.toPath()
360+
.relativize(Paths.get(relativePath).toFile().getCanonicalFile().toPath())
339361
.toString();
340362
}).getOrElse("~unknown"))
341363
.orElse("~unknown");

0 commit comments

Comments
 (0)