Class: RubyCms::VisitorError

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/ruby_cms/visitor_error.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.log_error(exception, request) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'app/models/ruby_cms/visitor_error.rb', line 14

def self.log_error(exception, request)
  create!(
    **base_request_attrs(request),
    **exception_attrs(exception),
    request_params: sanitize_params(request.params)
  )
rescue StandardError => e
  Rails.logger.error "Failed to log visitor error: #{e.message}"
end

.log_routing_error(request) ⇒ Object

Log routing errors (404s) from catch-all route. Called by RubyCms::ErrorsController#not_found



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/ruby_cms/visitor_error.rb', line 26

def self.log_routing_error(request)
  return if Rails.env.development?
  return if request.path.start_with?("/admin")

  create!(
    **base_request_attrs(request),
    error_class: "ActionController::RoutingError",
    error_message: routing_error_message(request),
    backtrace: nil,
    request_params: nil
  )
rescue StandardError => e
  Rails.logger.error "Failed to log routing error: #{e.message}"
end

Instance Method Details

#browser_infoObject



55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/ruby_cms/visitor_error.rb', line 55

def browser_info
  return "Unknown" if user_agent.blank?

  case user_agent
  when /Chrome/ then "Chrome"
  when /Firefox/ then "Firefox"
  when /Safari(?!.*Chrome)/ then "Safari"
  when /Edge/ then "Edge"
  else "Other"
  end
end

#codepathObject

Returns the code path: backtrace for exceptions, or synthetic path for routing errors



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/ruby_cms/visitor_error.rb', line 42

def codepath
  if backtrace.present?
    backtrace
  elsif error_class == "ActionController::RoutingError"
    "      Request \u2192 Router (no matching route) \u2192 RubyCms::ErrorsController#not_found\n      (Routing errors don't generate stack traces; the request never reached a controller action.)\n    TEXT\n  else\n    \"No stack trace available.\"\n  end\nend\n".strip