Permalink
Browse files

Improve network-related error handlers

Check the HTTP status code of every request, and raise an exception with
a (relatively) useful message whenever an error occurs.

Also changed the order of some exception raising statements
('<condition> and raise "..."' instead of raise '"...." unless
<condition>'). It's a matter of preference and starting the statements
with the condition feels more natural to me.
  • Loading branch information...
1 parent 85cb82a commit f76d0f08c7998a201372b19cba285e3e066e7b22 Gustavo Machado Campagnani Gama committed Dec 23, 2010
@@ -15,7 +15,7 @@ module Client
# specific token
def token
resp = request_proxy.get('/reader/api/0/token')
- raise "unable to retrieve token" unless resp.code_type == Net::HTTPOK
+ resp.code_type == Net::HTTPOK or raise "unable to retrieve token: #{resp.inspect}"
@google_reader_token = resp.body
end
@@ -38,7 +38,7 @@ def user
# (which includes the logged user him/herself)
def friends
resp = request_proxy.get('/reader/api/0/friend/list?output=json')
- raise "unable to retrieve the list of friends" unless resp.code_type == Net::HTTPOK
+ resp.code_type == Net::HTTPOK or raise "unable to retrieve the list of friends: #{resp.inspect}"
JSON.parse(resp.body)['friends'].collect do |friend|
((friend['flags'] & (1 << User::IS_ME)) > 0) ? CurrentUser.new(self, friend) : User.new(self, friend)
end
@@ -43,6 +43,7 @@ def next?
def next(params = {})
next? or return nil
resp = @client.access_token.get(merge_query_string(@url, params.merge({:continuation => @continuation})))
+ resp.code_type == Net::HTTPOK or raise "unable to fetch 'continuation' (#{@continuation}): #{resp.inspect}"
self.class.new(@client, resp.body)
end
alias :more :next
View
@@ -44,23 +44,31 @@ def keep_read
alias :unkeep_unread :keep_read
def add_tag(tag)
+ categories.include?(tag) and raise "duplicate tag \"#{tag}\""
params = {
- 'i' => id, # entry
+ 'i' => id, # item
'a' => tag, # label/state to add
'ac' => 'edit', # action (only known value: edit)
'T' => client.token # token
}
- client.access_token.post('/reader/api/0/edit-tag', params)
+ resp = client.access_token.post('/reader/api/0/edit-tag', params)
+ resp.code_type == Net::HTTPOK or raise "unable to add tag \"#{tag}\" to item \"#{id}\": #{resp.inspect}"
+ categories << tag
+ true
end
def remove_tag(tag)
+ categories.include?(tag) or raise "unknown tag \"#{tag}\""
params = {
- 'i' => id, # entry
+ 'i' => id, # item
'r' => tag, # label/state to remove
'ac' => 'edit', # action (only known value: edit)
'T' => client.token # the write-access token
}
- client.access_token.post('/reader/api/0/edit-tag', params)
+ resp = client.access_token.post('/reader/api/0/edit-tag', params)
+ resp.code_type == Net::HTTPOK or raise "unable to remove tag \"#{tag}\" from item \"#{id}\": #{resp.inspect}"
+ categories.delete(tag)
+ true
end
end
@@ -11,70 +11,70 @@ module Google; module Reader;
class Subscription < UnderscoreMash
def subscribe
params = {
- 's' => self.id, # the subscription id
+ 's' => id, # the subscription id
'ac' => 'subscribe', # the action
'T' => client.token # the write access token
}
- client.access_token.post('/reader/api/0/subscription/edit', params).code_type == Net::HTTPOK and begin
- client.user.subscriptions << self
- true
- end
+ resp = client.access_token.post('/reader/api/0/subscription/edit', params)
+ resp.code_type == Net::HTTPOK or raise "unable to subscribe to \"#{id}\": #{resp.inspect}"
+ client.user.subscriptions << self
+ true
end
def unsubscribe
params = {
- 's' => self.id, # the subscription id
+ 's' => id, # the subscription id
'ac' => 'unsubscribe', # the action
'T' => client.token # the write access token
}
- client.access_token.post('/reader/api/0/subscription/edit', params).code_type == Net::HTTPOK and begin
- client.user.subscriptions.reject!{|s| s.id == self.id}
- true
- end
+ resp = client.access_token.post('/reader/api/0/subscription/edit', params)
+ resp.code_type == Net::HTTPOK or raise "unable to unsubscribe from \"#{id}\": #{resp.inspect}"
+ client.user.subscriptions.reject!{|s| s.id == id}
+ true
end
def set_title(title)
self_title
params = {
- 's' => self.id, # the subscription id
+ 's' => id, # the subscription id
't' => title, # the subscription's title
'ac' => 'edit', # the action
'T' => client.token # the write access token
}
- client.access_token.post('/reader/api/0/subscription/edit', params).code_type == Net::HTTPOK and begin
- self.title = title
- true
- end
+ resp = client.access_token.post('/reader/api/0/subscription/edit', params)
+ resp.code_type == Net::HTTPOK or raise "unable to set title of \"#{id}\" to \"#{title}\": #{resp.inspect}"
+ self.title = title
+ true
end
def add_label(label)
- label = label.is_a?(Google::Reader::Tag) ? label.id : Google::Reader::Tag.build_id(label, self.client.user)
- raise "duplicate label #{label}" if self.labels.collect(&:id).include?(label)
+ label = label.is_a?(Google::Reader::Tag) ? label.id : Google::Reader::Tag.build_id(label, client.user)
+ labels.collect(&:id).include?(label) and raise "duplicate label \"#{label}\""
params = {
- 's' => self.id, # the subscription id
+ 's' => id, # the subscription id
'a' => label, # the label to be added
'ac' => 'edit', # the action
'T' => client.token # the write access token
}
- client.access_token.post('/reader/api/0/subscription/edit', params).code_type == Net::HTTPOK and begin
- labels << self.class.new('id' => label, 'label' => label[(label.rindex('/label/') + 7) .. -1])
- true
- end
+ resp = client.access_token.post('/reader/api/0/subscription/edit', params)
+ resp.code_type == Net::HTTPOK or raise "unable to add label \"#{label}\" to \"#{id}\": #{resp.inspect}"
+ labels << self.class.new('id' => label, 'label' => label[(label.rindex('/label/') + 7) .. -1])
+ true
end
def remove_label(label)
- label = label.is_a?(Google::Reader::Tag) ? label.id : Google::Reader::Tag.build_id(label, self.client.user)
- raise "invalid label #{label}" unless self.labels.collect(&:id).include?(label)
+ label = label.is_a?(Google::Reader::Tag) ? label.id : Google::Reader::Tag.build_id(label, client.user)
+ labels.collect(&:id).include?(label) or raise "unknown label \"#{label}\""
params = {
- 's' => self.id, # the subscription id
+ 's' => id, # the subscription id
'r' => label, # the label to be added
'ac' => 'edit', # the action
'T' => client.token # the write access token
}
- client.access_token.post('/reader/api/0/subscription/edit', params).code_type == Net::HTTPOK and begin
- labels.delete_if{|l| l.id == label}
- true
- end
+ resp = client.access_token.post('/reader/api/0/subscription/edit', params)
+ resp.code_type == Net::HTTPOK or raise "unable to remove label \"#{label}\" from \"#{id}\": #{resp.inspect}"
+ labels.delete_if{|l| l.id == label}
+ true
end
# alias the 'categories' attribute as 'labels', which is term used
View
@@ -24,11 +24,12 @@ def label
def share(val = true)
params = {
- 's' => self.id, # the tag id
+ 's' => id, # the tag id
'pub' => val, # true => public/share, false => not public/unshare
'T' => client.token # the write-access token
}
- client.access_token.post('/reader/api/0/tag/edit', params)
+ resp = client.access_token.post('/reader/api/0/tag/edit', params)
+ resp.code_type == Net::HTTPOK or raise "unable to #{val ? '' : 'un'}share tag \"#{id}\": #{resp.inspect}"
end
def unshare
@@ -37,11 +38,12 @@ def unshare
def disable
params = {
- 's' => self.id, # the tag id
+ 's' => id, # the tag id
'ac' => 'disable-tags', # action (only known value: disable-tags'
'T' => client.token # the write-access token
}
- client.access_token.post('/reader/api/0/disable-tag', params)
+ resp = client.access_token.post('/reader/api/0/disable-tag', params)
+ resp.code_type == Net::HTTPOK or raise "unable to disable/delete tag \"#{id}\": #{resp.inspect}"
end
alias :delete :disable
@@ -90,7 +90,7 @@ def #{prefix}_items!(params = {})
# retrieve the a list of items from a given user, using a given filter
def filtered_items_list(filter, params = {})
resp = @client.access_token.get(ItemList.merge_query_string("/reader/api/0/stream/contents/user/#{user_id}/state/com.google/#{filter}?output=json", params))
- raise "unable to retrieve the list of #{filter} items for user #{user_id}" unless resp.code_type == Net::HTTPOK
+ raise "unable to retrieve the list of #{filter} items for user \"#{user_id}\": #{resp.inspect}" unless resp.code_type == Net::HTTPOK
Google::Reader::ItemList.new(@client, resp.body)
end
@@ -108,7 +108,7 @@ class CurrentUser < User
def subscriptions
@subscriptions ||= begin
resp = @client.access_token.get('/reader/api/0/subscription/list?output=json')
- raise "unable to retrieve the list of subscription for user #{user_id}" unless resp.code_type == Net::HTTPOK
+ raise "unable to retrieve the list of subscription for user \"#{user_id}\": #{resp.inspect}" unless resp.code_type == Net::HTTPOK
JSON.parse(resp.body)['subscriptions'].collect do |hash|
Google::Reader::Subscription.new(hash.merge({:client => @client}))
end
@@ -126,7 +126,7 @@ def subscriptions!
def tags
@tags ||= begin
resp = @client.access_token.get('/reader/api/0/tag/list?output=json')
- raise "unable to retrieve the list of tags for user #{user_id}" unless resp.code_type == Net::HTTPOK
+ raise "unable to retrieve the list of tags for user \"#{user_id}\": #{resp.inspect}" unless resp.code_type == Net::HTTPOK
JSON.parse(resp.body)['tags'].collect do |hash|
Google::Reader::Tag.new(hash.merge({:client => @client}))
end

0 comments on commit f76d0f0

Please sign in to comment.