Skip to content

Commit

Permalink
Wrap OpenURI::HTTPError
Browse files Browse the repository at this point in the history
  • Loading branch information
sueyoshi_go committed Mar 20, 2014
1 parent 5c21373 commit 418d7d2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/android_market_api.rb
Expand Up @@ -5,3 +5,4 @@
require File.expand_path(File.dirname(__FILE__) + "/android_market_api/version")
require File.expand_path(File.dirname(__FILE__) + "/android_market_api/android_market")
require File.expand_path(File.dirname(__FILE__) + "/android_market_api/util")
require File.expand_path(File.dirname(__FILE__) + "/android_market_api/errors")
12 changes: 12 additions & 0 deletions lib/android_market_api/errors.rb
@@ -0,0 +1,12 @@
module AndroidMarketApi
class HTTPError < StandardError
attr_reader :status_code, :url, :cause

def initialize(message=nil, options={})
super(message)
@status_code = options[:status_code].to_i
@url = options[:url]
@cause = options[:cause]
end
end
end
4 changes: 4 additions & 0 deletions lib/android_market_api/util.rb
Expand Up @@ -21,6 +21,10 @@ def get_content(url, options = {})
header[name] = options[name] if options[name]
end
open(url, header).read
rescue OpenURI::HTTPError => e
e.message =~ /^([0-9]{3})/
status_code = $1
raise AndroidMarketApi::HTTPError.new("#{e.message} #{url}", :status_code => status_code, :url => url, :cause => e)
end

module_function :get_content
Expand Down
7 changes: 7 additions & 0 deletions spec/util_spec.rb
Expand Up @@ -59,5 +59,12 @@
it{ expect{subject}.to raise_error }
end
end

context "invalid url" do
let(:url){ "http://google.com/fooooooooooo" }
let(:options){ {} }

it{ expect{ subject }.to raise_error AndroidMarketApi::HTTPError, "404 Not Found http://google.com/fooooooooooo" }
end
end
end

0 comments on commit 418d7d2

Please sign in to comment.