Skip to content

Catching exceptions from the constructor when deserialization for clearer error info #3111

@Stream29

Description

@Stream29

What is your use-case and why do you need this feature?

I'm trying to migrate my Python AI agent project into Kotlin.

With Python

When validating the json output from the LLM, I use Pydantic to set the requirements on the fields of a data class.
It gives me clear error message with a unified ValidationError, allowing me to extract error information and fix it with LLM.

With Kotlin

kotlinx.serialization gives clear error message with path when there is missing fields. When it comes with the validation of the value, I use a init block in the data class to validate the input value.
When the validation fails, the exception is thrown with no path info, and it's not wrapped with SerializationException.

Describe the solution you'd like

When the constructor throws an IllegalArgumentException while deserialization, the exception should be wrapped and add path info on it.
When the exception happend, the raw input should be kept and accessible fully. (Currently the input will be shorten if larger than 200 chars)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions