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

Add ability to track the location of cells that raise casting exceptions #85

Merged
merged 5 commits into from
Jul 13, 2019

Conversation

brunslo
Copy link
Contributor

@brunslo brunslo commented Jun 18, 2019

With a backwards compatible change to the Casting interface it is simple to add the ability to log formatting errors found during the casting of values from the original workbook cells. The change adds a default method extending castValue which accepts row and column numbers along with the regular casting parameters. This change, coupled with the passing of already known row and column numbers in HSSFUnmarshaller and PoijiHandler into the few places castValue is called gives a light weight error location tracker for very little cost.

An example of how this could be done is provided in the LogCasting class, which is provided as a simple proof of concept without tests.

Using a default implementation of castValue(…) with row and column numbers we can silently add the ability to record the location of any casting being done for logging purposes.
This is an example implementation of the newly modified Casting interface, which stores information about casting errors for later retrieval.
@coveralls
Copy link

coveralls commented Jun 18, 2019

Coverage Status

Coverage increased (+5.1%) to 92.614% when pulling 5366cb0 on brunslo:feature/error-handling into 3d27d7a on ozlerhakan:master.

@ozlerhakan
Copy link
Owner

Hi @brunslo , thank you for your effort! here we need to improve our coverage more to be able to accept this PR

Fix regression due to overzealous IDE.
Given that the original authors were open to logging location in Casting, it makes sense to reduce code duplication by adding the logging code directly to DefaultCasting.

Logging is disabled by default and enabled with a boolean flag to the constructor.

A new public method isErrorLoggingEnabled() is added, which indicates whether or not logging was enabled on creation.

A new public method getErrors() is also added that returns details of any exception thrown during a cast, including the sheet, row, and column. This method throw an IllegalStateException when called on an instance without logging enabled. This ensures that a returned empty getErrors() List when logging is disabled is not misinterpreted to mean there were not casting exceptions thrown during unmarshaling.

Existing tests for DefaultCasting now include a check to ensure that logging is disabled and that the expected IllegalStateException is thrown when attempting to call getErrors(). Another set of tests that interrogates the logging will be in a forthcoming commit.
@brunslo
Copy link
Contributor Author

brunslo commented Jun 20, 2019

I took the presumptive step to fold the logging additions into the DefaultCasting class itself rather than have a separate implementation. This remove quite a lot of code duplication.

I still have the Casting interface using the new default method with row and column information to preserve backwards compatibility. I'm happy to remove that default method and simply have Casting now require one castValue(...) method with row and column information, and thereby break backwards compatibility, if that's desired.

@ozlerhakan
Copy link
Owner

Thank you @brunslo , I'll look at the code you made 🎉

@ozlerhakan ozlerhakan merged commit 5366cb0 into ozlerhakan:master Jul 13, 2019
@ozlerhakan
Copy link
Owner

merged @brunslo ! 🎉

@brunslo brunslo deleted the feature/error-handling branch July 23, 2019 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants