Skip to content

Fix #4603 (Keep stacktrace when re-throwing exception with JsonMappingException) #5041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,12 @@ public enum MapperFeature implements ConfigFeature
*
* @since 2.19
*/
REQUIRE_HANDLERS_FOR_JAVA8_OPTIONALS(true)
REQUIRE_HANDLERS_FOR_JAVA8_OPTIONALS(true),

/**
* @since 2.19
*/
UNWRAP_ROOT_CAUSE(true)
;

private final boolean _defaultState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,32 @@ public SettableBeanProperty unwrapped(NameTransformer xf)
/**********************************************************
*/

/**
* Method that takes in exception of any type, and casts or wraps it
* to an IOException or its subclass.
*/
protected void _throwAsIOE(DeserializationConfig config, JsonParser p, Exception e, Object value) throws IOException
{
if (e instanceof IllegalArgumentException) {
String actType = ClassUtil.classNameOf(value);
StringBuilder msg = new StringBuilder("Problem deserializing property '")
.append(getName())
.append("' (expected type: ")
.append(getType())
.append("; actual type: ")
.append(actType).append(")");
String origMsg = ClassUtil.exceptionMessage(e);
if (origMsg != null) {
msg.append(", problem: ")
.append(origMsg);
} else {
msg.append(" (no error message provided)");
}
throw JsonMappingException.from(p, msg.toString(), e);
}
_throwAsIOE(p, e, config.isEnabled(MapperFeature.UNWRAP_ROOT_CAUSE));
}

/**
* Method that takes in exception of any type, and casts or wraps it
* to an IOException or its subclass.
Expand All @@ -637,6 +663,23 @@ protected void _throwAsIOE(JsonParser p, Exception e, Object value) throws IOExc
_throwAsIOE(p, e);
}

/**
* @since 2.7
*/
protected IOException _throwAsIOE(JsonParser p, Exception e, boolean unwrapRootCause) throws IOException
{
ClassUtil.throwIfIOE(e);
ClassUtil.throwIfRTE(e);

if (unwrapRootCause) {
// let's wrap the innermost problem
Throwable th = ClassUtil.getRootCause(e);
throw JsonMappingException.from(p, ClassUtil.exceptionMessage(th), th);
} else {
throw JsonMappingException.from(p, ClassUtil.exceptionMessage(e.getCause()), e.getCause());
Copy link
Member Author

@JooHyukKim JooHyukKim Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thougths...

  1. Calling getCause() because exception is InvocationTarget exception thrown from a setter method.
    There maybe other cases? or nah

  2. How to centralize this handling? Or at least make it clean

}
}

/**
* @since 2.7
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void deserializeAndSet(JsonParser p, DeserializationContext ctxt,
try {
_setter.invoke(instance, value);
} catch (Exception e) {
_throwAsIOE(p, e, value);
_throwAsIOE(ctxt.getConfig(), p, e, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.fasterxml.jackson.databind.introspect;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;

// [databind#4603] Keep stacktrace when re-throwing exception with JsonMappingException
public class RetainStacktrace4603Test
extends DatabindTestUtil
{

static class CustomException extends RuntimeException {
public CustomException(String message, Throwable cause) {
super(message, cause);
}
}

static class Feature1347DeserBean {

int value;

public void setValue(int x) {
try {
// Throws ArithmeticException
int a = x / 0;
} catch (Exception e) {
// should NOT get called, but just to
throw new CustomException("Should NOT get called", e);
}
}
}

final String JSON = a2q("{'value':3}");

final ObjectMapper enabledMapper = jsonMapperBuilder()
.configure(MapperFeature.UNWRAP_ROOT_CAUSE, true)
.build();

final ObjectMapper disabledMapper = jsonMapperBuilder()
.configure(MapperFeature.UNWRAP_ROOT_CAUSE, false)
.build();

final ObjectMapper defaultMapper = newJsonMapper();


// Whether disabled or enabled, should get ArithmeticException
@Test
public void testVisibility()
throws Exception
{
DatabindException enabledResult = _tryDeserializeWith(enabledMapper);
assertInstanceOf(ArithmeticException.class, enabledResult.getCause());

DatabindException defaultResult = _tryDeserializeWith(defaultMapper);
assertInstanceOf(ArithmeticException.class, defaultResult.getCause());

DatabindException disabledResult = _tryDeserializeWith(disabledMapper);
assertInstanceOf(CustomException.class, disabledResult.getCause());
assertInstanceOf(ArithmeticException.class, disabledResult.getCause().getCause());

}

private DatabindException _tryDeserializeWith(ObjectMapper mapper) {
return assertThrows(DatabindException.class,
() -> mapper.readValue(JSON, Feature1347DeserBean.class)
);
}

}