add exceptions for salmon stuff and move all to one file

This commit is contained in:
Benjamin Neff 2015-09-25 23:31:13 +02:00
parent 7b4753148b
commit 778a782c76
4 changed files with 59 additions and 22 deletions

View file

@ -5,4 +5,5 @@ module DiasporaFederation
end end
end end
require "diaspora_federation/salmon/exceptions"
require "diaspora_federation/salmon/xml_payload" require "diaspora_federation/salmon/xml_payload"

View file

@ -0,0 +1,50 @@
module DiasporaFederation
module Salmon
# Raised, if the element containing the Magic Envelope is missing from the XML
class MissingMagicEnvelope < RuntimeError
end
# Raised, if the element containing the author is empty.
class MissingAuthor < RuntimeError
end
# Raised, if the element containing the header is missing from the XML
class MissingHeader < RuntimeError
end
# Raised if the decrypted header has an unexpected XML structure
class InvalidHeader < RuntimeError
end
# Raised, if the Magic Envelope XML structure is malformed.
class InvalidEnvelope < RuntimeError
end
# Raised, if the calculated signature doesn't match the one contained in the
# Magic Envelope.
class InvalidSignature < RuntimeError
end
# Raised, if the parsed Magic Envelope specifies an unhandled algorithm.
class InvalidAlgorithm < RuntimeError
end
# Raised, if the parsed Magic Envelope specifies an unhandled encoding.
class InvalidEncoding < RuntimeError
end
# Raised, if the XML structure of the parsed document doesn't resemble the
# expected structure.
class InvalidStructure < RuntimeError
end
# Raised, if the entity name in the XML is invalid
class InvalidEntityName < RuntimeError
end
# Raised, if the entity contained within the XML cannot be mapped to a
# defined {Entity} subclass.
class UnknownEntity < RuntimeError
end
end
end

View file

@ -44,11 +44,11 @@ module DiasporaFederation
# XML can't be found # XML can't be found
def self.unpack(xml) def self.unpack(xml)
raise ArgumentError, "only Nokogiri::XML::Element allowed" unless xml.instance_of?(Nokogiri::XML::Element) raise ArgumentError, "only Nokogiri::XML::Element allowed" unless xml.instance_of?(Nokogiri::XML::Element)
raise InvalidStructure unless wrap_valid?(xml) raise Salmon::InvalidStructure unless wrap_valid?(xml)
data = xml.at_xpath("post/*[1]") data = xml.at_xpath("post/*[1]")
klass_name = entity_class_name(data.name) klass_name = entity_class_name(data.name)
raise UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name) raise Salmon::UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name)
klass = Entities.const_get(klass_name) klass = Entities.const_get(klass_name)
populate_entity(klass, data) populate_entity(klass, data)
@ -70,7 +70,7 @@ module DiasporaFederation
# @return [String] "CamelCase" class name # @return [String] "CamelCase" class name
def self.entity_class_name(term) def self.entity_class_name(term)
term.to_s.tap do |string| term.to_s.tap do |string|
raise InvalidEntityName, "'#{string}' is invalid" unless string =~ /^[a-z]*(_[a-z]*)*$/ raise Salmon::InvalidEntityName, "'#{string}' is invalid" unless string =~ /^[a-z]*(_[a-z]*)*$/
string.sub!(/^[a-z]/, &:upcase) string.sub!(/^[a-z]/, &:upcase)
string.gsub!(/_([a-z])/) { Regexp.last_match[1].upcase } string.gsub!(/_([a-z])/) { Regexp.last_match[1].upcase }
end end
@ -126,20 +126,6 @@ module DiasporaFederation
n.map {|child| populate_entity(type.first, child) } n.map {|child| populate_entity(type.first, child) }
end end
private_class_method :parse_array_from_node private_class_method :parse_array_from_node
# Raised, if the XML structure of the parsed document doesn't resemble the
# expected structure.
class InvalidStructure < RuntimeError
end
# Raised, if the entity name in the XML is invalid
class InvalidEntityName < RuntimeError
end
# Raised, if the entity contained within the XML cannot be mapped to a
# defined {Entity} subclass.
class UnknownEntity < RuntimeError
end
end end
end end
end end

View file

@ -77,7 +77,7 @@ XML
XML XML
expect { expect {
Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root) Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root)
}.to raise_error Salmon::XmlPayload::UnknownEntity, "'UnknownEntity' not found" }.to raise_error Salmon::UnknownEntity, "'UnknownEntity' not found"
end end
it "raises an error when the entity is not found" do it "raises an error when the entity is not found" do
@ -88,7 +88,7 @@ XML
XML XML
expect { expect {
Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root) Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root)
}.to raise_error Salmon::XmlPayload::InvalidStructure }.to raise_error Salmon::InvalidStructure
end end
end end
@ -136,19 +136,19 @@ XML
it "raises an error when the entity name contains special characters" do it "raises an error when the entity name contains special characters" do
expect { expect {
Salmon::XmlPayload.send(:entity_class_name, "te.st-enti/ty") Salmon::XmlPayload.send(:entity_class_name, "te.st-enti/ty")
}.to raise_error Salmon::XmlPayload::InvalidEntityName, "'te.st-enti/ty' is invalid" }.to raise_error Salmon::InvalidEntityName, "'te.st-enti/ty' is invalid"
end end
it "raises an error when the entity name contains upper case letters" do it "raises an error when the entity name contains upper case letters" do
expect { expect {
Salmon::XmlPayload.send(:entity_class_name, "TestEntity") Salmon::XmlPayload.send(:entity_class_name, "TestEntity")
}.to raise_error Salmon::XmlPayload::InvalidEntityName, "'TestEntity' is invalid" }.to raise_error Salmon::InvalidEntityName, "'TestEntity' is invalid"
end end
it "raises an error when the entity name contains numbers" do it "raises an error when the entity name contains numbers" do
expect { expect {
Salmon::XmlPayload.send(:entity_class_name, "te5t_ent1ty_w1th_number5") Salmon::XmlPayload.send(:entity_class_name, "te5t_ent1ty_w1th_number5")
}.to raise_error Salmon::XmlPayload::InvalidEntityName, "'te5t_ent1ty_w1th_number5' is invalid" }.to raise_error Salmon::InvalidEntityName, "'te5t_ent1ty_w1th_number5' is invalid"
end end
end end
end end