3 XML::LibXML::Error - Structured Errors
11 # handle a structured error (XML::LibXML::Error object)
13 # error, but not an XML::LibXML::Error object
18 $XML::LibXML::Error::WARNINGS=1;
19 $message = $@->as_string();
21 $error_domain = $@->domain();
22 $error_code = $@->code();
23 $error_message = $@->message();
24 $error_level = $@->level();
25 $filename = $@->file();
27 $nodename = $@->nodename();
28 $error_str1 = $@->str1();
29 $error_str2 = $@->str2();
30 $error_str3 = $@->str3();
31 $error_num1 = $@->num1();
32 $error_num2 = $@->num2();
33 $string = $@->context();
34 $offset = $@->column();
35 $previous_error = $@->_prev();
39 The XML::LibXML::Error class is a tiny frontend to I<<<<<< libxml2 >>>>>>'s structured error support. If XML::LibXML is compied with structured error
40 support, all errors reported by libxml2 are transformed to XML::LibXML:Error
41 objects. These objects automatically serialize to the corresponding error
42 messages when printed or used in a string operation, but as objects, can also
43 be used to get a detailed and structured information about the error that
46 Unlike most other XML::LibXML objects, XML::LibXML::Error doesn't wrap an
47 underlying I<<<<<< libxml2 >>>>>> structure directly, but rather transforms it to a blessed Perl hash reference
48 containing the individual fields of the structured error information as hash
49 key-value pairs. Individual items (fields) of a structured error can either be
50 obtained directly as $@->{field}, or using autoloaded methods such as as
51 $@->field() (where field is the field name). XML::LibXML::Error objects have
52 the following fields: domain, code, level, file, line, nodename, message, str1,
53 str2, str3, num1, num2, and _prev (some of them may be undefined).
57 =item $XML::LibXML::Error::WARNINGS
59 $XML::LibXML::Error::WARNINGS=1;
61 Traditionally, XML::LibXML was supressing parser warnings by setting libxml2's
62 global variable xmlGetWarningsDefaultValue to 0. Since 1.70 we do not change
63 libxml2's global variables anymore; for backward compatibility, XML::LibXML
64 suppresses warnings. This variable can be set to 1 to enable reporting of these
65 warnings via Perl C<<<<<< warn >>>>>> and to 2 to report hem via C<<<<<< die >>>>>>.
67 $message = $@->as_string();
69 This functions takes serializes a XML::LibXML::Error object to a string
70 containing the full error message close to the message produced by I<<<<<< libxml2 >>>>>> default error handlers and tools like xmllint. This method is also used to
71 overload "" operator on XML::LibXML::Error, so it is automatically called
72 whenever XML::LibXML::Error object is treated as a string (e.g. in print $@).
79 This function serializes a XML::LibXML::Error to a string displaying all fields
80 of the error structure individually on separate lines of the form 'name' =>
86 $error_domain = $@->domain();
88 Returns string containing information about what part of the library raised the
89 error. Can be one of: "parser", "tree", "namespace", "validity", "HTML parser",
90 "memory", "output", "I/O", "ftp", "http", "XInclude", "XPath", "xpointer",
91 "regexp", "Schemas datatype", "Schemas parser", "Schemas validity", "Relax-NG
92 parser", "Relax-NG validity", "Catalog", "C14N", "XSLT", "validity".
97 $error_code = $@->code();
99 Returns the actual libxml2 error code. The XML::LibXML::ErrNo module defines
100 constants for individual error codes. Currently libxml2 uses over 480 different
106 $error_message = $@->message();
108 Returns a human-readable informative error message.
113 $error_level = $@->level();
115 Returns an integer value describing how consequent is the error.
116 XML::LibXML::Error defines the following constants:
129 XML_ERR_WARNING = 1 : A simple warning.
135 XML_ERR_ERROR = 2 : A recoverable error.
141 XML_ERR_FATAL = 3 : A fatal error.
150 $filename = $@->file();
152 Returns the filename of the file being processed while the error occurred.
159 The line number, if available.
164 $nodename = $@->nodename();
166 Name of the node where error occurred, if available. When this field is
167 non-empty, libxml2 actually returned a physical pointer to the specified node.
168 Due to memory management issues, it is very difficult to implement a way to
169 expose the pointer to the Perl level as a XML::LibXML::Node. For this reason,
170 XML::LibXML::Error currently only exposes the name the node.
175 $error_str1 = $@->str1();
177 Error specific. Extra string information.
182 $error_str2 = $@->str2();
184 Error specific. Extra string information.
189 $error_str3 = $@->str3();
191 Error specific. Extra string information.
196 $error_num1 = $@->num1();
198 Error specific. Extra numeric information.
203 $error_num2 = $@->num2();
205 In recent libxml2 versions, this value contains a column number of the error or
211 $string = $@->context();
213 For parsing errors, this field contains about 80 characters of the XML near the
214 place where the error occurred. The field C<<<<<< $@->column() >>>>>> contains the corresponding offset. Where N/A, the field is undefined.
219 $offset = $@->column();
221 See C<<<<<< $@->column() >>>>>> above.
226 $previous_error = $@->_prev();
228 This field can possibly hold a reference to another XML::LibXML::Error object
229 representing an error which occurred just before this error.
248 2001-2007, AxKit.com Ltd.
250 2002-2006, Christian Glahn.
252 2006-2009, Petr Pajas.