Skip to content
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

Unable to catch MissingFieldException #1266

Closed
carbotaniuman opened this issue Dec 25, 2020 · 12 comments
Closed

Unable to catch MissingFieldException #1266

carbotaniuman opened this issue Dec 25, 2020 · 12 comments
Assignees
Labels

Comments

@carbotaniuman
Copy link

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

I'd like to programmatically catch MissingFieldExceptions without having to resort to reading the stacktrace in order to check.

Describe the solution you'd like

Make MissingFieldException public.

@qwwdfsad
Copy link
Member

Could you please elaborate on what exactly are you trying to achieve? E.g. provide a snippet of code and/or a use-case.
Then we could understand why it is important to differentiate MFE from other serialization exceptions

@carbotaniuman
Copy link
Author

For me it's just to fulfill an API contract and return a better error, so if you don't want to commit to this in the public API my hack works fine for me.

@qwwdfsad
Copy link
Member

qwwdfsad commented Dec 31, 2020

if you don't want to commit to this in the public API

Well, it's not really about us wanting it or not, but about the rationale behind the decision. In general, we're trying to keep public API as small as possible, but still concise and pragmatic. So before making it public, we should know why it can be useful.

For me it's just to fulfill an API contract and return a better error

Could you please elaborate on why you can't catch plain SerializationException there? Or how MissingFieldException handling is different from SerializationException?

@bukajsytlos
Copy link

I second this and also not having the field name as an attribute of exception.
I my case, when I am parsing json request and there is a missing field and I want to provide custom error response message I would have to parse message to get info I want, instead of catching specific exception which would have this field attribute and I can do whatever I want with it.

@bukajsytlos
Copy link

@qwwdfsad ping

@qwwdfsad
Copy link
Member

I second this and also not having the field name as an attribute of exception.

For example in 1.1.0 it will be not the name, but names: https://github.com/Kotlin/kotlinx.serialization/blob/dev/core/commonMain/src/kotlinx/serialization/SerializationException.kt#L70 And it's nice for us to have an ability to freely change this API without preserving backwards compatibility.

Could you please elaborate on what's wrong with the default error message and how can we improve it?
Or, additionally, why you have to differentiate e.g. invalid data (e.g. expected string, but have int) from missing field exception?

@bukajsytlos
Copy link

for example internationalisation

@bukajsytlos
Copy link

well as in my example, when using serialization for http requests, I want to tell client why is his input bad, what's wrong with it, and which field is responsible

@ghost
Copy link

ghost commented Feb 10, 2021

Because we can only catch SerializationException, it is difficult to programmatically know if it's an issue due to improper JSON formatting, missing fields, or extraneous fields. We could parse the string and use that to determine which issue we're facing, but that is obviously a lot less safe than relying on a type.

Uses for being able to differentiate based on type instead of just letting the error message through include: different logging requirements depending on the organization (e.g. the error message may contain sensitive information), custom error codes for clients, custom error messages for clients, error reporting in an event-based architecture, routing to an appropriate error-correction handler, etc. There are many possibilities.

@mskonovalov
Copy link

Sorry, not sure if it is related to the topic or should I create a separate issue. But why most of the exceptions are internal?
I want to catch exception in my code kotlinx.serialization.json.internal.JsonDecodingException
but obviously I cannot do it as it is internal class and I cannot refer to it in my code.
Am I missing something?

@Intex32
Copy link

Intex32 commented Jun 24, 2022

@mskonovalov well, that's what this ticket is all about. But my hope keeps within limits. JetBrains has too many projects they're working on simultaneously.

@qwwdfsad
Copy link
Member

I'm working on that along with #1875

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants