Immutables, IllegalStateException, and 500

32 views
Skip to first unread message

Robert Di Falco

unread,
May 18, 2020, 11:35:46 AM5/18/20
to dropwizard-user
My server responds with status 500 instead of 400 when invalid JSON is sent to me dropwizard service. We tracked it down to Immutables throwing an IllegalStateException which Jackson then wraps as an InvalidDefinitionException which of course ends up triggering this line in the Dropwizard JsonProcessingExceptionMapper.

if (exception instanceof JsonGenerationException || exception instanceof InvalidDefinitionException) {
return super.toResponse(exception); // LoggingExceptionMapper will log exception
}

Is there a simple way around this? A configuration or some such so that I properly return the status of 400? The only thing we can come up with is writing our own Exception Mapper and not using the drop wizard supplied one. But it seems heavy handed. 
public class ImmutablesJsonProcessingExceptionMapper extends JsonProcessingExceptionMapper {
@Override
public Response toResponse(JsonProcessingException exception) {
if (exception instanceof InvalidDefinitionException) {
exception = new JsonParseException((JsonParser) null, null, exception);
}

return super.toResponse(exception);
}
}


 Thanks for any input. 

R.

עוזיאל סולקיס

unread,
May 18, 2020, 11:44:35 AM5/18/20
to dropwiz...@googlegroups.com
Did you try using the validation annotations?
I think that adding Validators will make illegal input to return bad request.

‫בתאריך יום ב׳, 18 במאי 2020 ב-18:35 מאת ‪Robert Di Falco‬‏ <‪rob...@bright.md‬‏>:‬
--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/2b89feea-abb3-44e8-a5ff-8b620a78cfdf%40googlegroups.com.


--
Look for me on Facebook, Twitter or LinkedIn.

Robert Di Falco

unread,
May 18, 2020, 2:51:59 PM5/18/20
to dropwizard-user
Can you be more specific? 

On Monday, May 18, 2020 at 8:44:35 AM UTC-7, עוזיאל סולקיס wrote:
Did you try using the validation annotations?
I think that adding Validators will make illegal input to return bad request.

‫בתאריך יום ב׳, 18 במאי 2020 ב-18:35 מאת ‪Robert Di Falco‬‏ <‪rob...@bright.md‬‏>:‬
My server responds with status 500 instead of 400 when invalid JSON is sent to me dropwizard service. We tracked it down to Immutables throwing an IllegalStateException which Jackson then wraps as an InvalidDefinitionException which of course ends up triggering this line in the Dropwizard JsonProcessingExceptionMapper.

if (exception instanceof JsonGenerationException || exception instanceof InvalidDefinitionException) {
return super.toResponse(exception); // LoggingExceptionMapper will log exception
}

Is there a simple way around this? A configuration or some such so that I properly return the status of 400? The only thing we can come up with is writing our own Exception Mapper and not using the drop wizard supplied one. But it seems heavy handed. 
public class ImmutablesJsonProcessingExceptionMapper extends JsonProcessingExceptionMapper {
@Override
public Response toResponse(JsonProcessingException exception) {
if (exception instanceof InvalidDefinitionException) {
exception = new JsonParseException((JsonParser) null, null, exception);
}

return super.toResponse(exception);
}
}


 Thanks for any input. 

R.

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwiz...@googlegroups.com.

עוזיאל סולקיס

unread,
May 19, 2020, 3:02:54 AM5/19/20
to dropwiz...@googlegroups.com
Now I understand that you meant that the JSON is not unexpected (missing properties and things like it) but actually an invalid JSON (not in a valid JSON form).
In this case validators won't work, I fear.

If it's about values in the request that you want to validate their values -
you can validate using validators from the javax.validation
image.png

Or using a custom validator

‫בתאריך יום ב׳, 18 במאי 2020 ב-21:52 מאת ‪Robert Di Falco‬‏ <‪rob...@bright.md‬‏>:‬
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/5ba1b61e-0212-49f1-882c-b46c1284fb8c%40googlegroups.com.

Jochen Schalanda

unread,
May 19, 2020, 5:47:41 AM5/19/20
to dropwiz...@googlegroups.com
Hi Robert,

could you please provide a small reproducible example for your issue?
Which version of Dropwizard are you using?

Cheers,
Jochen
Reply all
Reply to author
Forward
0 new messages