diff options
Diffstat (limited to 'lib/gitlab/error_tracking')
-rw-r--r-- | lib/gitlab/error_tracking/stack_trace_highlight_decorator.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/gitlab/error_tracking/stack_trace_highlight_decorator.rb b/lib/gitlab/error_tracking/stack_trace_highlight_decorator.rb new file mode 100644 index 00000000000..a403275fd4e --- /dev/null +++ b/lib/gitlab/error_tracking/stack_trace_highlight_decorator.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +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) + language = Rouge::Lexer.guess_by_filename(filename).tag + + 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 + end + end +end |