Context.json() does not throw checked exceptions that it actually throws
See original GitHub issueActual behavior (the bug)
context.json()
throws JsonParseException
but I’m unable to catch it in a proper way.
Expected behavior
context.json()
declares exceptions thrown by it.
To Reproduce
An example to generate a JsonParseException
error:
import io.javalin.Javalin;
public final class Main {
public static void main(String[] args) {
Javalin app = Javalin.create().start(12345);
app.post("/json", ctx -> ctx.result(ctx.bodyAsClass(TestClass2.class).getName()));
}
}
final class TestClass2 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Then a post of malformed JSON would trigger an exception, e.g. with “=” instead of “:”:
wget --post-data='{"name"="name"}' localhost:12345/json -O /dev/null -q
You get:
[qtpxxxxxxxxxx-xx] WARN io.javalin.core.ExceptionMapper - Uncaught exception
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('=' (code 61)): was expecting a colon to separate field name and value
at [Source: (String)"{"name"="name"}"; line: 1, column: 9]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
...
If I try to wrap it with try-catch:
try {
ctx.result(ctx.bodyAsClass(TestClass2.class).getName());
} catch (JsonParseException e) {
e.printStackTrace();
}
The compiler would complain:
Exception 'com.fasterxml.jackson.core.JsonParseException' is never thrown in the corresponding try block
This is an error that I can’t ignore it like a warning.
Additional context I’m doing something like this at the time being:
try {
ctx.result(ctx.bodyAsClass(TestClass2.class).getName());
} catch (Exception e) {
if (e instanceof JsonParseException) {
e.printStackTrace();
}
}
This at least allows me to compile, though there is an IDE warning highlighting:
Condition 'e instanceof JsonParseException' is always 'false' less
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Context.json() does not throw checked exceptions ... - GitHub
Actual behavior (the bug) context.json() throws JsonParseException but I'm unable to catch it in a proper way. Expected behavior
Read more >How to manage exceptions thrown in filters in Spring?
I would like to return the beautiful JSON response just like the rest of the exceptions thrown not from a Filter. { "status":...
Read more >ERR06-J. Do not throw undeclared checked exceptions
This noncompliant code example throws undeclared checked exceptions. The undeclaredThrow() method takes a Throwable argument and invokes a function that will ...
Read more >Replacing Throwing Exceptions with Notification in Validations
I was recently looking at some code to do some basic validation of some incoming JSON messages. It looked something like this. public...
Read more >Checked vs. Unchecked Exceptions: The Debate Is Not Over
When a method says it throws an exception, I understand that the method is not safe. It may fail sometimes, and it's my...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@avshenuk that’s correct, you’ll have to validate it manually if you want to catch your own exception.
Thanks @tipsy. This is an interesting way of handling exceptions.