Skip to content

Commit

Permalink
Merge pull request #140 from theirishpenguin/penguin/issue-135-handle…
Browse files Browse the repository at this point in the history
…-not-authorized

Handle 401 Not Authorized Response
  • Loading branch information
chingor13 committed Nov 16, 2015
2 parents ceae78b + 235922e commit 6bbdc61
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/json_api_client/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ClientError < ApiError
class AccessDenied < ClientError
end

class NotAuthorized < ClientError
end

class ConnectionError < ApiError
end

Expand Down
2 changes: 2 additions & 0 deletions lib/json_api_client/middleware/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def call(environment)
def handle_status(code, env)
case code
when 200..399
when 401
raise Errors::NotAuthorized, env
when 403
raise Errors::AccessDenied, env
when 404
Expand Down
9 changes: 9 additions & 0 deletions test/unit/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ def test_access_denied
end
end

def test_access_denied
stub_request(:get, "http://example.com/users")
.to_return(headers: {content_type: "text/plain"}, status: 401, body: "not authorized")

assert_raises JsonApiClient::Errors::NotAuthorized do
User.all
end
end

def test_errors_are_rescuable_by_default_rescue
begin
raise JsonApiClient::Errors::ApiError, "Something bad happened"
Expand Down

0 comments on commit 6bbdc61

Please sign in to comment.