summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/grape_logging/loggers/exception_logger_spec.rb
blob: e21af023bb82f9514c2b21c7dcb1bedc7c3f5493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'spec_helper'

describe Gitlab::GrapeLogging::Loggers::ExceptionLogger do
  subject { described_class.new }

  let(:mock_request) { OpenStruct.new(env: {}) }

  describe ".parameters" do
    describe 'when no exception is available' do
      it 'returns an empty hash' do
        expect(subject.parameters(mock_request, nil)).to eq({})
      end
    end

    describe 'when an exception is available' do
      let(:exception) { RuntimeError.new('This is a test') }
      let(:mock_request) do
        OpenStruct.new(
          env: {
            ::API::Helpers::API_EXCEPTION_ENV => exception
          }
        )
      end

      let(:expected) do
        {
          'exception.class' => 'RuntimeError',
          'exception.message' => 'This is a test'
        }
      end

      it 'returns the correct fields' do
        expect(subject.parameters(mock_request, nil)).to eq(expected)
      end

      context 'with backtrace' do
        before do
          current_backtrace = caller
          allow(exception).to receive(:backtrace).and_return(current_backtrace)
          expected['exception.backtrace'] = Gitlab::Profiler.clean_backtrace(current_backtrace)
        end

        it 'includes the backtrace' do
          expect(subject.parameters(mock_request, nil)).to eq(expected)
        end
      end
    end
  end
end