diff --git a/app/controllers/admin/dfc_product_imports_controller.rb b/app/controllers/admin/dfc_product_imports_controller.rb index c16031bf20..c166179ba3 100644 --- a/app/controllers/admin/dfc_product_imports_controller.rb +++ b/app/controllers/admin/dfc_product_imports_controller.rb @@ -38,6 +38,11 @@ module Admin end @count = imported.compact.count + rescue Faraday::Error, + Addressable::URI::InvalidURIError, + ActionController::ParameterMissing => e + flash[:error] = e.message + redirect_to admin_product_import_path end private diff --git a/spec/system/admin/dfc_product_import_spec.rb b/spec/system/admin/dfc_product_import_spec.rb index 17e61b9d62..c81197a768 100644 --- a/spec/system/admin/dfc_product_import_spec.rb +++ b/spec/system/admin/dfc_product_import_spec.rb @@ -76,4 +76,31 @@ RSpec.describe "DFC Product Import" do expect(product.variants[0].semantic_links).to be_present expect(product.image).to be_present end + + it "fails gracefully" do + user.oidc_account.update!( + uid: "anonymous@example.net", + updated_at: 1.minute.ago, + ) + url = "https://example.net/unauthorized" + stub_request(:get, url).to_return(status: [401, "Unauthorized"]) + + visit admin_product_import_path + select enterprise.name, from: "Enterprise" + fill_in "catalog_url", with: url + + expect { click_button "Import" }.not_to change { Spree::Variant.count } + + expect(page).to have_content "the server responded with status 401" + + select enterprise.name, from: "Enterprise" + fill_in "catalog_url", with: "badurl" + click_button "Import" + expect(page).to have_content "Absolute URI missing hierarchical segment: 'http://:80'" + + select enterprise.name, from: "Enterprise" + fill_in "catalog_url", with: "" + click_button "Import" + expect(page).to have_content "param is missing or the value is empty: catalog_url" + end end