Skip to content
Browse files

Merge pull request #62 from qixotic/error

Make ParseError a little more useful & fix to_array
  • Loading branch information...
2 parents e844914 + 220b3ae commit c40fe98f700c8790c6969474d4b15e68b1522c8d @adelevie committed Jan 29, 2013
Showing with 31 additions and 21 deletions.
  1. +4 −4 lib/parse_resource/base.rb
  2. +21 −14 lib/parse_resource/parse_error.rb
  3. +3 −2 test/test_parse_installation.rb
  4. +3 −1 test/test_parse_user.rb
View
8 lib/parse_resource/base.rb
@@ -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)
@@ -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
@@ -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
View
35 lib/parse_resource/parse_error.rb
@@ -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
View
5 test/test_parse_installation.rb
@@ -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
View
4 test/test_parse_user.rb
@@ -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
@@ -52,4 +54,4 @@ def test_authenticate
end
end
-end
+end

0 comments on commit c40fe98

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