2024-05-03 03:04:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ExceptionLog < ApplicationRecord
|
|
|
|
serialize :extra_params, coder: JSON
|
|
|
|
|
2024-10-21 06:36:59 +00:00
|
|
|
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,
|
2024-10-21 06:36:59 +00:00
|
|
|
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!(
|
2024-10-21 06:36:59 +00:00
|
|
|
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,
|
2024-10-21 06:36:59 +00:00
|
|
|
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
|