Skip to content

Commit

Permalink
Change to raise unparser specific error on unknown node type
Browse files Browse the repository at this point in the history
* This unparser specific exception type makes it easy to catch errors
  originating from the specific case unparser is exposed to a node type
  it does not understand.
  • Loading branch information
palkan committed Jun 10, 2020
1 parent 9501d31 commit 833b6a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# v0.4.8 unreleased

* Change to specific node type when unparser fails on an unknown node type: [#150](https://github.com/mbj/unparser/pull/150)
* Significantly improve verifier (only useful for debugging)
* Add `Unparser::Color` module for colorized source diffs

Expand Down
3 changes: 2 additions & 1 deletion lib/unparser/emitter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module Unparser
UnknownNodeError = Class.new(ArgumentError)

# Emitter base class
#
Expand Down Expand Up @@ -116,7 +117,7 @@ def write_to_buffer
def self.emitter(node, parent)
type = node.type
klass = REGISTRY.fetch(type) do
raise ArgumentError, "No emitter for node: #{type.inspect}"
raise UnknownNodeError, "Unknown node type: #{type.inspect}"
end
klass.new(node, parent)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/unit/unparser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ def apply
end
end

describe '.unparse' do
context 'on unknown node type' do
def apply
Unparser.unparse(node)
end

let(:node) { s(:example_node) }

it 'raises UnknownNodeError' do
expect { apply }.to raise_error(
Unparser::UnknownNodeError,
'Unknown node type: :example_node'
)
end
end
end

describe '.unparse' do
let(:builder_options) { {} }

Expand Down

0 comments on commit 833b6a5

Please sign in to comment.