Exception: Dommy::DOMException

Inherits:
StandardError
  • Object
show all
Defined in:
lib/dommy/dom_exception.rb

Overview

‘DOMException` — base class for DOM-spec errors. Subclasses match the well-known names from the WebIDL spec; each carries the legacy `code` integer (0 for names introduced after the legacy code table) and the canonical `name`.

Use:

raise Dommy::DOMException::IndexSizeError, "index 5 out of range"
rescue Dommy::DOMException => e
  e.name      # => "IndexSizeError"
  e.code      # => 1
  e.message   # => "index 5 out of range"
  e.to_s      # => "IndexSizeError: index 5 out of range"

The 2-arg form mirrors JS ‘new DOMException(message, name)`:

Dommy::DOMException.new("bad input", "SyntaxError")

which constructs a base DOMException carrying the supplied name —useful when the name is dynamic and you don’t have a subclass.

Inherits from StandardError so generic ‘rescue => e` catches them.

Defined Under Namespace

Classes: AbortError, ConstraintError, DataCloneError, DataError, EncodingError, HierarchyRequestError, InUseAttributeError, IndexSizeError, InvalidAccessError, InvalidCharacterError, InvalidModificationError, InvalidNodeTypeError, InvalidStateError, NamespaceError, NetworkError, NoModificationAllowedError, NotAllowedError, NotFoundError, NotReadableError, NotSupportedError, OperationError, QuotaExceededError, ReadOnlyError, SecurityError, SyntaxError, TimeoutError, TransactionInactiveError, TypeMismatchError, URLMismatchError, UnknownError, VersionError, WrongDocumentError

Constant Summary collapse

NAME =
"Error"
CODE =
0
LEGACY_CODES =

Legacy-name → numeric-code map. Used only by the 2-arg ‘new DOMException(msg, name)` form when the supplied name doesn’t match the current subclass. Subclass-direct construction reads ‘self.class::CODE` and ignores this map.

{
  "IndexSizeError" => 1,
  "HierarchyRequestError" => 3,
  "WrongDocumentError" => 4,
  "InvalidCharacterError" => 5,
  "NoModificationAllowedError" => 7,
  "NotFoundError" => 8,
  "NotSupportedError" => 9,
  "InUseAttributeError" => 10,
  "InvalidStateError" => 11,
  "SyntaxError" => 12,
  "InvalidModificationError" => 13,
  "NamespaceError" => 14,
  "InvalidAccessError" => 15,
  "TypeMismatchError" => 17,
  "SecurityError" => 18,
  "NetworkError" => 19,
  "AbortError" => 20,
  "URLMismatchError" => 21,
  "QuotaExceededError" => 22,
  "TimeoutError" => 23,
  "InvalidNodeTypeError" => 24,
  "DataCloneError" => 25
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(message = "", name = nil) ⇒ DOMException

Returns a new instance of DOMException.



56
57
58
59
60
# File 'lib/dommy/dom_exception.rb', line 56

def initialize(message = "", name = nil)
  @__message = message.to_s
  super(@__message)
  @explicit_name = name&.to_s
end

Instance Method Details

#__js_get__(key) ⇒ Object

JS exposes ‘e.name`, `e.code`, `e.message`.



81
82
83
84
85
86
87
88
89
90
# File 'lib/dommy/dom_exception.rb', line 81

def __js_get__(key)
  case key
  when "name"
    name
  when "code"
    code
  when "message"
    message
  end
end

#codeObject

When constructed on the base class with a dynamic name, derive the code from LEGACY_CODES (0 for unknown). When constructed via a subclass, always return that subclass’s CODE so the legacy number is preserved even if the caller passed a different name.



74
75
76
77
78
# File 'lib/dommy/dom_exception.rb', line 74

def code
  return self.class::CODE if self.class != DOMException || @explicit_name.nil?

  LEGACY_CODES[@explicit_name] || 0
end

#messageObject



62
63
64
# File 'lib/dommy/dom_exception.rb', line 62

def message
  @__message.to_s
end

#nameObject



66
67
68
# File 'lib/dommy/dom_exception.rb', line 66

def name
  @explicit_name || self.class::NAME
end

#to_sObject



92
93
94
# File 'lib/dommy/dom_exception.rb', line 92

def to_s
  "#{name}: #{@__message}"
end