38 lines
1.3 KiB
Ruby
38 lines
1.3 KiB
Ruby
# 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"
|
|
extra_params = {
|
|
host: Socket.gethostname,
|
|
params: request.try(:filtered_parameters),
|
|
referrer: request.try(:referrer),
|
|
user_agent: request.try(:user_agent) || "",
|
|
source: source,
|
|
**extra,
|
|
}
|
|
|
|
# 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",
|
|
class_name: unwrapped_exception.class.name,
|
|
message: unwrapped_exception.message,
|
|
trace: unwrapped_exception.backtrace.try(:join, "\n"),
|
|
code: SecureRandom.uuid,
|
|
version: Websites.config.full_version,
|
|
extra_params: extra_params,
|
|
)
|
|
end
|
|
end
|