Skip to content

Commit

Permalink
reworked error handling again
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinvangelder committed Sep 24, 2012
1 parent ead1fa5 commit 163c7bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
29 changes: 12 additions & 17 deletions lib/parse_resource/base.rb
Expand Up @@ -369,19 +369,18 @@ def create
attrs = @unsaved_attributes.to_json
result = self.resource.post(attrs, opts) do |resp, req, res, &block|

case resp.code
when 200
if resp.code
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
else
@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
Expand Down Expand Up @@ -415,21 +414,17 @@ 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 200

if resp.code
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
else
@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
Expand Down
1 change: 1 addition & 0 deletions lib/parse_resource/parse_error.rb
Expand Up @@ -22,6 +22,7 @@ def initialize(code, msg="")
when 205
@error = [:user, "with specified email not found"]
else
@error = "Unknown Error"
raise "Parse error #{code}: #{@error}"
end
end
Expand Down

0 comments on commit 163c7bb

Please sign in to comment.