Skip to content

Keep full stacktrace when re-throwing exception with DatabindException during deserialization #4603

@pn-santos

Description

@pn-santos

Is your feature request related to a problem? Please describe.

I have code that re-throws an exception wrapped in a custom exception during de-serialization via builder setters, e.g.

@JsonProperty
Builder property(String value) {
    try {
        (...)
    } catch (Exception ex) {
        throw MyCustomException(..., ex);
    }

When that happens Jackson will unconditionally wrap the root cause with a JsonMappingException in:

protected IOException _throwAsIOE(JsonParser p, Exception e) throws IOException
{
    ClassUtil.throwIfIOE(e);
    ClassUtil.throwIfRTE(e);
    // let's wrap the innermost problem
    Throwable th = ClassUtil.getRootCause(e);
    throw JsonMappingException.from(p, ClassUtil.exceptionMessage(th), th);
}

The custom exception is thus "lost" from the upstream perspective (i.e. code that triggered de-serialization).

This makes it impossible to propagate the custom exception with a cause upstream (it can be propagated but it cannot have a cause set).

Describe the solution you'd like

Be able to control whether Jackson will wrap the root cause or the outer most exception via object mapper config.

Usage example

No response

Additional context

In my particular use case, the idea is that if an exception is thrown by the application, if the custom exception is found in the causal chain, the handling of the exception is different than if it's not. But it's still useful to have the complete stacktrace for logging/debugging purposes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.0Issue planned for initial 3.0 release

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions