Websites/app/models/exception_log.rb

38 lines
1.3 KiB
Ruby
Raw Normal View History

2024-05-03 03:04:43 +00:00
# frozen_string_literal: true
class ExceptionLog < ApplicationRecord
serialize :extra_params, coder: JSON
def self.add(exception, request: nil, source: nil, **extra)
source ||= request.present? ? request.filtered_parameters.slice(:controller, :action).values.join("#") : "Unknown"
2024-05-03 03:04:43 +00:00
extra_params = {
host: Socket.gethostname,
params: request.try(:filtered_parameters),
referrer: request.try(:referrer),
user_agent: request.try(:user_agent) || "",
source: source,
**extra,
2024-05-03 03:04:43 +00:00
}
# Required to unwrap exceptions that occur inside template rendering.
unwrapped_exception = exception
unwrapped_exception = exception.cause if exception.is_a?(ActionView::Template::Error)
if unwrapped_exception.is_a?(ActiveRecord::QueryCanceled)
extra_params[:sql] = {}
extra_params[:sql][:query] = unwrapped_exception.sql || "[NOT FOUND?]"
extra_params[:sql][:binds] = unwrapped_exception.binds
end
create!(
ip_addr: request.try(:remote_ip) || "0.0.0.0",
2024-05-03 03:04:43 +00:00
class_name: unwrapped_exception.class.name,
message: unwrapped_exception.message,
trace: unwrapped_exception.backtrace.try(:join, "\n"),
2024-05-03 03:04:43 +00:00
code: SecureRandom.uuid,
2024-08-06 10:28:38 +00:00
version: Websites.config.full_version,
2024-05-03 03:04:43 +00:00
extra_params: extra_params,
)
end
end