Module: Dbviewer::ErrorHandling

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController
Defined in:
app/controllers/concerns/dbviewer/error_handling.rb

Instance Method Summary collapse

Instance Method Details

#handle_database_error(exception) ⇒ Object

Handle database connection errors



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/concerns/dbviewer/error_handling.rb', line 11

def handle_database_error(exception)
  message = case exception
  when ActiveRecord::ConnectionNotEstablished
    "Database connection could not be established."
  when ActiveRecord::StatementInvalid
    "Invalid SQL statement: #{exception.message}"
  else
    "Database error: #{exception.message}"
  end

  flash.now[:error] = message
  Rails.logger.error("Database error: #{exception.message}\n#{exception.backtrace.join("\n")}")

  # Determine where to redirect based on the current action
  if action_name == "show" || action_name == "query"
    @error = message
    @records = nil
    render action_name
  else
    @tables = []
    render :index
  end
end

#log_error(error, prefix = "Error") ⇒ Object



35
36
37
38
39
40
# File 'app/controllers/concerns/dbviewer/error_handling.rb', line 35

def log_error(error, prefix = "Error")
  error_msg = "#{prefix}: #{error.message}"
  flash.now[:error] = error_msg
  Rails.logger.error("#{prefix}: #{error.message}\n#{error.backtrace.join("\n")}")
  error_msg
end