5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 02:46:21 +00:00
panel/app/jobs/backtrace_job.rb
f 20f5acacb8 hacer una sola consulta
friendly_id primero busca por nombre y despues por id
2020-12-08 19:36:09 -03:00

65 lines
1.7 KiB
Ruby

# frozen_string_literal: true
# Procesa los errores de JavaScript
class BacktraceJob < ApplicationJob
queue_as :low_priority
attr_reader :params, :site_id
def perform(site_id:, params:)
@site_id = site_id
@params = params
return if files.empty?
params['errors'].each do |error|
error['backtrace'].each do |backtrace|
offset = SourceMap::Offset.new(backtrace['line'], backtrace['column'])
mapping = sourcemap.bsearch(offset)
next unless mapping
backtrace['file'] = mapping.source
backtrace['line'] = mapping.original.line
backtrace['column'] = mapping.original.column
end
end
begin
raise NoMethodError
rescue NoMethodError => e
ExceptionNotifier.notify_exception(e, data: { site: site.name, params: params })
end
end
private
def site
@site ||= Site.find_by_id(site_id)
end
# Obtiene todos los archivos del backtrace
def files
@files ||= params['errors'].map { |x| x['backtrace'] }.flatten.map { |x| x['file'].split('@').last }.uniq.select { |x| x&.start_with?(site.url) }
end
# Asume que todos los sourcemaps comparten la misma URL, lo
# correcto sería buscarlo en sourceMappingURL al final de cada
# archivo.
#
# Descarga los archivos y obtiene el backtrace original.
def sourcemap
@sourcemap ||=
begin
files.map { |x| "#{x}.map" }.map do |x|
data = FastJsonparser.parse(Rails.cache.fetch(x, expires_in: 12.hours) do
Down.open(x).read
end, symbolize_keys: false)
SourceMap::Map.from_hash data
rescue Down::Error, FastJsonparser::Error
SourceMap::Map.new
end.reduce(&:+)
end
end
end