From 4c84fdf7f1499d7ddfaa4475830fd438436df620 Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 21 Jan 2025 16:08:01 +1100 Subject: [PATCH] Show more specific message for oauth errors I forgot how hard it is to do view stuff in controllers. I tried using the short lazy-lookup key '.oauth_error_html', which is supposed to work in controllers. But perhaps it doesn't work within a rescue? --- .../admin/dfc_product_imports_controller.rb | 12 ++++++++++-- config/locales/en.yml | 1 + spec/system/admin/dfc_product_import_spec.rb | 7 +++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/dfc_product_imports_controller.rb b/app/controllers/admin/dfc_product_imports_controller.rb index 1f909ae76b..5e01c3b9f3 100644 --- a/app/controllers/admin/dfc_product_imports_controller.rb +++ b/app/controllers/admin/dfc_product_imports_controller.rb @@ -35,10 +35,18 @@ module Admin end @count = imported.compact.count + rescue Rack::OAuth2::Client::Error => e + flash[:error] = I18n.t( + 'admin.dfc_product_imports.index.oauth_error_html', + message: e.message, + oidc_settings_link: ActionController::Base.helpers.link_to( + I18n.t('spree.admin.tab.oidc_settings'), Rails.application.routes.url_helpers.admin_oidc_settings_path + ) + ).html_safe + redirect_to admin_product_import_path rescue Faraday::Error, Addressable::URI::InvalidURIError, - ActionController::ParameterMissing, - Rack::OAuth2::Client::Error => e + ActionController::ParameterMissing => e flash[:error] = e.message redirect_to admin_product_import_path end diff --git a/config/locales/en.yml b/config/locales/en.yml index 48ba87aee5..15242a01f3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -849,6 +849,7 @@ en: index: title: "Importing a DFC product catalog" imported_products: "Imported products:" + oauth_error_html: "Authentication error: %{message}. See %{oidc_settings_link}" enterprise_fees: index: title: "Enterprise Fees" diff --git a/spec/system/admin/dfc_product_import_spec.rb b/spec/system/admin/dfc_product_import_spec.rb index 323699e09e..8f19dec45c 100644 --- a/spec/system/admin/dfc_product_import_spec.rb +++ b/spec/system/admin/dfc_product_import_spec.rb @@ -91,8 +91,11 @@ RSpec.describe "DFC Product Import" do click_button "Import" - expect(page).to have_content "invalid_grant" - expect(page).to have_content "session not active" + within ".flash" do + expect(page).to have_content "invalid_grant" + expect(page).to have_content "session not active" + expect(page).to have_link "OIDC Settings" + end end it "fails gracefully" do