From a61429d2d134775ba9835da024fd732ac96617c4 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Thu, 31 Mar 2016 21:19:19 -0400 Subject: [PATCH] #5 show nicer stacktrace page --- src/main/java/io/jare/tk/TkApp.java | 29 ++++++++++++++++++++++++++- src/main/java/io/jare/tk/TkRelay.java | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/jare/tk/TkApp.java b/src/main/java/io/jare/tk/TkApp.java index 9cea601..9a05fb2 100644 --- a/src/main/java/io/jare/tk/TkApp.java +++ b/src/main/java/io/jare/tk/TkApp.java @@ -28,8 +28,10 @@ import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.takes.Take; import org.takes.facets.auth.TkSecure; +import org.takes.facets.fallback.TkFallback; import org.takes.facets.flash.TkFlash; import org.takes.facets.fork.FkAuthenticated; import org.takes.facets.fork.FkFixed; @@ -38,6 +40,10 @@ import org.takes.facets.fork.FkRegex; import org.takes.facets.fork.TkFork; import org.takes.facets.forward.TkForward; +import org.takes.misc.Opt; +import org.takes.rs.RsWithBody; +import org.takes.rs.RsWithStatus; +import org.takes.rs.RsWithType; import org.takes.tk.TkClasspath; import org.takes.tk.TkFiles; import org.takes.tk.TkGzip; @@ -57,6 +63,7 @@ * @checkstyle MultipleStringLiteralsCheck (500 lines) * @checkstyle ClassFanOutComplexityCheck (500 lines) */ +@SuppressWarnings("PMD.ExcessiveImports") public final class TkApp extends TkWrap { /** @@ -116,7 +123,27 @@ private static Take make(final Base base) throws IOException { */ private static Take regex(final Base base) throws IOException { return new TkFork( - new FkHost("relay.jare.io", new TkRelay(base)), + new FkHost( + "relay.jare.io", + new TkFallback( + new TkRelay(base), + req -> new Opt.Single<>( + new RsWithType( + new RsWithBody( + new RsWithStatus(req.code()), + String.format( + // @checkstyle LineLength (1 line) + "Please, submit this stacktrace to GitHub and we'll try to help: https://github.com/yegor256/jare/issues\n\n%s", + ExceptionUtils.getStackTrace( + req.throwable() + ) + ) + ), + "text/plain" + ) + ) + ) + ), new FkRegex("/robots.txt", ""), new FkRegex( "/xsl/[a-z\\-]+\\.xsl", diff --git a/src/main/java/io/jare/tk/TkRelay.java b/src/main/java/io/jare/tk/TkRelay.java index 51e715a..e24f817 100644 --- a/src/main/java/io/jare/tk/TkRelay.java +++ b/src/main/java/io/jare/tk/TkRelay.java @@ -114,7 +114,7 @@ private String path(final URI uri) throws HttpException { final Iterator domains = this.base.domain(host); if (!domains.hasNext()) { throw new HttpException( - HttpURLConnection.HTTP_NOT_FOUND, + HttpURLConnection.HTTP_BAD_REQUEST, String.format("domain \"%s\" is not registered", host) ); }