jaimeiniesta

jaimeiniesta

Rescuing from an Erlang error

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!

Marked As Solved

OvermindDL1

OvermindDL1

Easiest thing would be just to set a new group leader for its (yours? If run in the same process, I don’t know if it is or not) process.

It should be able to be done via:

f = get_empty_group_leader() # Implement this yourself as a supervised GenServer somewhere
:elixir.group_leader(f, self()) # Or instead of self() use whatever is the PID of the xmerl process

The part you need to implement yourself of get_empty_group_leader() just use a GenServer somewhere that implements the file streaming erlang protocol (see erlang docs), or have it basically ignore everything (I think you need to send :ok back or something on a few of them though).

Also Liked

OvermindDL1

OvermindDL1

It is not ‘raising’ an error, so rescue will not handle it. It is ‘throwing’ an :exit, so you need to catch it like:

iex> try do
...>   exit "I am exiting"
...> catch
...>   :exit, _ -> "not really"
...> end
"not really"

And that is the only way to handle an exit in that context (without looking at xmerl itself, if it is another process then trapping exits would work too).

EDIT: Or more accurately for your use case:

iex(2)> try do
...(2)>  :xmerl_scan.string '<xml>'
...(2)> catch
...(2)>  :exit, e -> {:nope, e}
...(2)> end
[error] 4108- fatal: :unexpected_end

{:nope,
 {:fatal,
  {:unexpected_end, {:file, :file_name_unknown}, {:line, 1}, {:col, 6}}}}
alecnmk

alecnmk

Greetings,

Actually there is event simpler solution(s).

  1. xmerl_scan has quiet ({quiet, Flag}) as one of the options. When set to true xmerl behaves and nothing get’s error-logged. Details are here: http://erlang.org/doc/man/xmerl_scan.html

  2. I was experiencing same issue with false error reporting as @jaimeiniesta. And that’s Elixir Logger capturing erlang’s standard_error and logs it with error level to configured Logger backends. See :handle_otp_reports config option of the Elixir.Logger

  3. Solution with wrapping xmerl parsing in a separate process and setting group leader to stub GenServer didn’t work for me. But good implementation example could be found in ExUnit Capture logs/io sources: https://github.com/elixir-lang/elixir/blob/master/lib/ex_unit/lib/ex_unit/capture_io.ex

Cheers

jaimeiniesta

jaimeiniesta

For the record, SweetXml allows passing options to xmerl when parsing:

SweetXml.parse(doc, quiet: true)

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement