Skip to content

Commit

Permalink
#1142 better exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 22, 2022
1 parent 32f7cba commit 7a4160a
Show file tree
Hide file tree
Showing 25 changed files with 180 additions and 77 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/takes/facets/auth/PsBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ public Opt<Identity> 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<>();
Expand Down Expand Up @@ -302,7 +305,10 @@ private Opt<String> 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<String> opt;
if (urn == null) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/takes/facets/auth/codecs/CcCompact.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/takes/facets/auth/social/PsFacebook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/takes/facets/fork/FkHitRefresh.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ public FkHitRefresh(final File file, final List<String> 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
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/takes/http/BkParallel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
)
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/takes/http/BkTimeable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/takes/http/FtCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
);
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/org/takes/http/FtRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,26 @@ 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
);
}
}
);
thread.start();
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) {
Expand All @@ -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
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/http/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static Map<String, String> asMap(final Iterable<String> 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);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/takes/misc/Href.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

Expand All @@ -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
);
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/takes/rq/RqHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public List<String> 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()
Expand All @@ -113,7 +113,7 @@ public List<String> 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
)
Expand All @@ -139,18 +139,22 @@ private Map<String, List<String>> 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<String, List<String>> 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(
Expand All @@ -164,6 +168,7 @@ private Map<String, List<String>> map() throws IOException {
new Trimmed(new TextOf(parts[1]))
).asString()
);
pos += 1;
}
return map;
}
Expand Down Expand Up @@ -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()
)
);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/takes/rq/form/RqFormBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ public Iterable<String> 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
);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/takes/rq/form/RqFormFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}
15 changes: 10 additions & 5 deletions src/main/java/org/takes/rs/RsHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public List<String> 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()
Expand All @@ -113,7 +113,7 @@ public List<String> 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
)
Expand All @@ -139,18 +139,22 @@ private Map<String, List<String>> 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<String, List<String>> map = new HashMap<>(0);
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(
Expand All @@ -164,6 +168,7 @@ private Map<String, List<String>> map() throws IOException {
new Trimmed(new TextOf(parts[1]))
).asString()
);
pos += 1;
}
return map;
}
Expand Down Expand Up @@ -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()
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/rs/RsPrint.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ 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
)
);
}
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
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rs/RsWithStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static Iterable<String> 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
)
);
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/takes/rs/RsXslt.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/takes/rs/xe/RsXembly.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Loading

0 comments on commit 7a4160a

Please sign in to comment.