This repository has been archived by the owner on Jun 18, 2019. It is now read-only.
Log warnings when invalid elements are found in a collection #189
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey! We wanted to share a little feature we included in our project that you guys might also like.
Motivation
We have a feature in our app that is a list of trips. Some users were reporting that some trips were missing from it. We started researching and found out that the problem was in unboxing an entity of that list.
When unboxing of an entity fails, we always log an error into our non-fatal log and send it to our API. In this case, we didn't have any error about this.
Upon further research, we found that the issue was that the array was using
allowInvalidElements
property so that one item doesn't break the unbox of that collection. In this case, one trip was broken and thus it was not listed.We use
allowInvalidElements
in multiple places in our app. We want to ensure that whenever an element in a list fails, we're informed about it and that's why we implemented this feature.How it works
There's a global logger for all warnings in
Unboxer.warningLogger
. Devs who are usingUnbox
in their project can optionally set a logger in this property to start listening to these notifications.Internally we added a few calls to this static property from inside the collection mapping methods.
Further improvement
We identified other types of warnings that we are interested in logging. Imagine you have the following model:
Now, imagine the API sends you the following:
When unboxing that JSON, you'll get the following:
So in this example the API is sending a
profession
attribute but we're not correctly parsing it. As it is an optional value, unbox process succeeds correctly but something is wrong.This could be also logged as a warning to indicate that the key was present but unboxing failed.