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

chore: improve error message for JSON deserialization #1911

Merged
merged 1 commit into from
Dec 13, 2023
Merged
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
40 changes: 26 additions & 14 deletions sdk/java-sdk-protobuf/src/main/java/kalix/javasdk/JsonSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,35 @@ public static <T> T decodeJson(Class<T> valueClass, Any any) {
} else {
return objectMapper.readValue(decodedBytes.toByteArray(), valueClass);
}
} catch (JsonProcessingException e) {
throw jsonProcessingException(valueClass, any, e);
} catch (IOException | NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new IllegalArgumentException(
"JSON with type url ["
+ any.getTypeUrl()
+ "] could not be decoded into a ["
+ valueClass.getName()
+ "]",
e);
throw genericDecodeException(valueClass, any, e);
}
}
}

private static <T> IllegalArgumentException jsonProcessingException(Class<T> valueClass, Any any, JsonProcessingException e) {
return new IllegalArgumentException(
"JSON with type url ["
+ any.getTypeUrl()
+ "] could not be decoded into a ["
+ valueClass.getName()
+ "]. Make sure that changes are backwards compatible or apply a @Migration mechanism (https://docs.kalix.io/java/serialization.html#_schema_evolution).",
Copy link
Member

Choose a reason for hiding this comment

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

Good one!

e);
}

private static <T> IllegalArgumentException genericDecodeException(Class<T> valueClass, Any any, Exception e) {
return new IllegalArgumentException(
"JSON with type url ["
+ any.getTypeUrl()
+ "] could not be decoded into a ["
+ valueClass.getName()
+ "]",
e);
}

private static <T> T migrate(Class<T> valueClass, ByteString decodedBytes, int fromVersion, JsonMigration jsonMigration) throws IOException {
JsonNode jsonNode = objectMapper.readTree(decodedBytes.toByteArray());
JsonNode newJsonNode = jsonMigration.transform(fromVersion, jsonNode);
Expand Down Expand Up @@ -214,14 +230,10 @@ public static <T, C extends Collection<T>> C decodeJsonCollection(Class<T> value
ByteString decodedBytes = ByteStringEncoding.decodePrimitiveBytes(any.getValue());
var typeRef = objectMapper.getTypeFactory().constructCollectionType(collectionType, valueClass);
return objectMapper.readValue(decodedBytes.toByteArray(), typeRef);
} catch (JsonProcessingException e) {
throw jsonProcessingException(valueClass, any, e);
} catch (IOException e) {
throw new IllegalArgumentException(
"JSON with type url ["
+ any.getTypeUrl()
+ "] could not be decoded into a ["
+ valueClass.getName()
+ "]",
e);
throw genericDecodeException(valueClass, any, e);
Copy link
Member

Choose a reason for hiding this comment

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

In which case would we get a IOException that is not a JsonProcessingException?
Corrupted payload?

Copy link
Member

Choose a reason for hiding this comment

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

Ok, maybe when reading from an external topic that is emitting bad data. In such a case, there is nothing the user can do. Migration won't work anyway.

}
}
}
Expand Down