Skip to content
Browse files

reworked error handling again

  • Loading branch information...
1 parent ead1fa5 commit 163c7bb135206aef73fde234b89f046ba8015707 @kevinvangelder kevinvangelder committed Sep 24, 2012
Showing with 13 additions and 17 deletions.
  1. +12 −17 lib/parse_resource/base.rb
  2. +1 −0 lib/parse_resource/parse_error.rb
View
29 lib/parse_resource/base.rb
@@ -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
@@ -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
View
1 lib/parse_resource/parse_error.rb
@@ -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

0 comments on commit 163c7bb

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