Class: Spree::Diagnostics::ErrorLog

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/diagnostics/error_log.rb

Overview

The most recent Spree errors, for the diagnostics report. Subscribed to the Rails error reporter, so it sees what Spree reports with Rails.error.report as well as unhandled request and job errors.

Only the exception class and where it was raised are kept, never the message: messages carry customer data (emails, addresses, values echoed from a row) and the report ends up in a public issue. Frames from the host application become app/[redacted] and frames from private gems gem/[redacted], so the merchant's own code is not shared either.

Entries live in Rails.cache, so a cache clear empties the log. With a per-process memory store a restart does too, and each process keeps its own.

Defined Under Namespace

Classes: Frame

Constant Summary collapse

CACHE_KEY =
'spree/diagnostics/errors'.freeze
LIMIT =
50
FRAME_LIMIT =
10
HOST_FRAME =
'app/[redacted]'.freeze
PRIVATE_FRAME =
'gem/[redacted]'.freeze
RECORDING =
:spree_diagnostics_error_log_recording

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear ⇒ void

This method returns an undefined value.



34
35
36
# File 'app/services/spree/diagnostics/error_log.rb', line 34

def clear
  Rails.cache.delete(CACHE_KEY)
end

.entries ⇒ Array<Hash>

Returns newest first.

Returns:

  • (Array<Hash>) —

    newest first



29
30
31
# File 'app/services/spree/diagnostics/error_log.rb', line 29

def entries
  Array(Rails.cache.read(CACHE_KEY)).sort_by { |entry| entry[:last_seen_at] }.reverse
end

.roots ⇒ Array<Array(String, Proc)>

Gem directories and the application's, longest first, so a path is matched by the most specific root: a gem vendored inside the app is that gem, and an app inside a gem's directory (a test dummy app) is the app. Built once per process, since every reported error needs it.

Returns:

  • (Array<Array(String, Proc)>) —

    path prefix and the frame it makes



44
45
46
47
48
# File 'app/services/spree/diagnostics/error_log.rb', line 44

def roots
  @roots ||= Gem.loaded_specs.values.map { |spec| [spec.full_gem_path + File::SEPARATOR, gem_frame(spec)] }.
             push([Rails.root.to_s + File::SEPARATOR, ->(_) { Frame.new(HOST_FRAME, false, true) }]).
             sort_by { |root, _| -root.length }
end

Instance Method Details

#report(error, context:, source: nil) ⇒ Object

Rails error subscriber interface. Leaves out refused requests: an error Rails answers with a 4xx (an invalid record, a missing one, a bad parameter) is the client's, not a bug. Never raises, and never recurses: a cache store that reports its own failure to Rails.error would otherwise call back in here.



65
66
67
68
69
70
71
72
73
74
# File 'app/services/spree/diagnostics/error_log.rb', line 65

def report(error, context:, source: nil, **)
  return if Thread.current[RECORDING] || client_error?(error)

  recording do
    frames = frames_for(error)
    record(error.class.name, source, frames, request_id(context)) if spree_error?(source, frames)
  end
rescue StandardError
  nil
end