From bf7354de7f3ccd975bb82330ac40a25e33d3fd41 Mon Sep 17 00:00:00 2001 From: x4d3 Date: Tue, 10 Oct 2017 16:56:20 +0100 Subject: [PATCH 1/2] Add error details to resource when destroy fails --- lib/json_api_client/resource.rb | 25 +++++++++++++++---------- test/unit/destroying_test.rb | 19 ++++++++++++------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index d00fdd8e..c1d32306 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -410,13 +410,7 @@ def save end if last_result_set.has_errors? - last_result_set.errors.each do |error| - if error.source_parameter - errors.add(self.class.key_formatter.unformat(error.source_parameter), error.title || error.detail) - else - errors.add(:base, error.title || error.detail) - end - end + fill_errors(last_result_set) false else self.errors.clear if self.errors @@ -436,11 +430,12 @@ def save # @return [Boolean] Whether or not the destroy succeeded def destroy self.last_result_set = self.class.requestor.destroy(self) - if !last_result_set.has_errors? + if last_result_set.has_errors? + fill_errors(last_result_set) + false + else self.attributes.clear true - else - false end end @@ -504,5 +499,15 @@ def attributes_for_serialization def relationships_for_serialization relationships.as_json_api end + + def fill_errors(last_result_set) + last_result_set.errors.each do |error| + if error.source_parameter + errors.add(self.class.key_formatter.unformat(error.source_parameter), error.title || error.detail) + else + errors.add(:base, error.title || error.detail) + end + end + end end end diff --git a/test/unit/destroying_test.rb b/test/unit/destroying_test.rb index fcb7c712..5b2896aa 100644 --- a/test/unit/destroying_test.rb +++ b/test/unit/destroying_test.rb @@ -43,18 +43,23 @@ def test_destroy_failure assert(user.persisted?) stub_request(:delete, "http://example.com/users/1") - .to_return(headers: {content_type: "application/json"}, body: { + .to_return(headers: { content_type: "application/json" }, body: { data: [], - errors: [{ - status: 400, - errors: [ - {title: "Some failure message"} - ] - }] + errors: [ + { + status: 400, + title: "Some failure message", + source: { + pointer: "/data/attributes/email_address" + } + } + ] }.to_json) assert_equal(false, user.destroy, "failed deletion should return falsy value") assert_equal(true, user.persisted?, "user should still be persisted because destroy failed") + assert(user.errors.present?) + assert_equal("Some failure message", user.errors.messages[:email_address].first, "user should contain the errors describing the failure") end def test_callbacks From fb54d7face94cfbb227d9556591633ce07978fcc Mon Sep 17 00:00:00 2001 From: x4d3 Date: Tue, 10 Oct 2017 17:08:15 +0100 Subject: [PATCH 2/2] Simplify code by using last_result_set field --- lib/json_api_client/resource.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index c1d32306..4c3d69e6 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -410,7 +410,7 @@ def save end if last_result_set.has_errors? - fill_errors(last_result_set) + fill_errors false else self.errors.clear if self.errors @@ -431,7 +431,7 @@ def save def destroy self.last_result_set = self.class.requestor.destroy(self) if last_result_set.has_errors? - fill_errors(last_result_set) + fill_errors false else self.attributes.clear @@ -500,7 +500,7 @@ def relationships_for_serialization relationships.as_json_api end - def fill_errors(last_result_set) + def fill_errors last_result_set.errors.each do |error| if error.source_parameter errors.add(self.class.key_formatter.unformat(error.source_parameter), error.title || error.detail)