Spark: Spark swallows exceptions

Created on 17 Jun 2016  路  5Comments  路  Source: perwendel/spark

Whenever I write code that causes e.g. a NullPointerException, like calling methods on an Object that is null, eclipse does not print any exceptions at all. See following code:

credentials = new HashMap<>();
if(credentials.get("user").equals("password")) { ... }

Now I didn't realize get() would return null if the key wasn't in the HashMap. However running this code gave me a 500 serverside error, while eclipse did not print a single exception. I had to find and fix the error myself like so:

String storedPassword = credentials.get("user");
if(storedPassword != null && password.equals(storedPassword)) { ... }

Please note: I have slf4j in my pom.xml and buildpath:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.21</version>
</dependency>

Most helpful comment

You can register a catch-all Spark exception handler to log uncaught exceptions:

Spark.exception(Exception.class, (exception, request, response) -> {
    exception.printStackTrace();
});

IMHO, this should be the default behavior tho.

All 5 comments

You can register a catch-all Spark exception handler to log uncaught exceptions:

Spark.exception(Exception.class, (exception, request, response) -> {
    exception.printStackTrace();
});

IMHO, this should be the default behavior tho.

@lewtds thank you, that would print the Exception expected in the faulty code above. However if there are no Exceptions printed at all it's a maddening process to go and find the wrong pieces yourself especially if your codebase is large I believe. Thus I also think this should be the default.

I did that, but when doing so the return status code in the response is 404 instead of 500 (expected). Does this happen to you as well?

This is the same as #486

lewtds's solution works, but I still don't get the expected behavior on 2.5.2.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

airinfection picture airinfection  路  3Comments

sagesolar picture sagesolar  路  4Comments

RichoDemus picture RichoDemus  路  7Comments

kiriti999 picture kiriti999  路  4Comments

Blok5 picture Blok5  路  5Comments