jaimeiniesta

jaimeiniesta

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!

First 7 of 7 Posts Switch mode

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}}}}
jaimeiniesta

jaimeiniesta OP

Thanks @OvermindDL1 - that’s what I’m doing, catching the :exit, but I also see the [error] 4108- fatal: :unexpected_end line, I thought an error was being raised and I was unable to catch it.

But, what I think it’s happening here is that the error just gets logged, no error is raised. Does that make sense?

OvermindDL1

OvermindDL1

Actually that [error] part is a logged message from xmerl, not even really an error message, not a thrown anything, just a bit of noise from an old erlang module. You could silence it by hooking its output if you really want. :slight_smile:

jaimeiniesta

jaimeiniesta OP

That would be great, because as the error gets logged, it’s catched by the Rollbax logger backend and notified to Rollbar, which is annoying. How can I do that?

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).

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 OP

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

SweetXml.parse(doc, quiet: true)
— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement