Skip to content

Commit

Permalink
Merge pull request #62 from qixotic/error
Browse files Browse the repository at this point in the history
Make ParseError a little more useful & fix to_array
  • Loading branch information
adelevie committed Jan 30, 2013
2 parents e844914 + 220b3ae commit c40fe98
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
8 changes: 4 additions & 4 deletions lib/parse_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ 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.to_s == "200" || resp.code.to_s == "201"
if resp.code == 200 || resp.code == 201
@attributes.merge!(JSON.parse(resp))
@attributes.merge!(@unsaved_attributes)
attributes = HashWithIndifferentAccess.new(attributes)
Expand All @@ -299,7 +299,7 @@ def create
return true
else
error_response = JSON.parse(resp)
pe = ParseError.new(resp.code.to_s).to_array
pe = ParseError.new(resp.code.to_s, error_response).to_array
self.errors.add(pe[0], pe[1])
return false
end
Expand Down Expand Up @@ -335,15 +335,15 @@ def update(attributes = {})

opts = {:content_type => "application/json"}
result = self.instance_resource.put(put_attrs, opts) do |resp, req, res, &block|
if resp.code.to_s == "200" || resp.code.to_s == "201"
if resp.code == 200 || resp.code == 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
pe = ParseError.new(resp.code.to_s, error_response).to_array
self.errors.add(pe[0], pe[1])
return false
end
Expand Down
35 changes: 21 additions & 14 deletions lib/parse_resource/parse_error.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
class ParseError
# ParseError actually represents both HTTP & parse.com error codes. If the
# HTTP response is 400, one can inspect the first element of the error
# converted to_array for the HTTP error code and the 2nd element for the
# parse error response.

# @param [String] an error code, e.g. "400"
# @param [Object] an optional error mesg/object.
def initialize(code, msg="")
@msg = msg
@code = code
case code
when "400"
@error = "Bad Request: The request cannot be fulfilled due to bad syntax."
if msg.empty?
@msg = "Bad Request: The request cannot be fulfilled due to bad syntax."
end
# otherwise we should have supplied the Parse msg JSON response.
when "401"
@error = "Unauthorized: Check your App ID & Master Key."
@msg = "Unauthorized: Check your App ID & Master Key."
when "403"
@error = "Forbidden: You do not have permission to access or modify this."
@msg = "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"
@msg = "Unsupported Media Type"
when "500"
@error = "Internal Server Error"
@msg = "Internal Server Error"
when "502"
@error = "Bad Gateway"
@msg = "Bad Gateway"
when "503"
@error = "Service Unavailable"
@msg = "Service Unavailable"
when "508"
@error = "Loop Detected"
@msg = "Loop Detected"
else
@error = "Unknown Error"
raise "Parse error #{code}: #{@error}"
@msg = "Unknown Error"
raise "Parse msg #{code}: #{@error}"
end
end

def to_array
@error[1] = @error[1] + " " + @msg
@error
return [@code, @msg]
end

end
5 changes: 3 additions & 2 deletions test/test_parse_installation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def test_creation_validation_check
i = Installation.create(:deviceToken => "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
:channels => [""])
assert_equal false, i.errors.empty?
# TODO: actually check for the parse error code 135, once ParseError is
# fixed to actually represent parse error codes.
assert_equal false, i.errors["400"].empty?
parse_error_response = i.errors["400"][0]
assert_equal 135, parse_error_response["code"] # deviceType must be specified in this operation
end

end
4 changes: 3 additions & 1 deletion test/test_parse_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def test_username_should_be_unique
u2 = User.new(:username => "alan", :password => "56789")
u2.save
assert_equal u2.errors.count, 1
parse_error_response = u2.errors["400"][0]
assert_equal 202, parse_error_response["code"] # username alan already taken
assert_equal nil, u2.id
end
end
Expand All @@ -52,4 +54,4 @@ def test_authenticate
end
end

end
end

0 comments on commit c40fe98

Please sign in to comment.