Skip to content
This repository has been archived by the owner on Jan 31, 2018. It is now read-only.

Commit

Permalink
Added some error handling / reporting to the http requests. Closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
dazoakley committed Aug 28, 2009
1 parent 1dcda45 commit d56b1b7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/biomart.rb
Expand Up @@ -12,18 +12,19 @@ module Biomart
# Specialised classes for error reporting
class BiomartError < StandardError
attr_reader :data

def initialize(data)
@data = data
super
end
end

class Unauthorized < StandardError; end
class Unavailable < StandardError; end
class NotFound < StandardError; end

@@url = 'http://www.biomart.org/biomart/martservice'
class HTTPError < StandardError; end
class BiomartFilterError < BiomartError; end
class BiomartAttributeError < BiomartError; end
class BiomartDatasetError < BiomartError; end

@@url = 'http://www.biomart.org/biomart/martservice'
@@client = Net::HTTP

def request( params={} )
Expand All @@ -42,6 +43,23 @@ def request( params={} )
res = get( params )
end

# Process the response code/body to catch errors.
if res.code != "200"
raise HTTPError, "HTTP error #{res.code}, please check your biomart server and URL settings."
else
if res.body =~ /ERROR/
if res.body =~ /Filter (.+) NOT FOUND/
raise BiomartFilterError.new(res.body), "Biomart error. Filter #{$1} not found."
elsif res.body =~ /Attribute (.+) NOT FOUND/
raise BiomartAttributeError.new(res.body), "Biomart error. Attribute #{$1} not found."
elsif res.body =~ /Dataset (.+) NOT FOUND/
raise BiomartDatasetError.new(res.body), "Biomart error. Dataset #{$1} not found."
else
raise BiomartError.new(res.body), "Biomart error."
end
end
end

return res.body
end

Expand Down
42 changes: 42 additions & 0 deletions test/test_biomart.rb
Expand Up @@ -93,4 +93,46 @@ def setup
assert( search2.first["marker_symbol"] == "Cbx1", "Biomart::Dataset.search (filters and attributes defined with processing) is not returning the correct info." )
end
end

context "The Biomart module" do
setup do
@not_biomart = Biomart::Server.new('http://www.sanger.ac.uk')
@htgt_targ = @htgt.datasets["htgt_targ"]
@bad_dataset = Biomart::Dataset.new( "http://www.sanger.ac.uk/htgt/biomart", { :name => "wibble" } )
end

should "handle user/configuration errors (i.e. incorrect URLs etc)" do
begin
@not_biomart.list_databases
rescue Biomart::HTTPError => e
http_error = e
end

assert( http_error.is_a?( Biomart::HTTPError ), "Biomart.request is not processing HTTP errors correctly." )
end

should "handle biomart server errors gracefully" do
begin
@htgt_targ.count( :filters => { "wibbleblibbleblip" => "1" } )
rescue Biomart::BiomartFilterError => e
filter_error = e
end

begin
@htgt_targ.search( :attributes => ["wibbleblibbleblip"] )
rescue Biomart::BiomartAttributeError => e
attribute_error = e
end

begin
@bad_dataset.count()
rescue Biomart::BiomartDatasetError => e
dataset_error = e
end

assert( filter_error.is_a?( Biomart::BiomartFilterError ), "Biomart.request is not handling Biomart filter errors correctly." )
assert( attribute_error.is_a?( Biomart::BiomartAttributeError ), "Biomart.request is not handling Biomart attribute errors correctly." )
assert( dataset_error.is_a?( Biomart::BiomartDatasetError ), "Biomart.request is not handling Biomart dataset errors correctly." )
end
end
end

0 comments on commit d56b1b7

Please sign in to comment.