Hello, I’ve tried 2 libraries for parsing XML: sweet_xml and quinn, and both rely on the Erlang package xmerl.
They both work great, but when they encounter malformed XML, they error and exit like this:
iex(4)> Quinn.parse "<xml>"
** (exit) {:fatal, {:unexpected_end, {:file, :file_name_unknown}, {:line, 1}, {:col, 6}}}
16:21:51.714 [error] 4108- fatal: :unexpected_end
xmerl_scan.erl:4111: :xmerl_scan.fatal/2
xmerl_scan.erl:572: :xmerl_scan.scan_document/2
xmerl_scan.erl:288: :xmerl_scan.string/2
lib/xml_parser/xml_parser.ex:5: Quinn.XmlParser.parse/1
If I directly use :xmerl_scan.string/1
I get the same error:
iex(1)> :xmerl_scan.string '<xml>'
** (exit) {:fatal, {:unexpected_end, {:file, :file_name_unknown}, {:line, 1}, {:col, 6}}}
16:36:46.245 [error] 4108- fatal: :unexpected_end
xmerl_scan.erl:4111: :xmerl_scan.fatal/2
xmerl_scan.erl:572: :xmerl_scan.scan_document/2
xmerl_scan.erl:288: :xmerl_scan.string/2
Now, I want to make my app rescue from this error, but I’m not being able to do this. I’ve tried this but it doesn’t work:
iex(1)> try do
...(1)> :xmerl_scan.string '<xml>'
...(1)> rescue
...(1)> RuntimeError -> "Error!"
...(1)> end
** (exit) {:fatal, {:unexpected_end, {:file, :file_name_unknown}, {:line, 1}, {:col, 6}}}
iex(1)>
16:40:20.003 [error] 4108- fatal: :unexpected_end
I suppose that it’s because it’s not a RuntimeError
I’m dealing with, because it’s not from Elixir, but Erlang. I can catch
the exit, however.
How can I rescue from this error?
Thanks!