Permalink
Browse files

fixed error handling for non 400/200 codes

  • Loading branch information...
1 parent 0a5dd5c commit dde58450c306162c1852726dca9e65e6c474ea7f @kevinvangelder kevinvangelder committed Sep 21, 2012
Showing with 14 additions and 18 deletions.
  1. +14 −18 lib/parse_resource/base.rb
@@ -258,7 +258,7 @@ def self.upload(file_instance, filename, options={})
private_resource = RestClient::Resource.new "#{base_uri}/#{filename}", app_id, master_key
private_resource.post(file_instance, options) do |resp, req, res, &block|
return false if resp.code == 400
- return JSON.parse(resp)
+ return JSON.parse(resp) rescue {"code" => 0, "error" => "unknown error"}
end
false
end
@@ -370,20 +370,18 @@ def create
result = self.resource.post(attrs, opts) do |resp, req, res, &block|
case resp.code
- when 400
-
- # https://www.parse.com/docs/ios/api/Classes/PFConstants.html
- error_response = JSON.parse(resp)
- pe = ParseError.new(error_response["code"]).to_array
- self.errors.add(pe[0], pe[1])
- return false
- else
+ when 200
@attributes.merge!(JSON.parse(resp))
@attributes.merge!(@unsaved_attributes)
attributes = HashWithIndifferentAccess.new(attributes)
@unsaved_attributes = {}
create_setters_and_getters!
return true
+ else
+ error_response = JSON.parse(resp) rescue {"code" => 0, "error" => "unknown error"}
+ pe = ParseError.new(error_response["code"]).to_array
+ self.errors.add(pe[0], pe[1])
+ return false
end
end
end
@@ -418,22 +416,20 @@ def update(attributes = {})
opts = {:content_type => "application/json"}
result = self.instance_resource.put(put_attrs, opts) do |resp, req, res, &block|
case resp.code
- when 400
-
- # https://www.parse.com/docs/ios/api/Classes/PFConstants.html
- error_response = JSON.parse(resp)
- pe = ParseError.new(error_response["code"], error_response["error"]).to_array
- self.errors.add(pe[0], pe[1])
-
- return false
- else
+ when 200
@attributes.merge!(JSON.parse(resp))
@attributes.merge!(@unsaved_attributes)
@unsaved_attributes = {}
create_setters_and_getters!
return true
+ else
+ error_response = JSON.parse(resp) rescue {"code" => 0, "error" => "unknown error"}
+ pe = ParseError.new(error_response["code"], error_response["error"]).to_array
+ self.errors.add(pe[0], pe[1])
+
+ return false
end
end
end

0 comments on commit dde5845

Please sign in to comment.