question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Context.json() does not throw checked exceptions that it actually throws

See original GitHub issue

Actual 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tipsycommented, Jul 27, 2020

@avshenuk that’s correct, you’ll have to validate it manually if you want to catch your own exception.

1reaction
azurviicommented, Mar 17, 2019

Thanks @tipsy. This is an interesting way of handling exceptions.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found