Skip to content

Commit a07145a

Browse files
authored
Merge pull request #404 from stokarenko/expose-not-found-json-errors
Expose NotFound json errors
2 parents f7fa7e8 + efc973e commit a07145a

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## Unreleased
4+
- [#404](https://github.com/JsonApiClient/json_api_client/pull/404) - Expose NotFound json errors
45
- [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
56

67
## 1.21.0

lib/json_api_client/errors.rb

+11-4
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,18 @@ class NotAuthorized < ClientError
4646

4747
class NotFound < ClientError
4848
attr_reader :uri
49-
def initialize(uri)
50-
@uri = uri
49+
def initialize(env_or_uri, msg = nil)
50+
env = nil
51+
52+
if env_or_uri.kind_of?(Faraday::Env)
53+
env = env_or_uri
54+
@uri = env[:url]
55+
else
56+
@uri = env_or_uri
57+
end
5158

52-
msg = "Resource not found: #{uri.to_s}"
53-
super nil, msg
59+
msg ||= "Resource not found: #{uri.to_s}"
60+
super env, msg
5461
end
5562
end
5663

lib/json_api_client/middleware/status.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def handle_status(code, env)
3737
when 403
3838
raise Errors::AccessDenied, env
3939
when 404
40-
raise Errors::NotFound, env[:url]
40+
raise Errors::NotFound, env
4141
when 408
4242
raise Errors::RequestTimeout, env
4343
when 409

lib/json_api_client/query/builder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def to_a
9292

9393
def find(args = {})
9494
if klass.raise_on_blank_find_param && args.blank?
95-
raise Errors::NotFound, 'blank .find param'
95+
raise Errors::NotFound, nil, 'blank .find param'
9696
end
9797

9898
case args

test/unit/errors_test.rb

+26-5
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,34 @@ def test_500_errors_with_with_json_api_response
6060
assert_equal '503 Service Unavailable (Timeout error)', exception.message
6161
end
6262

63-
def test_not_found
63+
def test_not_found_with_json_api_response
6464
stub_request(:get, "http://example.com/users")
65-
.to_return(status: 404, body: "something irrelevant")
65+
.to_return(
66+
headers: {content_type: "application/vnd.api+json"},
67+
status: 404,
68+
body: {errors: [{title: "Not found"}]}.to_json
69+
)
70+
71+
exception = assert_raises(JsonApiClient::Errors::NotFound) { User.all }
72+
assert_equal(
73+
"Resource not found: http://example.com/users (Not found)",
74+
exception.message
75+
)
76+
end
6677

67-
assert_raises JsonApiClient::Errors::NotFound do
68-
User.all
69-
end
78+
def test_not_found_errors_with_plain_text_response
79+
stub_request(:get, "http://example.com/users")
80+
.to_return(
81+
headers: {content_type: "text/plain"},
82+
status: 404,
83+
body: "resource not found"
84+
)
85+
86+
exception = assert_raises(JsonApiClient::Errors::NotFound) { User.all }
87+
assert_equal(
88+
"Resource not found: http://example.com/users",
89+
exception.message
90+
)
7091
end
7192

7293
def test_conflict

0 commit comments

Comments
 (0)