Skip to content

Commit

Permalink
Moving handle exception to killbill-admin-ui-standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleduyxyz committed Jun 18, 2024
1 parent bd84df4 commit 824cd41
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
31 changes: 3 additions & 28 deletions app/controllers/kaui/engine_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require_relative '../../../lib/kaui/error_handler'

class Kaui::EngineController < ApplicationController

include Kaui::EngineControllerUtil
include Kaui::ErrorHandler

before_action :authenticate_user!, :check_for_redirect_to_tenant_screen, :populate_account_details

Expand Down Expand Up @@ -59,25 +62,6 @@ def retrieve_allowed_users_for_current_user
end
end

# Note! Order matters, StandardError needs to be first
rescue_from(StandardError) do |error|
flash[:error] = "Error: #{as_string(error)}"
try_to_redirect_to_account_path = !params[:controller].ends_with?('accounts')
perform_redirect_after_error try_to_redirect_to_account_path
end

rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
log_rescue_error parameter_missing_exception
flash[:error] = "Required parameter missing: #{parameter_missing_exception.param}"
perform_redirect_after_error
end

rescue_from(KillBillClient::API::ResponseError) do |killbill_exception|
flash[:error] = "Error while communicating with the Kill Bill server: #{as_string(killbill_exception)}"
try_to_redirect_to_account_path = !killbill_exception.is_a?(KillBillClient::API::Unauthorized) && !(killbill_exception.is_a?(KillBillClient::API::NotFound) && params[:controller].ends_with?('accounts'))
perform_redirect_after_error try_to_redirect_to_account_path
end

private

def current_tenant_user
Expand All @@ -95,13 +79,4 @@ def current_tenant_user
end
result
end

def perform_redirect_after_error(try_to_redirect_to_account_path = true)
account_id = nested_hash_value(params.permit!.to_h, :account_id)
if try_to_redirect_to_account_path && account_id.present?
redirect_to kaui_engine.account_path(account_id)
else
redirect_to kaui_engine.home_path
end
end
end
14 changes: 10 additions & 4 deletions app/helpers/kaui/exception_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
module Kaui
module ExceptionHelper
def standardize_exception(exception)
if defined?(JRUBY_VERSION)
case exception
when ActiveRecord::JDBCError
return I18n.translate('errors.messages.unable_to_connect_database')
end
end

case exception
when ActiveRecord::DatabaseConnectionError, ActiveRecord::JDBCError
when ActiveRecord::DatabaseConnectionError
I18n.translate('errors.messages.unable_to_connect_database')
when Errno::ECONNREFUSED
url = exception.message.match(/for "(.*)" port/) { |m| m[1] }
url && (KillBillClient.url.include? url) ? I18n.translate('errors.messages.unable_to_connect_killbill') : nil
when Errno::ECONNREFUSED, Errno::EBADF
I18n.translate('errors.messages.unable_to_connect_killbill')
when ->(e) { e.class.name.start_with?('KillBillClient::API') }
I18n.translate('errors.messages.error_communicating_killbill')
else
Expand Down
37 changes: 37 additions & 0 deletions lib/kaui/error_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Kaui
module ErrorHandler
extend ActiveSupport::Concern
include Kaui::EngineControllerUtil

included do
rescue_from(StandardError) do |error|
flash[:error] = "Error: #{as_string(error)}"
try_to_redirect_to_account_path = !params[:controller].ends_with?('accounts')
perform_redirect_after_error(redirect: try_to_redirect_to_account_path)
end

rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
log_rescue_error parameter_missing_exception
flash[:error] = "Required parameter missing: #{parameter_missing_exception.param}"
perform_redirect_after_error
end

rescue_from(KillBillClient::API::ResponseError) do |killbill_exception|
flash[:error] = "Error while communicating with the Kill Bill server: #{as_string(killbill_exception)}"
try_to_redirect_to_account_path = !killbill_exception.is_a?(KillBillClient::API::Unauthorized) && !(killbill_exception.is_a?(KillBillClient::API::NotFound) && params[:controller].ends_with?('accounts'))
perform_redirect_after_error(redirect: try_to_redirect_to_account_path)
end
end

def perform_redirect_after_error(redirect: true)
account_id = nested_hash_value(params.permit!.to_h, :account_id)
if redirect && account_id.present?
redirect_to kaui_engine.account_path(account_id)
else
redirect_to kaui_engine.home_path
end
end
end
end

0 comments on commit 824cd41

Please sign in to comment.