Skip to content
Browse files

Clean up some missed FailedRequest refactorings.

  • Loading branch information...
1 parent 7253e3e commit 0eb30371325cd9a6b8e4943ae866ee26c057c1de @seancribbs seancribbs committed
View
2 riak-client/lib/riak/bucket.rb
@@ -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
View
2 riak-client/lib/riak/cache_store.rb
@@ -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
View
4 riak-client/lib/riak/client/beefcake_protobuffs_backend.rb
@@ -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
@@ -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
View
15 riak-client/lib/riak/failed_request.rb
@@ -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
@@ -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
View
1 riak-client/lib/riak/locale/en.yml
@@ -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}"
View
2 riak-client/lib/riak/map_reduce.rb
@@ -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
View
2 riak-client/spec/riak/map_reduce_spec.rb
@@ -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
View
2 riak-sessions/lib/ripple/session_store.rb
@@ -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
View
3 ripple/lib/ripple/document/finders.rb
@@ -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)

0 comments on commit 0eb3037

Please sign in to comment.
Something went wrong with that request. Please try again.