Exception: MultiXML::ParserLoadError

Inherits:
ArgumentError
  • Object
show all
Defined in:
lib/multi_xml/errors.rb

Overview

Raised when a parser cannot be loaded or is not recognized

Covers three failure modes in one typed error, so callers can catch all "I couldn't even get to parsing" problems with one rescue:

  • Invalid spec type (not a Symbol, String, or Module)
  • LoadError from requiring the parser file
  • A custom parser that doesn't satisfy the contract (no .parse method or no parse_error method / ParseError constant)

Matches the role of MultiJSON::AdapterError.

Examples:

Catching a load error

begin
  MultiXML.parser = :bogus
rescue MultiXML::ParserLoadError => e
  puts e.message
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, cause: nil) ⇒ ParserLoadError

Create a new ParserLoadError

Examples:

ParserLoadError.new("Unknown parser", cause: original_error)

Parameters:

  • message (String, nil) (defaults to: nil)

    error message

  • cause (Exception, nil) (defaults to: nil)

    the original exception



88
89
90
91
# File 'lib/multi_xml/errors.rb', line 88

def initialize(message = nil, cause: nil)
  super(message)
  set_backtrace(cause.backtrace) if cause
end

Class Method Details

.build(original_exception) ⇒ ParserLoadError

Build a ParserLoadError from an original exception

The original exception's class name is included in the message so a downstream consumer reading just the ParserLoadError can tell whether the underlying failure was a LoadError, an ArgumentError from the spec validator, or some other class without having to look at error.cause separately.

Examples:

ParserLoadError.build(LoadError.new("cannot load such file"))

Parameters:

  • original_exception (Exception)

    the original load error

Returns:



106
107
108
109
110
111
112
# File 'lib/multi_xml/errors.rb', line 106

def self.build(original_exception)
  new(
    "Did not recognize your parser specification " \
    "(#{original_exception.class}: #{original_exception.message}).",
    cause: original_exception
  )
end