File tree Expand file tree Collapse file tree 5 files changed +40
-11
lines changed Expand file tree Collapse file tree 5 files changed +40
-11
lines changed Original file line number Diff line number Diff line change 11# Changelog
22
33## Unreleased
4+ - [ #404 ] ( https://github.com/JsonApiClient/json_api_client/pull/404 ) - Expose NotFound json errors
45
56## 1.21.0
67- [ #395 ] ( https://github.com/JsonApiClient/json_api_client/pull/395 ) - relaxing faraday dependency to anything less than 2.0
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments