Skip to content

Commit

Permalink
#1: Treat unknown 2xx status codes as 200
Browse files Browse the repository at this point in the history
As required by HTTP:

> [..] However, a client MUST
> understand the class of any status code, as indicated by the first
> digit, and treat an unrecognized status code as being equivalent to
> the x00 status code of that class, with the exception that a
> recipient MUST NOT cache a response with an unrecognized status code.

See RFC 7231, section 6.

Error status handling will be improved with #4.
  • Loading branch information
mfrister committed Jan 7, 2016
1 parent 68b6fd6 commit af20dcd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/grac/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ def run(request)

grac_response = Response.new(response)
case response.code
when 200, 201
when 200..203, 206..299
# unknown status codes must be treated as the x00 of their class, so 200
if grac_response.json_content?
return postprocessing(grac_response.parsed_json)
end

return grac_response.body
when 204
when 204, 205
return true
when 0
raise Exception::RequestFailed.new(method, request.url, response.return_message)
Expand Down
9 changes: 7 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ This results in a request to `/v1/users?page=2`.

#### Responses

For the **success** status codes `200` and `201`, Grac tries to parse the response as JSON if the response Content-Type contains `application/json`. For other content types, Grac returns the response as String and doesn't attempt to parse it. For a `204` response, the return value is undefined (it's currently `true`, but this might change in the future).
For most **success** status codes (`2xx`, except `204` and `205`), Grac tries to parse the response as JSON if the response Content-Type contains `application/json`. For other content types, Grac returns the response as String and doesn't attempt to parse it. For a `204` or `205` response, the return value is undefined (it's currently `true`, but this might change in the future).

When a **failure** occurs, one of these exceptions will be raised:

* Status 400: `Grac::Exception::BadRequest`
* Status 403: `Grac::Exception::Forbidden`
* Status 404: `Grac::Exception::NotFound`
* Status 409: `Grac::Exception::Conflict`
* All other status codes: `ServiceError` - this includes all unknown status codes, even 2xx and 3xx codes. See [issue #4](https://github.com/Barzahlen/grac/issues/4) for ideas on improving this.
* All other status codes: `ServiceError` - this includes all unknown status codes, even 3xx codes. See [issue #4](https://github.com/Barzahlen/grac/issues/4) for ideas on improving this.
* `InvalidContent` - JSON Parsing failed, server response indicates success
* `RequestFailed` - The request failed, there's no response from the server.
* `ServiceTimeout` - A subclass of `RequestFailed` - the request failed due to a timeout (like waiting for the connection or for the response).
Expand Down Expand Up @@ -187,6 +187,11 @@ Into this Ruby Hash:
Postprocessing recursively runs through all of the data.
This may have significant influence on performance depending on size and depth of the result.

## Limitations

* 3xx status codes (i.e. redirects) are not yet supported.
* Not all error response codes have proper exceptions, see [issue #4](https://github.com/Barzahlen/grac/issues/4).

## Bugs and Contribution
For bugs and feature requests open an issue on Github. For code contributions fork the repo, make your changes and create a pull request.

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/grac/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ def check_options(client, field, value)
end
end

context "response code 201" do
context "unknown response code 299" do
before do
allow(response).to receive(:code).and_return(201)
allow(response).to receive(:code).and_return(299)
end

it "returns a parsed json body" do
Expand Down

0 comments on commit af20dcd

Please sign in to comment.