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.
-
#register ⇒ Object
Register Ruby error handlers to feed the tracker.
-
#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.
126 127 128 129 130 131 132 133 134 |
# File 'lib/tina4/dev_admin.rb', line 126 def initialize @mutex = Mutex.new @errors = nil # lazy-loaded @registered = false @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.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/tina4/dev_admin.rb', line 142 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.
171 172 173 174 175 176 177 178 179 |
# File 'lib/tina4/dev_admin.rb', line 171 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.
233 234 235 236 237 238 |
# File 'lib/tina4/dev_admin.rb', line 233 def clear_all @mutex.synchronize do @errors = {} save_unlocked end end |
#clear_resolved ⇒ Object
Remove all resolved errors.
224 225 226 227 228 229 230 |
# File 'lib/tina4/dev_admin.rb', line 224 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).
183 184 185 186 187 188 189 190 |
# File 'lib/tina4/dev_admin.rb', line 183 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).
201 202 203 204 205 206 207 208 209 |
# File 'lib/tina4/dev_admin.rb', line 201 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 |
#register ⇒ Object
Register Ruby error handlers to feed the tracker. Installs an at_exit hook that captures unhandled exceptions. Safe to call multiple times — only registers once.
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/tina4/dev_admin.rb', line 243 def register return if @registered @registered = true tracker = self at_exit do if (exc = $!) && !exc.is_a?(SystemExit) tracker.capture_exception(exc) end end end |
#reset! ⇒ Object
Reset (for testing).
256 257 258 259 260 261 262 |
# File 'lib/tina4/dev_admin.rb', line 256 def reset! @mutex.synchronize do @errors = {} @registered = false File.delete(@store_path) if File.exist?(@store_path) end end |
#resolve(id) ⇒ Object
Mark a single error as resolved.
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/tina4/dev_admin.rb', line 212 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.
193 194 195 196 197 198 |
# File 'lib/tina4/dev_admin.rb', line 193 def unresolved_count @mutex.synchronize do load_unlocked @errors.count { |_, e| !e[:resolved] } end end |