File tree 5 files changed +40
-11
lines changed
5 files changed +40
-11
lines changed Original file line number Diff line number Diff line change 1
1
# Changelog
2
2
3
3
## Unreleased
4
+ - [ #404 ] ( https://github.com/JsonApiClient/json_api_client/pull/404 ) - Expose NotFound json errors
4
5
- [ 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
5
6
6
7
## 1.21.0
Original file line number Diff line number Diff line change @@ -46,11 +46,18 @@ class NotAuthorized < ClientError
46
46
47
47
class NotFound < ClientError
48
48
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
51
58
52
- msg = "Resource not found: #{ uri . to_s } "
53
- super nil , msg
59
+ msg || = "Resource not found: #{ uri . to_s } "
60
+ super env , msg
54
61
end
55
62
end
56
63
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ def handle_status(code, env)
37
37
when 403
38
38
raise Errors ::AccessDenied , env
39
39
when 404
40
- raise Errors ::NotFound , env [ :url ]
40
+ raise Errors ::NotFound , env
41
41
when 408
42
42
raise Errors ::RequestTimeout , env
43
43
when 409
Original file line number Diff line number Diff line change @@ -92,7 +92,7 @@ def to_a
92
92
93
93
def find ( args = { } )
94
94
if klass . raise_on_blank_find_param && args . blank?
95
- raise Errors ::NotFound , 'blank .find param'
95
+ raise Errors ::NotFound , nil , 'blank .find param'
96
96
end
97
97
98
98
case args
Original file line number Diff line number Diff line change @@ -60,13 +60,34 @@ def test_500_errors_with_with_json_api_response
60
60
assert_equal '503 Service Unavailable (Timeout error)' , exception . message
61
61
end
62
62
63
- def test_not_found
63
+ def test_not_found_with_json_api_response
64
64
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
66
77
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
+ )
70
91
end
71
92
72
93
def test_conflict
You can’t perform that action at this time.
0 commit comments