Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make ParseError a little more useful & fix to_array #62

Merged
merged 1 commit into from

3 participants

@qixotic

Fixes some ParseError breakage in 760bf33 (pull req 42)

@qixotic qixotic referenced this pull request
Closed

fix error message #60

@adelevie adelevie merged commit c40fe98 into adelevie:master

1 check passed

Details default The Travis build passed
@adelevie
Owner

Thanks!

@jamonholmgren jamonholmgren commented on the diff
lib/parse_resource/parse_error.rb
((12 lines not shown))
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."
@jamonholmgren Collaborator

You could do:

# remove if
@msg ||= "Bad Request: The request cannot be fulfilled due to bad syntax."
@jamonholmgren Collaborator

Your msg="" should change to msg=nil in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 29, 2013
  1. @qixotic
This page is out of date. Refresh to see the latest.
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,7 +335,7 @@ 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 = {}
@@ -343,7 +343,7 @@ def update(attributes = {})
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."
@jamonholmgren Collaborator

You could do:

# remove if
@msg ||= "Bad Request: The request cannot be fulfilled due to bad syntax."
@jamonholmgren Collaborator

Your msg="" should change to msg=nil in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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
Something went wrong with that request. Please try again.