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.
124 125 126 127 128 129 130 131 132 |
# File 'lib/tina4/dev_admin.rb', line 124 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.
140 141 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 |
# File 'lib/tina4/dev_admin.rb', line 140 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.
169 170 171 172 173 174 175 176 177 |
# File 'lib/tina4/dev_admin.rb', line 169 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.
231 232 233 234 235 236 |
# File 'lib/tina4/dev_admin.rb', line 231 def clear_all @mutex.synchronize do @errors = {} save_unlocked end end |
#clear_resolved ⇒ Object
Remove all resolved errors.
222 223 224 225 226 227 228 |
# File 'lib/tina4/dev_admin.rb', line 222 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).
181 182 183 184 185 186 187 188 |
# File 'lib/tina4/dev_admin.rb', line 181 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).
199 200 201 202 203 204 205 206 207 |
# File 'lib/tina4/dev_admin.rb', line 199 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.
241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/tina4/dev_admin.rb', line 241 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).
254 255 256 257 258 259 260 |
# File 'lib/tina4/dev_admin.rb', line 254 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.
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/tina4/dev_admin.rb', line 210 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.
191 192 193 194 195 196 |
# File 'lib/tina4/dev_admin.rb', line 191 def unresolved_count @mutex.synchronize do load_unlocked @errors.count { |_, e| !e[:resolved] } end end |