Permalink
Browse files

Merge pull request #42 from clearsightstudio/master

Fixed error handling
  • Loading branch information...
2 parents e6be1e4 + 8e3c4c6 commit bb2222f16a50307abeb2d4762a213b7f0839496e @adelevie committed Sep 24, 2012
Showing with 38 additions and 44 deletions.
  1. +1 −0 README.md
  2. +18 −26 lib/parse_resource/base.rb
  3. +19 −18 lib/parse_resource/parse_error.rb
View
@@ -93,6 +93,7 @@ p.valid? #=> true
# setting more attributes, then saving
p.author = "Alan deLevie"
p.body = "Ipso Lorem"
+p.date = Time.now
p.save #=> true
# checking the id generated by Parse's servers
View
@@ -63,7 +63,7 @@ def self.field(fname, val=nil)
unless self.respond_to? "#{fname}="
class_eval do
define_method("#{fname}=") do |val|
- set_attribute("#{fname}", "#{val}")
+ set_attribute("#{fname}", val)
val
end
@@ -95,7 +95,7 @@ def to_pointer
def create_setters!(k,v)
unless self.respond_to? "#{k}="
self.class.send(:define_method, "#{k}=") do |val|
- set_attribute("#{k}", "#{val}")
+ set_attribute("#{k}", val)
val
end
@@ -205,10 +205,12 @@ def self.upload(file_instance, filename, options={})
options[:content_type] ||= 'image/jpg' # TODO: Guess mime type here.
file_instance = File.new(file_instance, 'rb') if file_instance.is_a? String
+ filename = filename.parameterize
+
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
@@ -280,22 +282,18 @@ def create
opts = {:content_type => "application/json"}
attrs = @unsaved_attributes.to_json
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
+ if resp.code.to_s == "200" || resp.code.to_s == "201"
@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)
+ pe = ParseError.new(resp.code.to_s).to_array
+ self.errors.add(pe[0], pe[1])
+ return false
end
end
end
@@ -329,23 +327,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 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
-
+ if resp.code.to_s == "200" || resp.code.to_s == "201"
@attributes.merge!(JSON.parse(resp))
@attributes.merge!(@unsaved_attributes)
@unsaved_attributes = {}
create_setters_and_getters!
-
return true
+ else
+ error_response = JSON.parse(resp)
+ pe = ParseError.new(resp.code.to_s, error_response["error"]).to_array
+ self.errors.add(pe[0], pe[1])
+ return false
end
end
end
@@ -413,7 +405,7 @@ def set_attribute(k, v)
else
v = v.to_pointer if v.respond_to?(:to_pointer)
end
- @unsaved_attributes[k.to_s] = v unless v == @attributes[k.to_s] || @unsaved_attributes[k.to_s]
+ @unsaved_attributes[k.to_s] = v unless v == @attributes[k.to_s] # || @unsaved_attributes[k.to_s]
@attributes[k.to_s] = v
v
end
@@ -3,25 +3,26 @@ class ParseError
def initialize(code, msg="")
@msg = msg
case code
- when 202
- @error = [:username, "must be unique"]
- when 111
- @error = [:base, "field set to incorrect type. Error code #{code}. #{msg}"]
- when 125
- @error = [:email, "must be valid"]
- when 122
- @error = [:file_name, "contains only a-zA-Z0-9_. characters and is between 1 and 36 characters."]
- when 204
- @error = [:email, "must not be missing"]
- when 203
- @error = [:email, "has already been taken"]
- when 200
- @error = [:username, "is missing or empty"]
- when 201
- @error = [:password, "is missing or empty"]
- when 205
- @error = [:user, "with specified email not found"]
+ when "400"
+ @error = "Bad Request: The request cannot be fulfilled due to bad syntax."
+ when "401"
+ @error = "Unauthorized: Check your App ID & Master Key."
+ when "403"
+ @error = "Forbidden: You do not have permission to access or modify this."
+ when "408"
+ @error = "Request Timeout: The request was not completed within the time the server was prepared to wait."
+ when "415"
+ @error = "Unsupported Media Type"
+ when "500"
+ @error = "Internal Server Error"
+ when "502"
+ @error = "Bad Gateway"
+ when "503"
+ @error = "Service Unavailable"
+ when "508"
+ @error = "Loop Detected"
else
+ @error = "Unknown Error"
raise "Parse error #{code}: #{@error}"
end
end

0 comments on commit bb2222f

Please sign in to comment.