Skip to content

Commit

Permalink
use new error handling for XPath contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed May 31, 2024
1 parent f799ad9 commit 2a64756
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/LibXML/ErrorHandling.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ role LibXML::ErrorHandling {

# SAX External Callback
sub structured-error-cb($ctx, xmlError:D $err) is export(:structured-error-cb) {
CATCH { default { note "error handling XML structured error: $_" } }
$*XML-CONTEXT.structured-error($err);
CATCH { default { note "error handling XML structured error: $_" } }
$*XML-CONTEXT.structured-error($err);
}

# API Callback - structured
Expand Down
3 changes: 3 additions & 0 deletions lib/LibXML/Raw.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,9 @@ class xmlXPathContext is repr('CStruct') is export {
constant xmlXPathFunction = Pointer;
method RegisterFuncLookup( &func4 (xmlXPathContext, xmlCharP $name, xmlCharP $ns-uri --> xmlXPathFunction), Pointer) is native($XML2) is symbol('xmlXPathRegisterFuncLookup') {*};
method FunctionLookupNS(xmlCharP $name, xmlCharP $ns_uri --> xmlXPathFunction) is native($XML2) is symbol('xmlXPathFunctionLookupNS') {*};
# recommended libxml 2.13.0+
method SetErrorHandler(&error-func (xmlXIncludeCtxt $, xmlError $)) is native($XML2) is symbol('xmlXPathSetErrorHandler') {*};
# legacy
method SetStructuredErrorFunc( &error-func (xmlXPathContext $, xmlError $)) is native($BIND-XML2) is symbol('domSetXPathCtxtErrorHandler') {*};
}

Expand Down
14 changes: 7 additions & 7 deletions lib/LibXML/SAX.rakumod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use LibXML::Parser;
unit class LibXML::SAX;

class LibXML::SAX
is LibXML::Parser {
use LibXML::SAX::Handler::SAX2;
use LibXML::Parser;
also is LibXML::Parser;

submethod TWEAK {
self.sax-handler //= LibXML::SAX::Handler::SAX2;
}
use LibXML::SAX::Handler::SAX2;

submethod TWEAK {
self.sax-handler //= LibXML::SAX::Handler::SAX2;
}

12 changes: 9 additions & 3 deletions lib/LibXML/XPath/Context.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ proto submethod TWEAK (xmlXPathContext :$raw) {
with $raw {
.Reference with .node;
}
self.init-local-error-handling
unless self.config.version < v2.13.00;
{*}
}

Expand Down Expand Up @@ -185,8 +187,11 @@ method !do(&action) is hidden-from-backtrace {
my $rv;

protected sub () is hidden-from-backtrace {
my $handlers = xml6_gbl::save-error-handlers();
self.SetGenericErrorFunc: &generic-error-cb;
my $handlers;
if self.global-error-handling {
$handlers = xml6_gbl::save-error-handlers();
self.SetGenericErrorFunc: &generic-error-cb;
}
my $*XPATH-CONTEXT = self;
my @prev = self.config.setup;

Expand All @@ -196,7 +201,8 @@ method !do(&action) is hidden-from-backtrace {

LEAVE {
self.config.restore(@prev);
xml6_gbl::restore-error-handlers($handlers);
xml6_gbl::restore-error-handlers($handlers)
if self.global-error-handling;
}
}
$rv;
Expand Down
7 changes: 1 addition & 6 deletions t/00errors.t
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use v6;
use Test;
use LibXML::XPath::Context;
plan 4;

my $errors;
LibXML::XPath::Context.SetGenericErrorFunc(-> |c { $errors++ });
plan 5;
{
use LibXML::Pattern;
my LibXML::Pattern $patt;
Expand All @@ -20,5 +17,3 @@ plan 5;
lives-ok {$regexp.new(:regexp('a'))};
throws-like { $regexp.new(:regexp('a[zz')) }, X::LibXML::OpFail, :message('XML RegExp Compile operation failed');
}

ok $errors, 'errors caught';

0 comments on commit 2a64756

Please sign in to comment.