Skip to content

Commit

Permalink
Issue 330 500 error improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleduyxyz committed May 24, 2024
1 parent 7de5226 commit 19ad933
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/kaui/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
module Kaui
# Subclassed to specify the correct layout
class SessionsController < Devise::SessionsController
include Kaui::ExceptionHelper

layout Kaui.config[:layout]

skip_before_action :check_for_redirect_to_tenant_screen, raise: false

# The sign-in flow eventually calls authenticate! from config/initializers/killbill_authenticatable.rb

rescue_from(StandardError) do |exception|
@error = standardize_exception(exception)
render 'kaui/errors/500', status: 500, layout: false
end

protected

# Override after_sign_in_path_for to not have to rely on the default 'root' config which we want to keep on home#index
Expand Down
19 changes: 19 additions & 0 deletions app/helpers/kaui/exception_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Kaui
module ExceptionHelper
def standardize_exception(exception)
case exception
when ActiveRecord::DatabaseConnectionError
I18n.translate('errors.messages.unable_to_connect_database')
when Errno::ECONNREFUSED
url = exception.message.match(/for "(.*)" port/)[1] rescue nil
url && (KillBillClient.url.include? url) ? I18n.translate('errors.messages.unable_to_connect_killbill') : nil
when ->(e) { e.class.name.start_with?('KillBillClient::API') }
I18n.translate('errors.messages.error_communicating_killbill')
else
nil
end
end
end
end
29 changes: 29 additions & 0 deletions app/views/kaui/errors/500.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>We're sorry, but something went wrong (500)</title>
<style type="text/css">
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 35em; /* Increase the width of the dialog */
padding: 1em 6em; /* Increase the padding of the dialog */
margin: 4em auto 0 auto;
border: 1px solid #ccc;
border-right-color: #999;
border-bottom-color: #999;
font-size: 1.2em; /* Increase the font size in the dialog */
}
h1 { font-size: 100%; color: #f00; line-height: 1.0em; } /* Increase the font size of the title */
</style>
</head>

<body>
<!-- This file lives in public/500.html -->
<div class="dialog">
<h1>We're sorry, but something went wrong.</h1>
<% if @error.present? %>
<p><%= @error %></p>
<% end %>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ en:
invalid_xml: "Invalid XML: %{error}"
invalid_min_date: "Invalid min date format"
invalid_max_date: "Invalid max date format"
unable_to_connect_database: "Unable to connect to the database."
unable_to_connect_killbill: "Unable to connect to the Kill Bill server."
error_communicating_killbill: "Error while communicating with the Kill Bill server."
views:
subscriptions:
requested_date_for_billing_notice: <ul>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def nested_param
controllers: { sessions: 'kaui/sessions', registrations: 'kaui/registrations' }

root to: 'home#index', as: 'kaui'
get '/500', to: 'errors#show', code: 500

scope '/accounts' do
match '/pagination' => 'accounts#pagination', :via => :get, :as => 'accounts_pagination'
Expand Down

0 comments on commit 19ad933

Please sign in to comment.