Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Add extra error classes to handle server errors. #1

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions lib/json_api_client/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ class AccessDenied < ClientError
class NotAuthorized < ClientError
end

class NotFound < ClientError
attr_reader :uri
def initialize(uri)
@uri = uri

msg = "Couldn't find resource at: #{uri.to_s}"
super nil, msg
end
end

class RequestTimeout < ClientError
end

class Conflict < ClientError
def initialize(env, msg = 'Resource already exists')
super env, msg
end
end

class TooManyRequests < ClientError
end

class ConnectionError < ApiError
end

Expand All @@ -59,23 +81,16 @@ def initialize(env, msg = nil)
end
end

class Conflict < ServerError
def initialize(env, msg = 'Resource already exists')
super env, msg
end
class InternalServerError < ServerError
end

class NotFound < ServerError
attr_reader :uri
def initialize(uri)
@uri = uri
class BadGateway < ServerError
end

msg = "Couldn't find resource at: #{uri.to_s}"
super nil, msg
end
class ServiceUnavailable < ServerError
end

class InternalServerError < ServerError
class GatewayTimeout < ServerError
end

class UnexpectedStatus < ServerError
Expand Down
10 changes: 10 additions & 0 deletions lib/json_api_client/middleware/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,24 @@ def handle_status(code, env)
raise Errors::AccessDenied, env
when 404
raise Errors::NotFound, env[:url]
when 408
raise Errors::RequestTimeout, env
when 409
raise Errors::Conflict, env
when 422
# Allow to proceed as resource errors will be populated
when 429
raise Errors::TooManyRequests, env
when 400..499
raise Errors::ClientError, env
when 500
raise Errors::InternalServerError, env
when 502
raise Errors::BadGateway, env
when 503
raise Errors::ServiceUnavailable, env
when 504
raise Errors::GatewayTimeout, env
when 501..599
raise Errors::ServerError, env
else
Expand Down
45 changes: 45 additions & 0 deletions test/unit/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,51 @@ def test_not_authorized
end
end

def test_request_timeout
stub_request(:get, "http://example.com/users")
.to_return(headers: {content_type: "text/plain"}, status: 408, body: "request timeout")

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

def test_too_many_requests
stub_request(:get, "http://example.com/users")
.to_return(headers: {content_type: "text/plain"}, status: 429, body: "too many requests")

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

def test_bad_gateway
stub_request(:get, "http://example.com/users")
.to_return(headers: {content_type: "text/plain"}, status: 502, body: "bad gateway")

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

def test_service_unavailable
stub_request(:get, "http://example.com/users")
.to_return(headers: {content_type: "text/plain"}, status: 503, body: "service unavailable")

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

def test_gateway_timeout
stub_request(:get, "http://example.com/users")
.to_return(headers: {content_type: "text/plain"}, status: 504, body: "gateway timeout")

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

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