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

bug in casting values to integer or float #70

Open
ckruse opened this issue Mar 23, 2019 · 0 comments
Open

bug in casting values to integer or float #70

ckruse opened this issue Mar 23, 2019 · 0 comments

Comments

@ckruse
Copy link

ckruse commented Mar 23, 2019

Hi,

I'm trying to parse a XML feed with sweet_xml. Due to the nature of the web ;-) there may be feeds that are invalid. In this case I had a feed with a missing element, and thus the XPath expression could not be resolved;

However, this clause matches for an integer cast in this case:

defp to_cast(value, :integer, _is_opt?), do: String.to_integer(to_string(value))

This leads to an argument error because String.to_integer gets called with an empty string:

** (ArgumentError) argument error
    :erlang.binary_to_integer("")
    (sweet_xml) lib/sweet_xml.ex:665: SweetXml.to_cast/3
    (sweet_xml) lib/sweet_xml.ex:441: SweetXml.xpath/2
    (sweet_xml) lib/sweet_xml.ex:531: anonymous fn/3 in SweetXml.xmap/3
    (elixir) lib/map.ex:791: Map.get_and_update/3
    (sweet_xml) lib/sweet_xml.ex:531: SweetXml.xmap/3
    (sweet_xml) lib/sweet_xml.ex:530: SweetXml.xmap/3

Is this a thing you will want to fix? All other XPath expressions don't lead to a crash, so it seems like a bug to me. On the other hand, the input is invalid; what's the right thing to do here?

To fix this one could add another function clause for empty strings in casts to float or integer; but then it would fail if the input string is not a number.

That's why I didn't create a pull request, I'm really not sure what your thoughts are about this.

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

No branches or pull requests

1 participant