123456789_123456789_123456789_123456789_123456789_

Exception: LibXML::XML::Error

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::StandardError, Exception
Instance Chain:
self, ::StandardError, Exception
Inherits: StandardError
Defined in: ext/libxml/ruby_xml_error.c,
ext/libxml/ruby_xml_error.c,
lib/libxml/error.rb

Overview

The Error class exposes libxml errors as standard Ruby exceptions. When appropriate, libxml-ruby will raise an exception - for example, when parsing a non-well formed xml document.

By default, warnings, errors and fatal errors that libxml generates are printed to STDERR via the VERBOSE_HANDLER proc.

To disable this output you can install the quiet handler:

XML::Error.set_handler(&XML::Error::QUIET_HANDLER)

Get the current handler:

proc = XML::Error.get_handler

Install your own handler:

XML::Error.set_handler do |error|
  puts error.to_s
end

Or remove all handlers:

XML::Error.reset_handler

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Class Method Details

.get_error_handler

Returns the proc that will be called when libxml generates warning, error or fatal error messages.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_error.c', line 47

static VALUE rxml_error_get_handler(VALUE self)
{
  VALUE block = rb_cvar_get(eXMLError, ERROR_HANDLER_ID);
  return block;
}

.reset_error_handler

Removes the current error handler.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_error.c', line 159

static VALUE rxml_error_reset_handler(VALUE self)
{
  rxml_set_handler(self, Qnil);
  return self;
}

.set_error_handler {|error| ... }

Registers a block that will be called with an instance of Error when libxml generates warning, error or fatal error messages.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_error.c', line 138

static VALUE rxml_error_set_handler(VALUE self)
{
  VALUE block;

  if (rb_block_given_p() == Qfalse)
    rb_raise(rb_eRuntimeError, "No block given");

  block = rb_block_proc();

  /* Embed the block within the Error class to avoid it to be collected.
   Previous handler will be overwritten if it exists. */
  rxml_set_handler(self, block);

  return self;
}

Instance Attribute Details

#code (readonly)

[ GitHub ]

#ctxt (readonly)

[ GitHub ]

#domain (readonly)

[ GitHub ]

#file (readonly)

[ GitHub ]

#int1 (readonly)

[ GitHub ]

#int2 (readonly)

[ GitHub ]

#level (readonly)

Error attributes

[ GitHub ]

#line (readonly)

[ GitHub ]

#node (readonly)

[ GitHub ]

#str1 (readonly)

[ GitHub ]

#str2 (readonly)

[ GitHub ]

#str3 (readonly)

[ GitHub ]

Instance Method Details

#==(other)

[ GitHub ]

  
# File 'lib/libxml/error.rb', line 37

def ==(other)
  eql?(other)
end

#code_to_s

[ GitHub ]

  
# File 'lib/libxml/error.rb', line 76

def code_to_s
  ERROR_CODE_MAP[self.code].to_s
end

#domain_to_s

[ GitHub ]

  
# File 'lib/libxml/error.rb', line 72

def domain_to_s
  DOMAIN_CODE_MAP[self.domain].to_s
end

#eql?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/libxml/error.rb', line 41

def eql?(other)
  self.code == other.code and
  self.domain == other.domain and
  self.message == other.message and
  self.level == other.level and
  self.file == other.file and
  self.line == other.line and
  self.str1 == other.str1 and
  self.str2 == other.str2 and
  self.str3 == other.str3 and
  self.int1 == other.int1 and
  self.int2 == other.int2 and
  self.ctxt == other.ctxt and
  self.node == other.node
rescue
  false
end

#level_to_s

[ GitHub ]

  
# File 'lib/libxml/error.rb', line 59

def level_to_s
  case self.level
    when NONE
      ''
    when WARNING
      'Warning:'
    when ERROR
      'Error:'
    when FATAL
      'Fatal error:'
  end
end

#to_s

[ GitHub ]

  
# File 'lib/libxml/error.rb', line 80

def to_s
  msg = super
  msg = msg ? msg.strip: ''

  if self.line
    sprintf("%s %s at %s:%d.", self.level_to_s, msg,
                               self.file, self.line)
  else
    sprintf("%s %s.", self.level_to_s, msg)
  end
end