[Xml-compile] (Abusing?) error handling in recent versions of XML::Compile

Hakim Cassimally hakim.cassimally at gmail.com
Tue Dec 9 11:35:46 GMT 2008


Hi,

We're planning to upgrade our X::C from the venerable 0.64-0.69 at the
beginning of the year to the current 0.97, yay!  Of course it looks
like XML::Compile has been a busy little module in 2008, and we may
need to change a few things.  One of them is a nasty little hack
around error reporting - I'll explain it here, but we're hoping not to
just update it for the new version but to improve the approach if
possible.

We found that XML::Compile's error reporting at the time would quite
easily lose information - so if a compile failed we'd only know that
"an element" was declared with the wrong attribute, but not which
one... grrrrr!  Also compilation would stop immediately: we wanted to
show *all* the errors to the caller, so that they can take action more
easily.

So we do this:

        my @err;
        {
            no warnings qw/once redefine/;
            my $error_sub = sub (@) {
                push @err, $self->error(
                    type   => 'Internal',
                    number => '9999',
                    Text   => shift);
                };
            *XML::Compile::Schema::XmlWriter::error     = $error_sub;
            *XML::Compile::Schema::BuiltInFacets::error = $error_sub;
            *XML::Compile::Schema::BuiltInTypes::error  = $error_sub;
            }

        my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
        my ($writer, $xml, $tree);
        $tree = $c->stash->{output}{xml}) |
               {
                regardingReferenceNumber    => '',
                version  => $c->stash->{xml_version},
                CriticalError  => "No data arrived to end",
                };
      }

                                     eval {
            $writer = $c->stash->{schema}->compile(
                                             WRITER   =>
'{http://www.example.com/webXG/path/to/schema/}Root',
                use_default_prefix  => 1,
                );
            $xml = $writer->($doc, $tree);

When the compiler gets called, any errors will get placed in @err, so
we can add them to logging and the xml response output.

We obviously can't use the above code (some of the packages we're
monkeypatching don't exist anyway even) and I'm sure that there must
be a saner approach :-)

Grateful for any advice or pointers!

regards,
osfameron



More information about the Xml-compile mailing list