-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy patherror.cr
52 lines (43 loc) · 1.2 KB
/
error.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require "./libxml2"
class XML::Error < Exception
getter line_number : Int32
def self.new(error : LibXML::Error*)
new String.new(error.value.message).chomp, error.value.line
end
def initialize(message, @line_number)
super(message)
end
# TODO: this logic isn't thread/fiber safe, but error checking is less needed than
# the ability to parse HTML5 and malformed documents. In any case, fix this.
@@errors = [] of self
LibXML.xmlSetStructuredErrorFunc nil, ->(ctx, error) {
@@errors << XML::Error.new(error)
}
LibXML.xmlSetGenericErrorFunc nil, ->(ctx, fmt) {
# TODO: use va_start and va_end to
message = String.new(fmt).chomp
error = XML::Error.new(message, 0)
{% if flag?(:arm) || flag?(:aarch64) %}
# libxml2 is likely missing ARM unwind tables (.ARM.extab and .ARM.exidx
# sections) which prevent raising from a libxml2 context.
@@errors << error
{% else %}
raise error
{% end %}
}
# :nodoc:
def self.set_errors(node)
if errors = self.errors
node.errors = errors
end
end
def self.errors
if @@errors.empty?
nil
else
errors = @@errors.dup
@@errors.clear
errors
end
end
end