Skip to content

Commit

Permalink
Filter suppressed errors and warnings early #115
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Nov 24, 2024
1 parent cbbe29e commit 081ef6a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
40 changes: 19 additions & 21 deletions lib/LibXML/ErrorHandling.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ role LibXML::ErrorHandling {
method config {...}
has Lock $.lock .= new;
has X::LibXML @.errors;
has $!min-error-level = self.suppress-errors
?? XML_ERR_FATAL
!! (self.suppress-warnings ?? XML_ERR_ERROR !! XML_ERR_NONE);
has UInt $.max-errors = self.config.max-errors;
has $.global-error-handling is built = True;

Expand Down Expand Up @@ -274,21 +277,23 @@ role LibXML::ErrorHandling {
method structured-error(xmlError:D $_) {
CATCH { default { note "error handling structured error: $_" } }

$!lock.protect: {
if @!errors <= $!max-errors {
my Int $level = .level;
my Str $file = .file;
my UInt:D $line = .line;
my Str() $context = .context(my uint32 $column);
my UInt:D $code = .code;
my UInt:D $domain-num = .domain;
my Str $msg = .message;
$column ||= .column;
$msg //= $code.Str;
if $msg ~~ /^\d+$/ {
$msg ~= " ({.key})" with xmlParserErrors($msg.Int);
if .level >= $!min-error-level {
$!lock.protect: {
if @!errors <= $!max-errors {
my Int $level = .level;
my Str $file = .file;
my UInt:D $line = .line;
my Str() $context = .context(my uint32 $column);
my UInt:D $code = .code;
my UInt:D $domain-num = .domain;
my Str $msg = .message;
$column ||= .column;
$msg //= $code.Str;
if $msg ~~ /^\d+$/ {
$msg ~= " ({.key})" with xmlParserErrors($msg.Int);
}
self!error: X::LibXML::Parser.new( :$level, :$msg, :$file, :$line, :$column, :$code, :$domain-num, :$context );
}
self!error: X::LibXML::Parser.new( :$level, :$msg, :$file, :$line, :$column, :$code, :$domain-num, :$context );
}
}
}
Expand Down Expand Up @@ -349,13 +354,6 @@ role LibXML::ErrorHandling {
@!errors = ();
}

if self.suppress-errors {
@errs .= grep: *.level > XML_ERR_ERROR;
}
elsif self.suppress-warnings {
@errs .= grep({ .level >= XML_ERR_ERROR })
}

my X::LibXML $fatal = @errs.first: *.level >= XML_ERR_ERROR;
my X::LibXML $err = @errs.tail;
@errs[$_].prev = @errs[$_-1] for 1 ..^ +@errs;
Expand Down
1 change: 0 additions & 1 deletion lib/LibXML/Parser/Context.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ multi method do(::?CLASS:D $ctx: &action, Bool :$recover = $.recover, Bool :$che
.flush-errors for @input-contexts;
$rv := $ctx.is-valid if $check-valid;
$ctx.flush-errors: :$recover;
$ctx.publish() without self;

LEAVE {
self.config.restore(@prev);
Expand Down
15 changes: 8 additions & 7 deletions t/00dtd.t
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ is $b.value, 'inherited';

my SaxHandler $sax-handler .= new();

$doc .= parse( string => q:to<EOF>, :load-ext-dtd, :$sax-handler, :suppress-warnings);
<!DOCTYPE test PUBLIC "-//TEST" "test.dtd" []>
<test>
<title>T1</title>
</test>
EOF

quietly {
$doc .= parse( string => q:to<EOF>, :load-ext-dtd, :$sax-handler);
<!DOCTYPE test PUBLIC "-//TEST" "test.dtd" []>
<test>
<title>T1</title>
</test>
EOF
}
is $level, +XML_ERR_WARNING;
like $message, rx/'failed to load'.*'"test.dtd"'/;

Expand Down

0 comments on commit 081ef6a

Please sign in to comment.