From f7e3c2a19a6124e46495a668e7fbb2e005774772 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Mon, 2 Jun 2025 20:33:50 +0530 Subject: [PATCH] refactor: simplify error handling --- .../google/callbacks_controller.rb | 9 ++++---- config/locales/en.yml | 2 +- lib/custom_exceptions/oauth.rb | 21 ------------------- .../google/callbacks_controller_spec.rb | 18 ++++++++++++++-- 4 files changed, 21 insertions(+), 29 deletions(-) delete mode 100644 lib/custom_exceptions/oauth.rb diff --git a/app/controllers/google/callbacks_controller.rb b/app/controllers/google/callbacks_controller.rb index 047d7c7d5..6bf7a9db7 100644 --- a/app/controllers/google/callbacks_controller.rb +++ b/app/controllers/google/callbacks_controller.rb @@ -19,7 +19,7 @@ class Google::CallbacksController < OauthCallbackController missing_scopes = required_scopes - granted_scopes return if missing_scopes.empty? - raise CustomExceptions::OAuth::InsufficientScopes.new({ missing_scopes: missing_scopes }) + raise StandardError, I18n.t('errors.oauth.insufficient_scopes', scopes: missing_scopes.join(', ')) end def provider_name @@ -38,10 +38,9 @@ class Google::CallbacksController < OauthCallbackController def handle_error(exception) ChatwootExceptionTracker.new(exception).capture_exception - error_code = exception.respond_to?(:code) ? exception.code : 'OAUTH_ERR' - error_message = exception.message || I18n.t('errors.oauth.authorization_failed', provider: 'Google') - + error_message = exception.message || I18n.t('errors.oauth.authorization_failed') redirect_url = "#{ENV.fetch('FRONTEND_URL', 'http://localhost:3000')}/app/accounts/#{account.id}/settings/inboxes/new/email" - redirect_to "#{redirect_url}?error=#{CGI.escape(error_message)}&code=#{error_code}" + + redirect_to "#{redirect_url}?error=#{CGI.escape(error_message)}" end end diff --git a/config/locales/en.yml b/config/locales/en.yml index f8af898fe..f099a5794 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -50,7 +50,7 @@ en: failed: Signup failed oauth: insufficient_scopes: 'Insufficient permissions granted. Missing scopes: %{scopes}' - authorization_failed: 'Authorization failed for %{provider}' + authorization_failed: 'Authorization failed' data_import: data_type: invalid: Invalid data type diff --git a/lib/custom_exceptions/oauth.rb b/lib/custom_exceptions/oauth.rb deleted file mode 100644 index 0bffa8531..000000000 --- a/lib/custom_exceptions/oauth.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module CustomExceptions::OAuth - class InsufficientScopes < CustomExceptions::Base - def message - I18n.t('errors.oauth.insufficient_scopes', scopes: @data[:missing_scopes].join(', ')) - end - - def code - 'SCOPE_ERR' - end - - def to_hash - { - message: message, - code: code, - missing_scopes: @data[:missing_scopes] - } - end - end -end \ No newline at end of file diff --git a/spec/controllers/google/callbacks_controller_spec.rb b/spec/controllers/google/callbacks_controller_spec.rb index 91535533c..94db10a46 100644 --- a/spec/controllers/google/callbacks_controller_spec.rb +++ b/spec/controllers/google/callbacks_controller_spec.rb @@ -73,7 +73,20 @@ RSpec.describe 'Google::CallbacksController', type: :request do expect(inbox.name).to eq email.split('@').first.parameterize.titleize end - it 'redirects to google app in case of error' do + it 'redirects to custom error URL when insufficient scopes are granted' do + stub_request(:post, 'https://accounts.google.com/o/oauth2/token') + .with(body: { 'code' => code, 'grant_type' => 'authorization_code', + 'redirect_uri' => "#{ENV.fetch('FRONTEND_URL', 'http://localhost:3000')}/google/callback" }) + .to_return(status: 200, body: response_body_insufficient_scopes.to_json, headers: { 'Content-Type' => 'application/json' }) + + get google_callback_url, params: { code: code } + + expect(response).to redirect_to(/#{Regexp.escape("#{ENV.fetch('FRONTEND_URL', 'http://localhost:3000')}/app/accounts/#{account.id}/settings/inboxes/new/email")}/) + expect(response.location).to include('error=') + expect(Redis::Alfred.get(cache_key).to_i).to eq account.id + end + + it 'redirects to custom error URL in case of OAuth error' do stub_request(:post, 'https://accounts.google.com/o/oauth2/token') .with(body: { 'code' => code, 'grant_type' => 'authorization_code', 'redirect_uri' => "#{ENV.fetch('FRONTEND_URL', 'http://localhost:3000')}/google/callback" }) @@ -81,7 +94,8 @@ RSpec.describe 'Google::CallbacksController', type: :request do get google_callback_url, params: { code: code } - expect(response).to redirect_to '/' + expect(response).to redirect_to(/#{Regexp.escape("#{ENV.fetch('FRONTEND_URL', 'http://localhost:3000')}/app/accounts/#{account.id}/settings/inboxes/new/email")}/) + expect(response.location).to include('error=') expect(Redis::Alfred.get(cache_key).to_i).to eq account.id end end