Skip to content

Commit

Permalink
reworked error system to work with http error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinvangelder committed Sep 24, 2012
1 parent 163c7bb commit 760bf33
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
25 changes: 12 additions & 13 deletions lib/parse_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,19 +368,18 @@ def create
opts = {:content_type => "application/json"}
attrs = @unsaved_attributes.to_json
result = self.resource.post(attrs, opts) do |resp, req, res, &block|

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
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
Expand Down Expand Up @@ -414,17 +413,17 @@ def update(attributes = {})

opts = {:content_type => "application/json"}
result = self.instance_resource.put(put_attrs, opts) do |resp, req, res, &block|
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
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
Expand Down
36 changes: 18 additions & 18 deletions lib/parse_resource/parse_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ 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}"
Expand Down

0 comments on commit 760bf33

Please sign in to comment.