Allow customization of the error message for missing element when decoding
See original GitHub issueCurrently decoding a JSON that misses an element will result in a DecodingFailure
with a message saying: "Attempt to decode value on failed cursor"
.
This error cannot be customized other than by doing string matching on DecodingFailure.message
.
I would suggest to add a default parameter on decode
and decodeAccumulating
:
final def decode[A: Decoder](input: String, missingElementErrorMessage: String =
defaultMissingElementErrorMessage)
An alternative would be to encode that case in the type:
sealed trait DecodingFailure { def history: List[CursorOp] }
case class MissingElement(val history: List[CursorOp]) extends DecodingFailure
case class IncorrectValue( /* ... */ ) extends DecodingFailure
Issue Analytics
- State:
- Created 7 years ago
- Reactions:16
- Comments:11 (9 by maintainers)
Top Results From Across the Web
"The data couldn't be read because it is missing" error when ...
The data couldn't be read because it is missing. When I run the following code: struct Indicator: Decodable { let section: String ...
Read more >Get Started with Custom Error Handling in Spring Boot (Java)
Learn how to implement custom error handling logic in Spring Boot. You will see two approaches based on the @ControllerAdvice annotation.
Read more >Ignoring invalid JSON elements when using Codable
How elements containing missing or invalid data can be automatically ignored when using Swift's Codable API to parse JSON.
Read more >Decode an array with a corrupted element - Sarunw
Each User required first name and last name; the middle name is optional. struct User: Codable { let firstName: String
Read more >Codable: Tips and Tricks | kean.blog
struct Post: Decodable { let id: Id<Post> // More about this type ... Swift provides a complete error report using DecodingError which is ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I opended a PR #1081 to implement this feature, but no response for 3 months. If it is unlikely to be implemented in circe, please close this issue as wontfix.
Maybe this might help; I did change the message by looking at the cursor history and trying to examine whether the failure was due to a missing field (which could be simplified a bit by using CursorOp.opsToPath): https://github.com/knutwalker/akka-stream-json/blob/b8bc752ef3ab2f76793c0db40bddc8a2bd5787df/support/stream-circe/src/main/scala/de/knutwalker/akka/stream/support/CirceStreamSupport.scala#L46-L73