Skip to content

Commit

Permalink
Raise specific error when an anchor isn't defined
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Jul 27, 2022
1 parent 42b43de commit 98fbd52
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/psych/exception.rb
Expand Up @@ -13,6 +13,13 @@ def initialize
end
end

# Subclasses `BadAlias` for backwards compatibility
class AnchorNotDefined < BadAlias
def initialize anchor_name
super "An alias referenced an unknown anchor: #{anchor_name}"
end
end

class DisallowedClass < Exception
def initialize action, klass_name
super "Tried to #{action} unspecified class: #{klass_name}"
Expand Down
2 changes: 1 addition & 1 deletion lib/psych/visitors/to_ruby.rb
Expand Up @@ -323,7 +323,7 @@ def visit_Psych_Nodes_Stream o
end

def visit_Psych_Nodes_Alias o
@st.fetch(o.anchor) { raise BadAlias, "Unknown alias: #{o.anchor}" }
@st.fetch(o.anchor) { raise AnchorNotDefined, o.anchor }
end

private
Expand Down
2 changes: 1 addition & 1 deletion test/psych/test_hash.rb
Expand Up @@ -124,7 +124,7 @@ def test_anchor_reuse
end

def test_raises_if_anchor_not_defined
assert_raise(Psych::BadAlias) do
assert_raise(Psych::AnchorNotDefined) do
Psych.unsafe_load(<<~eoyml)
---
foo: &foo
Expand Down
2 changes: 1 addition & 1 deletion test/psych/test_merge_keys.rb
Expand Up @@ -117,7 +117,7 @@ def test_missing_merge_key
bar:
<< : *foo
eoyml
exp = assert_raise(Psych::BadAlias) { Psych.load(yaml, aliases: true) }
exp = assert_raise(Psych::AnchorNotDefined) { Psych.load(yaml, aliases: true) }
assert_match 'foo', exp.message
end

Expand Down

0 comments on commit 98fbd52

Please sign in to comment.