make ExceptionLog work outside of requests

This commit is contained in:
Donovan Daniels 2024-10-21 01:36:59 -05:00
parent e61efff10a
commit 6f8cb49a03
Signed by: Donovan_DMC
GPG Key ID: 907D29CBFD6157BA
4 changed files with 15 additions and 11 deletions

View File

@ -205,7 +205,7 @@ class ApplicationController < ActionController::Base
@message = "An unexpected error occurred." if !(Rails.env.development? || CurrentUser.is_admin?) && message == exception.message @message = "An unexpected error occurred." if !(Rails.env.development? || CurrentUser.is_admin?) && message == exception.message
WebLogger.log_exception(@exception, expected: @expected) WebLogger.log_exception(@exception, expected: @expected)
log = ExceptionLog.add(exception, request) unless @expected log = ExceptionLog.add(exception, request: request) unless @expected
@log_code = log&.code @log_code = log&.code
render("static/error", status: status, formats: format) render("static/error", status: status, formats: format)
end end

View File

@ -5,11 +5,12 @@ class GitJob < ApplicationJob
good_job_control_concurrency_with(perform_limit: 1) good_job_control_concurrency_with(perform_limit: 1)
# used for locking def perform(identifier, *commands)
def perform(_identifier, *commands)
commands.each do |command| commands.each do |command|
system!(command) system!(command)
end end
rescue StandardError => e
ExceptionLog.add(e, source: "GitJob", identifier: identifier)
end end
private private

View File

@ -3,12 +3,15 @@
class ExceptionLog < ApplicationRecord class ExceptionLog < ApplicationRecord
serialize :extra_params, coder: JSON serialize :extra_params, coder: JSON
def self.add(exception, request) def self.add(exception, request: nil, source: nil, **extra)
source ||= request.present? ? request.filtered_parameters.slice(:controller, :action).values.join("#") : "Unknown"
extra_params = { extra_params = {
host: Socket.gethostname, host: Socket.gethostname,
params: request.filtered_parameters, params: request.try(:filtered_parameters),
referrer: request.referrer, referrer: request.try(:referrer),
user_agent: request.user_agent || "", user_agent: request.try(:user_agent) || "",
source: source,
**extra,
} }
# Required to unwrap exceptions that occur inside template rendering. # Required to unwrap exceptions that occur inside template rendering.
@ -22,10 +25,10 @@ class ExceptionLog < ApplicationRecord
end end
create!( create!(
ip_addr: request.remote_ip || "0.0.0.0", ip_addr: request.try(:remote_ip) || "0.0.0.0",
class_name: unwrapped_exception.class.name, class_name: unwrapped_exception.class.name,
message: unwrapped_exception.message, message: unwrapped_exception.message,
trace: unwrapped_exception.backtrace.join("\n"), trace: unwrapped_exception.backtrace.try(:join, "\n"),
code: SecureRandom.uuid, code: SecureRandom.uuid,
version: Websites.config.full_version, version: Websites.config.full_version,
extra_params: extra_params, extra_params: extra_params,

View File

@ -4,7 +4,7 @@
<th style="width: 200px">Created At</th> <th style="width: 200px">Created At</th>
<th>Code</th> <th>Code</th>
<th style="width: 100px">Commit</th> <th style="width: 100px">Commit</th>
<th>Controller/Action</th> <th>Source</th>
<th>Message</th> <th>Message</th>
<th>Stacktrace</th> <th>Stacktrace</th>
</tr> </tr>
@ -15,7 +15,7 @@
<td><%= compact_time(exception.created_at) %></td> <td><%= compact_time(exception.created_at) %></td>
<td><%= exception.code %></td> <td><%= exception.code %></td>
<td><%= exception.version.present? ? link_to(exception.version, GitHelper.commit_url(exception.version)) : "Unknown" %></td> <td><%= exception.version.present? ? link_to(exception.version, GitHelper.commit_url(exception.version)) : "Unknown" %></td>
<td><%= exception.extra_params.dig("params", "controller") %>/<%= exception.extra_params.dig("params", "action") %></td> <td><%= exception.extra_params["source"] %></td>
<td><%= truncate(exception.message, length: 50) %></td> <td><%= truncate(exception.message, length: 50) %></td>
<td><%= link_to("View", admin_exception_path(exception)) %></td> <td><%= link_to("View", admin_exception_path(exception)) %></td>
</tr> </tr>