debian-mirror-gitlab/spec/lib/gitlab/exception_log_formatter_spec.rb

66 lines
2.3 KiB
Ruby
Raw Normal View History

2022-01-26 12:08:38 +05:30
# 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
2022-04-01 21:47:47 +05:30
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
2022-01-26 12:08:38 +05:30
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 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
end
end