Skip to content

Commit

Permalink
Clean up some missed FailedRequest refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
seancribbs committed Mar 28, 2011
1 parent 7253e3e commit 0eb3037
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion riak-client/lib/riak/bucket.rb
Expand Up @@ -113,7 +113,7 @@ def get_or_new(key, options={})
begin
get(key, options)
rescue Riak::FailedRequest => fr
if fr.code.to_i == 404
if fr.not_found?
new(key)
else
raise fr
Expand Down
2 changes: 1 addition & 1 deletion riak-client/lib/riak/cache_store.rb
Expand Up @@ -77,7 +77,7 @@ def read_entry(key, options={})
begin
bucket.get(key).data
rescue Riak::FailedRequest => fr
raise fr unless fr.code.to_i == 404
raise fr unless fr.not_found?
nil
end
end
Expand Down
4 changes: 2 additions & 2 deletions riak-client/lib/riak/client/beefcake_protobuffs_backend.rb
Expand Up @@ -58,7 +58,7 @@ def reload_object(robject, r=nil)
def store_object(robject, returnbody=false, w=nil, dw=nil)
if robject.prevent_stale_writes
other = fetch_object(robject.bucket, robject.key)
raise Riak::FailedRequest(:pb, :not_stale, :stale, "stale write prevented") unless other.vclock == robject.vclock
raise Riak::ProtobuffsFailedRequest(:stale_object, t("stale_write_prevented")) unless other.vclock == robject.vclock
end
req = dump_object(robject)
req.w = normalize_quorum_value(w) if w
Expand Down Expand Up @@ -142,7 +142,7 @@ def decode_response(*args)
case MESSAGE_CODES[msgcode]
when :ErrorResp
res = RpbErrorResp.decode(message)
raise Riak::ProtobufsFailedRequest.new(res.errcode, res.errmsg)
raise Riak::ProtobuffsFailedRequest.new(res.errcode, res.errmsg)
when :GetClientIdResp
res = RpbGetClientIdResp.decode(message)
res.client_id
Expand Down
15 changes: 15 additions & 0 deletions riak-client/lib/riak/failed_request.rb
Expand Up @@ -43,6 +43,10 @@ def initialize(method, expected_code, received_code, headers, body)
super t("http_failed_request", :expected => @expected.inspect, :code => @code, :body => @body)
end

def is_json?
headers['content-type'].include?('application/json')
end

# @return [true,false] whether the error represents a "not found" response
def not_found?
@code.to_i == 404
Expand All @@ -59,10 +63,21 @@ def server_error?
class ProtobuffsFailedRequest < FailedRequest
def initialize(code, message)
super t('protobuffs_failed_request', :code => code, :body => message)
@original_message = message
@not_found = code == :not_found
@server_error = code == :server_error
end

# @return [true, false] whether the error response is in JSON
def is_json?
begin
JSON.parse(original_message)
true
rescue
false
end
end

# @return [true,false] whether the error represents a "not found" response
def not_found?
@not_found
Expand Down
1 change: 1 addition & 0 deletions riak-client/lib/riak/locale/en.yml
Expand Up @@ -41,6 +41,7 @@ en:
resource_path_short: "Resource path too short"
search_docs_require_id: "Search index documents must include the 'id' field."
search_remove_requires_id_or_query: "Search index documents to be removed must have 'id' or 'query' keys."
stale_write_prevented: "Stale write prevented by client."
stored_function_invalid: "function must have :bucket and :key when a hash"
string_type: "invalid_argument %{string} is not a String"
too_few_arguments: "too few arguments: %{params}"
Expand Down
2 changes: 1 addition & 1 deletion riak-client/lib/riak/map_reduce.rb
Expand Up @@ -188,7 +188,7 @@ def run(&block)
raise MapReduceError.new(t("empty_map_reduce_query")) if @query.empty?
@client.backend.mapred(self, &block)
rescue FailedRequest => fr
if fr.code == 500 && fr.headers['content-type'].include?("application/json")
if fr.server_error? && fr.is_json?
raise MapReduceError.new(fr.body)
else
raise fr
Expand Down
2 changes: 1 addition & 1 deletion riak-client/spec/riak/map_reduce_spec.rb
Expand Up @@ -255,7 +255,7 @@
begin
@mr.run
rescue Riak::MapReduceError => mre
mre.message.should == '{"error":"syntax error"}'
mre.message.should include('{"error":"syntax error"}')
else
fail "No exception raised!"
end
Expand Down
2 changes: 1 addition & 1 deletion riak-sessions/lib/ripple/session_store.rb
Expand Up @@ -47,7 +47,7 @@ def get_session(env, sid)
begin
session = @bucket.get(sid).data
rescue Riak::FailedRequest => fr
raise fr unless fr.code.to_i == 404
raise fr unless fr.not_found?
end
[sid, session]
end
Expand Down
3 changes: 1 addition & 2 deletions ripple/lib/ripple/document/finders.rb
Expand Up @@ -112,8 +112,7 @@ def all
def find_one(key)
instantiate(bucket.get(key, quorums.slice(:r)))
rescue Riak::FailedRequest => fr
return nil if fr.code.to_i == 404
raise fr
raise fr unless fr.not_found?
end

def instantiate(robject)
Expand Down

0 comments on commit 0eb3037

Please sign in to comment.