Skip to content

Expose NotFound json errors #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- [#404](https://github.com/JsonApiClient/json_api_client/pull/404) - Expose NotFound json errors
- [378](https://github.com/JsonApiClient/json_api_client/pull/378) - Add the ability to create a subclass of JsonApiClient::Resource to have a modified id method

## 1.21.0
Expand Down
15 changes: 11 additions & 4 deletions lib/json_api_client/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ class NotAuthorized < ClientError

class NotFound < ClientError
attr_reader :uri
def initialize(uri)
@uri = uri
def initialize(env_or_uri, msg = nil)
env = nil

if env_or_uri.kind_of?(Faraday::Env)
env = env_or_uri
@uri = env[:url]
else
@uri = env_or_uri
end

msg = "Resource not found: #{uri.to_s}"
super nil, msg
msg ||= "Resource not found: #{uri.to_s}"
super env, msg
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/json_api_client/middleware/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def handle_status(code, env)
when 403
raise Errors::AccessDenied, env
when 404
raise Errors::NotFound, env[:url]
raise Errors::NotFound, env
when 408
raise Errors::RequestTimeout, env
when 409
Expand Down
2 changes: 1 addition & 1 deletion lib/json_api_client/query/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def to_a

def find(args = {})
if klass.raise_on_blank_find_param && args.blank?
raise Errors::NotFound, 'blank .find param'
raise Errors::NotFound, nil, 'blank .find param'
end

case args
Expand Down
31 changes: 26 additions & 5 deletions test/unit/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,34 @@ def test_500_errors_with_with_json_api_response
assert_equal '503 Service Unavailable (Timeout error)', exception.message
end

def test_not_found
def test_not_found_with_json_api_response
stub_request(:get, "http://example.com/users")
.to_return(status: 404, body: "something irrelevant")
.to_return(
headers: {content_type: "application/vnd.api+json"},
status: 404,
body: {errors: [{title: "Not found"}]}.to_json
)

exception = assert_raises(JsonApiClient::Errors::NotFound) { User.all }
assert_equal(
"Resource not found: http://example.com/users (Not found)",
exception.message
)
end

assert_raises JsonApiClient::Errors::NotFound do
User.all
end
def test_not_found_errors_with_plain_text_response
stub_request(:get, "http://example.com/users")
.to_return(
headers: {content_type: "text/plain"},
status: 404,
body: "resource not found"
)

exception = assert_raises(JsonApiClient::Errors::NotFound) { User.all }
assert_equal(
"Resource not found: http://example.com/users",
exception.message
)
end

def test_conflict
Expand Down