diff --git a/src/main/java/org/takes/facets/auth/PsBasic.java b/src/main/java/org/takes/facets/auth/PsBasic.java index 49daf1b6f..3f6ef1d41 100644 --- a/src/main/java/org/takes/facets/auth/PsBasic.java +++ b/src/main/java/org/takes/facets/auth/PsBasic.java @@ -249,7 +249,10 @@ public Opt enter(final String user, final String pwd) { ) ); } catch (final UnsupportedEncodingException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to decode URN '%s'", urn.get()), + ex + ); } } else { identity = new Opt.Empty<>(); @@ -302,7 +305,10 @@ private Opt urn(final String user, final String pwd) { ) ); } catch (final UnsupportedEncodingException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to encode user name or password", + ex + ); } final Opt opt; if (urn == null) { diff --git a/src/main/java/org/takes/facets/auth/codecs/CcCompact.java b/src/main/java/org/takes/facets/auth/codecs/CcCompact.java index 8b6af71a3..1052ee39b 100644 --- a/src/main/java/org/takes/facets/auth/codecs/CcCompact.java +++ b/src/main/java/org/takes/facets/auth/codecs/CcCompact.java @@ -54,7 +54,10 @@ public byte[] encode(final Identity identity) { stream.writeUTF(ent.getValue()); } } catch (final IOException ex) { - throw new IllegalArgumentException(ex); + throw new IllegalArgumentException( + "Failed to encode the identity", + ex + ); } return data.toByteArray(); } diff --git a/src/main/java/org/takes/facets/auth/social/PsFacebook.java b/src/main/java/org/takes/facets/auth/social/PsFacebook.java index fda70f1d1..d389b1814 100644 --- a/src/main/java/org/takes/facets/auth/social/PsFacebook.java +++ b/src/main/java/org/takes/facets/auth/social/PsFacebook.java @@ -188,11 +188,12 @@ private User fetch(final String token) { this.requestor, new DefaultJsonMapper(), Version.LATEST - ).fetchObject( - "me", User.class - ); + ).fetchObject("me", User.class); } catch (final FacebookException ex) { - throw new IllegalArgumentException(ex); + throw new IllegalArgumentException( + "Failed to fetch object from Facebook token", + ex + ); } } diff --git a/src/main/java/org/takes/facets/fork/FkHitRefresh.java b/src/main/java/org/takes/facets/fork/FkHitRefresh.java index 1a8fd73f5..5dcec6194 100644 --- a/src/main/java/org/takes/facets/fork/FkHitRefresh.java +++ b/src/main/java/org/takes/facets/fork/FkHitRefresh.java @@ -88,7 +88,10 @@ public FkHitRefresh(final File file, final List cmd, try { new ProcessBuilder().command(cmd).start(); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to run command '%s'", cmd), + ex + ); } }, tke diff --git a/src/main/java/org/takes/http/BkParallel.java b/src/main/java/org/takes/http/BkParallel.java index 6d0d5232d..8955200ec 100644 --- a/src/main/java/org/takes/http/BkParallel.java +++ b/src/main/java/org/takes/http/BkParallel.java @@ -80,7 +80,10 @@ public BkParallel(final Back back, final ExecutorService svc) { try { back.accept(socket); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Socket wasn't accepted by the back", + ex + ); } } ) diff --git a/src/main/java/org/takes/http/BkTimeable.java b/src/main/java/org/takes/http/BkTimeable.java index d8e494f5b..a10e2c922 100644 --- a/src/main/java/org/takes/http/BkTimeable.java +++ b/src/main/java/org/takes/http/BkTimeable.java @@ -75,7 +75,10 @@ public void run() { TimeUnit.SECONDS.sleep(1L); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Interrupted while waiting", + ex + ); } } } diff --git a/src/main/java/org/takes/http/FtCli.java b/src/main/java/org/takes/http/FtCli.java index d66977e70..41f6d9a18 100644 --- a/src/main/java/org/takes/http/FtCli.java +++ b/src/main/java/org/takes/http/FtCli.java @@ -115,7 +115,10 @@ public void start(final Exit exit) throws IOException { try { front.start(this.exit(exit)); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to start the front", + ex + ); } } ); diff --git a/src/main/java/org/takes/http/FtRemote.java b/src/main/java/org/takes/http/FtRemote.java index 7ad69ea47..ba89541c5 100644 --- a/src/main/java/org/takes/http/FtRemote.java +++ b/src/main/java/org/takes/http/FtRemote.java @@ -123,7 +123,10 @@ public void exec(final FtRemote.Script script) throws Exception { } ); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to start the app thread", + ex + ); } } ); @@ -131,12 +134,15 @@ public void exec(final FtRemote.Script script) throws Exception { try { if (!latch.await(10L, TimeUnit.SECONDS)) { throw new IllegalArgumentException( - "failed to start the app" + "Failed to start the app" ); } } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Interrupted while waiting for latch", + ex + ); } final String protocol; if (this.secured) { @@ -157,7 +163,10 @@ public void exec(final FtRemote.Script script) throws Exception { thread.join(); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Thread waiting interrupted", + ex + ); } } diff --git a/src/main/java/org/takes/http/Options.java b/src/main/java/org/takes/http/Options.java index 25351b865..8e62a6022 100644 --- a/src/main/java/org/takes/http/Options.java +++ b/src/main/java/org/takes/http/Options.java @@ -171,7 +171,7 @@ private static Map asMap(final Iterable args) { final Matcher matcher = ptn.matcher(arg); if (!matcher.matches()) { throw new IllegalStateException( - String.format("can't parse this argument: '%s'", arg) + String.format("Can't parse this argument: '%s'", arg) ); } final String value = matcher.group(2); diff --git a/src/main/java/org/takes/misc/Href.java b/src/main/java/org/takes/misc/Href.java index edc1d4882..b057d72be 100644 --- a/src/main/java/org/takes/misc/Href.java +++ b/src/main/java/org/takes/misc/Href.java @@ -265,7 +265,10 @@ private static String encode(final String txt) { txt, Charset.defaultCharset().name() ); } catch (final UnsupportedEncodingException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to encode '%s'", txt), + ex + ); } } @@ -280,7 +283,10 @@ private static String decode(final String txt) { txt, Charset.defaultCharset().name() ); } catch (final UnsupportedEncodingException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to decode '%s'", txt), + ex + ); } } diff --git a/src/main/java/org/takes/rq/RqHeaders.java b/src/main/java/org/takes/rq/RqHeaders.java index 14bc2c585..e16fe16c2 100644 --- a/src/main/java/org/takes/rq/RqHeaders.java +++ b/src/main/java/org/takes/rq/RqHeaders.java @@ -103,7 +103,7 @@ public List header(final CharSequence key) list = new VerboseList<>( Collections.emptyList(), new FormattedText( - "there are no headers by name \"%s\" among %d others: %s", + "There are no headers by name \"%s\" among %d others: %s", key, this.map().size(), this.map().keySet() @@ -113,7 +113,7 @@ public List header(final CharSequence key) list = new VerboseList<>( values, new FormattedText( - "there are only %d headers by name \"%s\"", + "There are only %d headers by name \"%s\"", values.size(), key ) @@ -139,18 +139,22 @@ private Map> map() throws IOException { if (!head.hasNext()) { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, - "a valid request must contain at least one line in the head" + "A valid request must contain at least one line in the head" ); } head.next(); final Map> map = new HashMap<>(0); + int pos = 1; while (head.hasNext()) { final String line = head.next(); final String[] parts = line.split(":", 2); if (parts.length < 2) { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, - String.format("invalid HTTP header: \"%s\"", line) + String.format( + "Invalid HTTP header on line #%d: \"%s\"", + pos, line + ) ); } final String key = new UncheckedText( @@ -164,6 +168,7 @@ private Map> map() throws IOException { new Trimmed(new TextOf(parts[1])) ).asString() ); + pos += 1; } return map; } @@ -232,7 +237,7 @@ public String single(final CharSequence name) throws IOException { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, String.format( - "header \"%s\" is mandatory, not found among %s", + "Header \"%s\" is mandatory, not found among %s", name, this.names() ) ); diff --git a/src/main/java/org/takes/rq/form/RqFormBase.java b/src/main/java/org/takes/rq/form/RqFormBase.java index 95b61013c..139f38ea8 100644 --- a/src/main/java/org/takes/rq/form/RqFormBase.java +++ b/src/main/java/org/takes/rq/form/RqFormBase.java @@ -117,13 +117,17 @@ public Iterable names() throws IOException { * @return Decoded */ private static String decode(final Text txt) { + final String body = new UncheckedText(txt).asString(); try { return URLDecoder.decode( - new UncheckedText(txt).asString(), + body, Charset.defaultCharset().name() ); } catch (final UnsupportedEncodingException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to decode '%s'", body), + ex + ); } } diff --git a/src/main/java/org/takes/rq/form/RqFormFake.java b/src/main/java/org/takes/rq/form/RqFormFake.java index 78ad13e72..641d59219 100644 --- a/src/main/java/org/takes/rq/form/RqFormFake.java +++ b/src/main/java/org/takes/rq/form/RqFormFake.java @@ -117,7 +117,10 @@ private static String encode(final CharSequence txt) { txt.toString(), Charset.defaultCharset().name() ); } catch (final UnsupportedEncodingException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to encode '%s'", txt), + ex + ); } } } diff --git a/src/main/java/org/takes/rs/RsHeaders.java b/src/main/java/org/takes/rs/RsHeaders.java index cd755701d..ffe2fb2cd 100644 --- a/src/main/java/org/takes/rs/RsHeaders.java +++ b/src/main/java/org/takes/rs/RsHeaders.java @@ -103,7 +103,7 @@ public List header(final CharSequence key) list = new VerboseList<>( Collections.emptyList(), new FormattedText( - "there are no headers by name \"%s\" among %d others: %s", + "There are no headers by name \"%s\" among %d others: %s", key, this.map().size(), this.map().keySet() @@ -113,7 +113,7 @@ public List header(final CharSequence key) list = new VerboseList<>( values, new FormattedText( - "there are only %d headers by name \"%s\"", + "There are only %d headers by name \"%s\"", values.size(), key ) @@ -139,10 +139,11 @@ private Map> map() throws IOException { if (!head.hasNext()) { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, - "a valid request must contain at least one line in the head" + "A valid request must contain at least one line in the head" ); } head.next(); + int pos = 1; final Map> map = new HashMap<>(0); while (head.hasNext()) { final String line = head.next(); @@ -150,7 +151,10 @@ private Map> map() throws IOException { if (parts.length < 2) { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, - String.format("invalid HTTP header: \"%s\"", line) + String.format( + "Invalid HTTP header on line #%d: \"%s\"", + pos, line + ) ); } final String key = new UncheckedText( @@ -164,6 +168,7 @@ private Map> map() throws IOException { new Trimmed(new TextOf(parts[1])) ).asString() ); + pos += 1; } return map; } @@ -232,7 +237,7 @@ public String single(final CharSequence name) throws IOException { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, String.format( - "header \"%s\" is mandatory, not found among %s", + "Header \"%s\" is mandatory, not found among %s", name, this.names() ) ); diff --git a/src/main/java/org/takes/rs/RsPrint.java b/src/main/java/org/takes/rs/RsPrint.java index 61a964c2c..50be8bb7d 100644 --- a/src/main/java/org/takes/rs/RsPrint.java +++ b/src/main/java/org/takes/rs/RsPrint.java @@ -133,7 +133,7 @@ public void printHead(final OutputStream output) throws IOException { if (pos == 0 && !RsPrint.FIRST.matcher(line).matches()) { throw new IllegalArgumentException( String.format( - "first line of HTTP response \"%s\" doesn't match \"%s\" regular expression, but it should, according to RFC 7230", + "First line of HTTP response \"%s\" doesn't match \"%s\" regular expression, but it should, according to RFC 7230", line, RsPrint.FIRST ) ); @@ -141,7 +141,7 @@ public void printHead(final OutputStream output) throws IOException { if (pos > 0 && !RsPrint.OTHERS.matcher(line).matches()) { throw new IllegalArgumentException( String.format( - "header line #%d of HTTP response \"%s\" doesn't match \"%s\" regular expression, but it should, according to RFC 7230", + "Header line #%d of HTTP response \"%s\" doesn't match \"%s\" regular expression, but it should, according to RFC 7230", pos + 1, line, RsPrint.OTHERS ) ); diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index a92b32914..56724dad7 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -103,7 +103,7 @@ private static Iterable head(final Response origin, if (status < 100 || status > 999) { throw new IllegalArgumentException( String.format( - "according to RFC 7230 HTTP status code must have three digits: %d", + "According to RFC 7230 HTTP status code must have three digits: %d", status ) ); diff --git a/src/main/java/org/takes/rs/RsXslt.java b/src/main/java/org/takes/rs/RsXslt.java index 417bdc61e..22dd4707f 100644 --- a/src/main/java/org/takes/rs/RsXslt.java +++ b/src/main/java/org/takes/rs/RsXslt.java @@ -137,7 +137,10 @@ private static InputStream transform(final TransformerFactory factory, try { input = RsXslt.consume(xml); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to consume XML by XSLT", + ex + ); } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Source xsl = RsXslt.stylesheet( @@ -193,7 +196,7 @@ private static Source stylesheet(final TransformerFactory factory, ); if (stylesheet == null) { throw new IllegalArgumentException( - "no associated stylesheet found in XML" + "No associated stylesheet found in XML" ); } return stylesheet; @@ -240,7 +243,10 @@ public Source resolve(final String href, final String base) try { input = uri.toURL().openStream(); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to open URL '%s'", uri), + ex + ); } } else { input = this.getClass().getResourceAsStream(uri.getPath()); diff --git a/src/main/java/org/takes/rs/xe/RsXembly.java b/src/main/java/org/takes/rs/xe/RsXembly.java index 7490b9a8c..4e95ef25a 100644 --- a/src/main/java/org/takes/rs/xe/RsXembly.java +++ b/src/main/java/org/takes/rs/xe/RsXembly.java @@ -142,7 +142,10 @@ private static InputStream render(final Node dom, ) ); } catch (final TransformerException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to transform XML via XSLT", + ex + ); } return new ByteArrayInputStream(baos.toByteArray()); } diff --git a/src/main/java/org/takes/rs/xe/XeDirectives.java b/src/main/java/org/takes/rs/xe/XeDirectives.java index 3ab049c7b..dfff1cd71 100644 --- a/src/main/java/org/takes/rs/xe/XeDirectives.java +++ b/src/main/java/org/takes/rs/xe/XeDirectives.java @@ -102,7 +102,10 @@ private static Iterable transform(final Iterable texts) { list.add(dir); } } catch (final SyntaxException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to parse Xembly directives", + ex + ); } } return list; diff --git a/src/main/java/org/takes/servlet/HttpServletRequestFake.java b/src/main/java/org/takes/servlet/HttpServletRequestFake.java index 3272f6011..b6b91b60a 100644 --- a/src/main/java/org/takes/servlet/HttpServletRequestFake.java +++ b/src/main/java/org/takes/servlet/HttpServletRequestFake.java @@ -98,7 +98,10 @@ public Enumeration getHeaders(final String key) { new RqHeaders.Base(this.request).header(key) ); } catch (final IOException ex) { - throw new IllegalArgumentException(ex); + throw new IllegalArgumentException( + String.format("Failed to read header '%s'", key), + ex + ); } } @@ -109,7 +112,10 @@ public Enumeration getHeaderNames() { new RqHeaders.Base(this.request).names() ); } catch (final IOException ex) { - throw new IllegalArgumentException(ex); + throw new IllegalArgumentException( + "Failed to parse headers in the request", + ex + ); } } @@ -118,7 +124,10 @@ public String getMethod() { try { return new RqMethod.Base(this.request).method(); } catch (final IOException ex) { - throw new IllegalArgumentException(ex); + throw new IllegalArgumentException( + "Failed to get method from the request", + ex + ); } } @@ -159,43 +168,64 @@ public String getRequestURI() { try { return new RqHref.Base(this.request).href().path(); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to get HREF from Request", + ex + ); } } @Override public String getQueryString() { + final String raw = this.getRequestURI(); + final URI uri; try { - return new URI(this.getRequestURI()).getQuery(); + uri = new URI(raw); } catch (final URISyntaxException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to parse URI '%s'", raw), + ex + ); } + return uri.getQuery(); } @Override public String getServerName() { + final String raw = this.getRequestURI(); + final URI uri; try { - String host = new URI(this.getRequestURI()).getHost(); - if (host == null || host.isEmpty()) { - host = "localhost"; - } - return host; + uri = new URI(raw); } catch (final URISyntaxException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to parse URI '%s'", raw), + ex + ); + } + String host = uri.getHost(); + if (host == null || host.isEmpty()) { + host = "localhost"; } + return host; } @Override public int getServerPort() { + final String raw = this.getRequestURI(); + final URI uri; try { - int port = new URI(this.getRequestURI()).getPort(); - if (port == -1) { - port = 80; - } - return port; + uri = new URI(raw); } catch (final URISyntaxException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format("Failed to parse URI '%s'", raw), + ex + ); + } + int port = uri.getPort(); + if (port == -1) { + port = 80; } + return port; } @Override diff --git a/src/main/java/org/takes/servlet/HttpServletResponseFake.java b/src/main/java/org/takes/servlet/HttpServletResponseFake.java index c72cfee21..3709891ca 100644 --- a/src/main/java/org/takes/servlet/HttpServletResponseFake.java +++ b/src/main/java/org/takes/servlet/HttpServletResponseFake.java @@ -119,22 +119,24 @@ public void sendError( @Override public Collection getHeaders(final String header) { - final String prefix = String.format( - "%s", - new Lowered(header) - ); + final Iterable head; try { - return new ListOf<>( - new Filtered<>( - hdr -> new StartsWith( - new Lowered(hdr), new TextOf(prefix) - ).value(), - this.response.get().head() - ) - ); + head = this.response.get().head(); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to read the head from the request", + ex + ); } + return new ListOf<>( + new Filtered<>( + hdr -> new StartsWith( + new Lowered(hdr), + new TextOf(String.format("%s", new Lowered(header))) + ).value(), + head + ) + ); } @Override diff --git a/src/main/java/org/takes/servlet/ServletInputStreamOf.java b/src/main/java/org/takes/servlet/ServletInputStreamOf.java index 5ccbc6362..f0eafb634 100644 --- a/src/main/java/org/takes/servlet/ServletInputStreamOf.java +++ b/src/main/java/org/takes/servlet/ServletInputStreamOf.java @@ -55,7 +55,10 @@ public boolean isFinished() { try { finished = this.available() == 0; } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + "Failed to check the available() status of the stream", + ex + ); } return finished; } diff --git a/src/main/java/org/takes/servlet/SrvTake.java b/src/main/java/org/takes/servlet/SrvTake.java index 62d01434d..8a2102878 100644 --- a/src/main/java/org/takes/servlet/SrvTake.java +++ b/src/main/java/org/takes/servlet/SrvTake.java @@ -119,7 +119,9 @@ public void init() throws ServletException { ); } if (!this.tke.compareAndSet(null, take)) { - throw new IllegalStateException("Take already constructed."); + throw new IllegalStateException( + "Take is already constructed" + ); } } diff --git a/src/main/java/org/takes/tk/TkRetry.java b/src/main/java/org/takes/tk/TkRetry.java index 92e681ffe..65321fc0c 100644 --- a/src/main/java/org/takes/tk/TkRetry.java +++ b/src/main/java/org/takes/tk/TkRetry.java @@ -74,7 +74,7 @@ public TkRetry(final int retries, final int wait, final Take original) { public Response act(final Request req) throws IOException { if (this.count <= 0) { throw new IllegalArgumentException( - "can't make less than one attempt" + "Can't make less than one attempt" ); } int attempts = 0; diff --git a/src/test/java/org/takes/rq/RqSocketTest.java b/src/test/java/org/takes/rq/RqSocketTest.java index f552d20cf..b5ac8cffc 100644 --- a/src/test/java/org/takes/rq/RqSocketTest.java +++ b/src/test/java/org/takes/rq/RqSocketTest.java @@ -119,7 +119,7 @@ void returnNotFoundRemoteAddress() { MatcherAssert.assertThat( ex.getMessage(), Matchers.containsString( - "header \"X-Takes-RemoteAddress\" is mandatory" + "Header \"X-Takes-RemoteAddress\" is mandatory" ) ); throw ex; @@ -146,7 +146,7 @@ void returnNotFoundLocalAddress() { MatcherAssert.assertThat( ex.getMessage(), Matchers.containsString( - "header \"X-Takes-LocalAddress\" is mandatory" + "Header \"X-Takes-LocalAddress\" is mandatory" ) ); throw ex; @@ -171,7 +171,7 @@ void returnNotFoundRemotePort() { MatcherAssert.assertThat( ex.getMessage(), Matchers.containsString( - "header \"X-Takes-RemotePort\" is mandatory" + "Header \"X-Takes-RemotePort\" is mandatory" ) ); throw ex; @@ -196,7 +196,7 @@ void returnNotFoundLocalPort() { MatcherAssert.assertThat( ex.getMessage(), Matchers.containsString( - "header \"X-Takes-LocalPort\" is mandatory" + "Header \"X-Takes-LocalPort\" is mandatory" ) ); throw ex;