refactor: simplify error handling

This commit is contained in:
Shivam Mishra
2025-06-02 20:33:50 +05:30
parent 3197fafad1
commit 0a27c3fcbf
4 changed files with 5 additions and 29 deletions
@@ -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
+1 -1
View File
@@ -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
-21
View File
@@ -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
@@ -88,7 +88,6 @@ RSpec.describe 'Google::CallbacksController', type: :request do
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(response.location).to include('code=SCOPE_ERR')
expect(Redis::Alfred.get(cache_key).to_i).to eq account.id
end
@@ -102,7 +101,6 @@ RSpec.describe 'Google::CallbacksController', type: :request do
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(response.location).to include('code=OAUTH_ERR')
expect(Redis::Alfred.get(cache_key).to_i).to eq account.id
end
end