-
Notifications
You must be signed in to change notification settings - Fork 39
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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).", | ||
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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In which case would we get a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good one!