Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Resuce from ParseError (#11554)

Aleksandar Petrushevaa87f6304ec7
diff --git a/api/app/controllers/spree/api/v2/base_controller.rb b/api/app/controllers/spree/api/v2/base_controller.rb
index b3196b29ec..21cafcae51 100644
--- a/api/app/controllers/spree/api/v2/base_controller.rb
+++ b/api/app/controllers/spree/api/v2/base_controller.rb
@@ -17,6 +17,7 @@ module Spree
rescue_from JSONAPI::Serializer::UnsupportedIncludeError, with: :error_during_processing
end
rescue_from ArgumentError, with: :error_during_processing
+ rescue_from ActionDispatch::Http::Parameters::ParseError, with: :error_during_processing
def content_type
Spree::Api::Config[:api_v2_content_type]
diff --git a/api/spec/controllers/spree/api/v2/base_controller_spec.rb b/api/spec/controllers/spree/api/v2/base_controller_spec.rb
index d1ee52e468..94c1486976 100644
--- a/api/spec/controllers/spree/api/v2/base_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v2/base_controller_spec.rb
@@ -103,21 +103,36 @@ describe Spree::Api::V2::BaseController, type: :controller do
end
let!(:user) { create :user }
- let(:exception) { ArgumentError.new('foo') }
let(:result_class) { Struct.new(:value) }
let(:result) { result_class.new({message: 'foo'}) }
- it 'handles ArgumentError exceptions' do
- expect(subject).to receive(:index).and_raise(exception)
- expect(subject).to receive(:spree_current_user).and_return(user)
- expect_next_instance_of(::Spree::Api::ErrorHandler) do |instance|
- expect(instance).to receive(:call).with(
- exception: exception,
- opts: { user: user }
- ).and_return(result)
- end
- get :index, params: { token: 'exception-message' }
- expect(json_response).to eql('error' => 'foo')
+ shared_examples 'rescues from error' do
+ it do
+ expect(subject).to receive(:index).and_raise(exception)
+ expect(subject).to receive(:spree_current_user).and_return(user)
+ expect_next_instance_of(::Spree::Api::ErrorHandler) do |instance|
+ expect(instance).to receive(:call).with(
+ exception: exception,
+ opts: { user: user }
+ ).and_return(result)
+ end
+ get :index, params: { token: 'exception-message' }
+ expect(json_response).to eql('error' => 'foo')
+ end
+ end
+
+ context 'ArgumentError' do
+ let(:exception) { ArgumentError.new('foo') }
+
+ it_behaves_like 'rescues from error'
+ end
+
+ context 'ActionDispatch::Http::Parameters::ParseError' do
+ let(:exception) { ActionDispatch::Http::Parameters::ParseError.new }
+
+ before { expect($!).to receive(:message).and_return('foo') }
+
+ it_behaves_like 'rescues from error'
end
end