2019-12-12 00:07:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-04 15:11:19 +00:00
|
|
|
# This should be in the ErrorTracking namespace. For more details, see:
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/323342
|
2019-12-12 00:07:43 +00:00
|
|
|
module Gitlab
|
|
|
|
module ErrorTracking
|
|
|
|
module StackTraceHighlightDecorator
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def decorate(error_event)
|
|
|
|
::Gitlab::ErrorTracking::ErrorEvent.new(
|
|
|
|
issue_id: error_event.issue_id,
|
|
|
|
date_received: error_event.date_received,
|
|
|
|
stack_trace_entries: highlight_stack_trace(error_event.stack_trace_entries)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def highlight_stack_trace(stack_trace)
|
|
|
|
stack_trace.map do |entry|
|
|
|
|
highlight_stack_trace_entry(entry)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_stack_trace_entry(entry)
|
|
|
|
return entry unless entry['context']
|
|
|
|
|
|
|
|
entry.merge('context' => highlight_entry_context(entry['filename'], entry['context']))
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_entry_context(filename, context)
|
2019-12-25 15:07:36 +00:00
|
|
|
language = guess_language_by_filename(filename)
|
2019-12-12 00:07:43 +00:00
|
|
|
|
|
|
|
context.map do |line_number, line_of_code|
|
|
|
|
[
|
|
|
|
line_number,
|
|
|
|
# Passing nil for the blob name allows skipping linking dependencies for the line_of_code
|
|
|
|
Gitlab::Highlight.highlight(nil, line_of_code, language: language)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2019-12-25 15:07:36 +00:00
|
|
|
|
|
|
|
def guess_language_by_filename(filename)
|
|
|
|
Rouge::Lexer.guess_by_filename(filename).tag
|
|
|
|
rescue Rouge::Guesser::Ambiguous => e
|
|
|
|
e.alternatives.min_by(&:tag)&.tag
|
|
|
|
end
|
2019-12-12 00:07:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|