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

JsonAdapter.fromJson functions are documented as throwing IOException but actually throw JsonDataException #1885

Open
Orbar opened this issue Aug 26, 2024 · 1 comment
Labels

Comments

@Orbar
Copy link

Orbar commented Aug 26, 2024

JsonAdapter.fromJson function are annotated with @Throws(IOException::class) indicating that is the exception that they will throw when there is an error when deserializing a json string, however what is actually thrown is a JsonDataException which is a RuntimeException meaning that if you try to catch the document IOException, your error handling code will be skipped.

For example, consider the following code

@JsonClass(generateAdapter = true)
data class MyType(@Json(name = "one") override val param: String)

...

try {
  myTypeAdapter.fromJson("{}") // just an empty json
catch (e: IOException) {
  // exception will not be caught
}

JsonDataException should extend IOException, or JsonAdapter.fromJson should be updated to indicate that they throw JsonDataException

@Orbar Orbar added the bug label Aug 26, 2024
@JakeWharton
Copy link
Collaborator

The exception list is not exhaustive. IOException is declared because it is a checked exception for Java callers.

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

4 participants
@JakeWharton @Orbar and others