Welcome to mirror list, hosted at ThFree Co, Russian Federation.

exception_log_formatter_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 179054e2d15b0834e2cc4a382e0607af08a285b3 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::ExceptionLogFormatter do
  describe '.format!' do
    let(:exception) { RuntimeError.new('bad request') }
    let(:backtrace) { caller }

    let(:payload) { {} }

    before do
      allow(exception).to receive(:backtrace).and_return(backtrace)
    end

    it 'adds exception data to log' do
      described_class.format!(exception, payload)

      expect(payload['exception.class']).to eq('RuntimeError')
      expect(payload['exception.message']).to eq('bad request')
      expect(payload['exception.backtrace']).to eq(Gitlab::BacktraceCleaner.clean_backtrace(backtrace))
      expect(payload['exception.sql']).to be_nil
    end

    it 'cleans the exception message' do
      expect(Gitlab::Sanitizers::ExceptionMessage).to receive(:clean).with('RuntimeError', 'bad request').and_return('cleaned')

      described_class.format!(exception, payload)

      expect(payload['exception.message']).to eq('cleaned')
    end

    context 'when exception is ActiveRecord::StatementInvalid' do
      let(:exception) { ActiveRecord::StatementInvalid.new(sql: 'SELECT "users".* FROM "users" WHERE "users"."id" = 1 AND "users"."foo" = $1') }

      it 'adds the normalized SQL query to payload' do
        described_class.format!(exception, payload)

        expect(payload['exception.sql']).to eq('SELECT "users".* FROM "users" WHERE "users"."id" = $2 AND "users"."foo" = $1')
      end
    end

    context 'when the ActiveRecord::StatementInvalid is wrapped in another exception' do
      before do
        allow(exception).to receive(:cause).and_return(ActiveRecord::StatementInvalid.new(sql: 'SELECT "users".* FROM "users" WHERE "users"."id" = 1 AND "users"."foo" = $1'))
      end

      it 'adds the cause_class to payload' do
        described_class.format!(exception, payload)

        expect(payload['exception.cause_class']).to eq('ActiveRecord::StatementInvalid')
      end

      it 'adds the normalized SQL query to payload' do
        described_class.format!(exception, payload)

        expect(payload['exception.sql']).to eq('SELECT "users".* FROM "users" WHERE "users"."id" = $2 AND "users"."foo" = $1')
      end
    end

    context 'when the ActiveRecord::StatementInvalid is a bad query' do
      let(:exception) { ActiveRecord::StatementInvalid.new(sql: 'SELECT SELECT FROM SELECT') }

      it 'adds the query as-is to payload' do
        described_class.format!(exception, payload)

        expect(payload['exception.sql']).to eq('SELECT SELECT FROM SELECT')
      end
    end

    context 'when exception is a gRPC bad status' do
      let(:unavailable_error) do
        ::GRPC::Unavailable.new(
          "unavailable",
          gitaly_error_metadata: {
            storage: 'default',
            address: 'unix://gitaly.socket',
            service: :ref_service,
            rpc: :find_local_branches
          }
        )
      end

      context 'when the gRPC error is wrapped by ::Gitlab::Git::BaseError' do
        let(:exception) { ::Gitlab::Git::CommandError.new(unavailable_error) }

        it 'adds gitaly metadata to payload' do
          described_class.format!(exception, payload)

          expect(payload['exception.gitaly']).to eq('{:storage=>"default", :address=>"unix://gitaly.socket", :service=>:ref_service, :rpc=>:find_local_branches}')
        end
      end

      context 'when the gRPC error is wrapped by another error' do
        before do
          allow(exception).to receive(:cause).and_return(unavailable_error)
        end

        it 'adds gitaly metadata to payload' do
          described_class.format!(exception, payload)

          expect(payload['exception.cause_class']).to eq('GRPC::Unavailable')
          expect(payload['exception.gitaly']).to eq('{:storage=>"default", :address=>"unix://gitaly.socket", :service=>:ref_service, :rpc=>:find_local_branches}')
        end
      end

      context 'when the gRPC error is not wrapped' do
        let(:exception) { unavailable_error }

        it 'adds gitaly metadata to payload' do
          described_class.format!(exception, payload)

          expect(payload['exception.cause_class']).to be_nil
          expect(payload['exception.gitaly']).to eq('{:storage=>"default", :address=>"unix://gitaly.socket", :service=>:ref_service, :rpc=>:find_local_branches}')
        end
      end
    end
  end
end