Always raise a DiscoveryError when something with the discovery fails

This commit is contained in:
Benjamin Neff 2017-09-01 01:48:11 +02:00
parent 53fc77ac5d
commit 458d0f2215
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
2 changed files with 40 additions and 0 deletions

View file

@ -15,6 +15,7 @@ module DiasporaFederation
# Fetches all metadata for the account and saves it via callback
# @return [Person]
# @raise [DiscoveryError] if something with the discovery failed
def fetch_and_save
logger.info "Fetch data for #{diaspora_id}"
@ -23,6 +24,10 @@ module DiasporaFederation
DiasporaFederation.callbacks.trigger(:save_person_after_webfinger, person)
logger.info "successfully webfingered #{diaspora_id}"
person
rescue DiscoveryError
raise # simply re-raise DiscoveryError
rescue => e
raise DiscoveryError, "Failed discovery for #{diaspora_id}: #{e.class}: #{e.message}"
end
private

View file

@ -144,6 +144,41 @@ module DiasporaFederation
expect { Discovery::Discovery.new(account).fetch_and_save }.to raise_error Discovery::DiscoveryError
end
context "error handling" do
subject { Discovery::Discovery.new(account) }
it "re-raises DiscoveryError" do
expect(subject).to receive(:validate_diaspora_id)
.and_raise(Discovery::DiscoveryError, "Something went wrong!")
expect { subject.fetch_and_save }.to raise_error Discovery::DiscoveryError, "Something went wrong!"
end
it "re-raises InvalidDocument" do
expect(subject).to receive(:validate_diaspora_id)
.and_raise(Discovery::InvalidDocument, "Wrong document!")
expect { subject.fetch_and_save }.to raise_error Discovery::InvalidDocument, "Wrong document!"
end
it "re-raises InvalidData" do
expect(subject).to receive(:validate_diaspora_id)
.and_raise(Discovery::InvalidData, "Wrong data!")
expect { subject.fetch_and_save }.to raise_error Discovery::InvalidData, "Wrong data!"
end
it "raises a DiscoveryError when an unhandled error occurs" do
expect(subject).to receive(:validate_diaspora_id)
.and_raise("OMG! EVERYTHING IS BROKEN!")
expect {
subject.fetch_and_save
}.to raise_error Discovery::DiscoveryError,
"Failed discovery for #{account}: RuntimeError: OMG! EVERYTHING IS BROKEN!"
end
end
end
end
end