Class: Tina4::ErrorTracker
- Inherits:
-
Object
- Object
- Tina4::ErrorTracker
- Defined in:
- lib/tina4/dev_admin.rb
Overview
Thread-safe, file-persisted error tracker for the dev dashboard Error Tracker panel.
Errors are stored in a JSON file in the system temp directory keyed by project path, so they survive across requests and server restarts. Duplicate errors (same type + message + file + line) are de-duplicated —the count increments and the entry is re-opened if it was resolved.
Instance Method Summary collapse
-
#capture(error_type:, message:, traceback: "", file: "", line: 0) ⇒ Object
Capture a Ruby error / exception into the tracker.
-
#capture_exception(exc) ⇒ Object
Capture a Ruby exception object directly.
-
#clear_all ⇒ Object
Remove ALL errors.
-
#clear_resolved ⇒ Object
Remove all resolved errors.
-
#get(include_resolved: true) ⇒ Object
Return all errors (newest first).
-
#health ⇒ Object
Health summary (matches Python BrokenTracker interface).
-
#initialize ⇒ ErrorTracker
constructor
A new instance of ErrorTracker.
-
#reset! ⇒ Object
Reset (for testing).
-
#resolve(id) ⇒ Object
Mark a single error as resolved.
-
#unresolved_count ⇒ Object
Count of unresolved errors.
Constructor Details
#initialize ⇒ ErrorTracker
Returns a new instance of ErrorTracker.
122 123 124 125 126 127 128 129 |
# File 'lib/tina4/dev_admin.rb', line 122 def initialize @mutex = Mutex.new @errors = nil # lazy-loaded @store_path = File.join( Dir.tmpdir, "tina4_dev_errors_#{Digest::MD5.hexdigest(Dir.pwd)}.json" ) end |
Instance Method Details
#capture(error_type:, message:, traceback: "", file: "", line: 0) ⇒ Object
Capture a Ruby error / exception into the tracker.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/tina4/dev_admin.rb', line 137 def capture(error_type:, message:, traceback: "", file: "", line: 0) @mutex.synchronize do load_unlocked fingerprint = Digest::MD5.hexdigest("#{error_type}|#{}|#{file}|#{line}") now = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ") if @errors.key?(fingerprint) @errors[fingerprint][:count] += 1 @errors[fingerprint][:last_seen] = now @errors[fingerprint][:resolved] = false # re-open resolved duplicates else @errors[fingerprint] = { id: fingerprint, error_type: error_type, message: , traceback: traceback, file: file, line: line, first_seen: now, last_seen: now, count: 1, resolved: false } end save_unlocked end end |
#capture_exception(exc) ⇒ Object
Capture a Ruby exception object directly.
166 167 168 169 170 171 172 173 174 |
# File 'lib/tina4/dev_admin.rb', line 166 def capture_exception(exc) capture( error_type: exc.class.name, message: exc., traceback: (exc.backtrace || []).first(20).join("\n"), file: (exc.backtrace_locations&.first&.path || ""), line: (exc.backtrace_locations&.first&.lineno || 0) ) end |
#clear_all ⇒ Object
Remove ALL errors.
228 229 230 231 232 233 |
# File 'lib/tina4/dev_admin.rb', line 228 def clear_all @mutex.synchronize do @errors = {} save_unlocked end end |
#clear_resolved ⇒ Object
Remove all resolved errors.
219 220 221 222 223 224 225 |
# File 'lib/tina4/dev_admin.rb', line 219 def clear_resolved @mutex.synchronize do load_unlocked @errors.reject! { |_, e| e[:resolved] } save_unlocked end end |
#get(include_resolved: true) ⇒ Object
Return all errors (newest first).
178 179 180 181 182 183 184 185 |
# File 'lib/tina4/dev_admin.rb', line 178 def get(include_resolved: true) @mutex.synchronize do load_unlocked entries = @errors.values entries = entries.reject { |e| e[:resolved] } unless include_resolved entries.sort_by { |e| e[:last_seen] }.reverse end end |
#health ⇒ Object
Health summary (matches Python BrokenTracker interface).
196 197 198 199 200 201 202 203 204 |
# File 'lib/tina4/dev_admin.rb', line 196 def health @mutex.synchronize do load_unlocked total = @errors.size resolved = @errors.count { |_, e| e[:resolved] } unresolved = total - resolved { total: total, unresolved: unresolved, resolved: resolved, healthy: unresolved.zero? } end end |
#reset! ⇒ Object
Reset (for testing).
236 237 238 239 240 241 |
# File 'lib/tina4/dev_admin.rb', line 236 def reset! @mutex.synchronize do @errors = {} File.delete(@store_path) if File.exist?(@store_path) end end |
#resolve(id) ⇒ Object
Mark a single error as resolved.
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/tina4/dev_admin.rb', line 207 def resolve(id) @mutex.synchronize do load_unlocked return false unless @errors.key?(id) @errors[id][:resolved] = true save_unlocked true end end |
#unresolved_count ⇒ Object
Count of unresolved errors.
188 189 190 191 192 193 |
# File 'lib/tina4/dev_admin.rb', line 188 def unresolved_count @mutex.synchronize do load_unlocked @errors.count { |_, e| !e[:resolved] } end end |