Add logging to snippet services

parent 81835524
...@@ -50,6 +50,7 @@ module Snippets ...@@ -50,6 +50,7 @@ module Snippets
snippet_saved snippet_saved
rescue => e # Rescuing all because we can receive Creation exceptions, GRPC exceptions, Git exceptions, ... rescue => e # Rescuing all because we can receive Creation exceptions, GRPC exceptions, Git exceptions, ...
snippet.errors.add(:base, e.message) snippet.errors.add(:base, e.message)
log_error(e.message)
# If the commit action failed we need to remove the repository if exists # If the commit action failed we need to remove the repository if exists
snippet.repository.remove if snippet.repository_exists? snippet.repository.remove if snippet.repository_exists?
......
...@@ -51,8 +51,9 @@ module Snippets ...@@ -51,8 +51,9 @@ module Snippets
# the changes # the changes
create_commit(snippet) if snippet.repository_exists? create_commit(snippet) if snippet.repository_exists?
end end
rescue rescue => e
snippet.errors.add(:repository, 'Error updating the snippet') snippet.errors.add(:repository, 'Error updating the snippet')
log_error(e.message)
false false
end end
......
...@@ -199,6 +199,12 @@ describe Snippets::CreateService do ...@@ -199,6 +199,12 @@ describe Snippets::CreateService do
expect(SnippetRepository.count).to be_zero expect(SnippetRepository.count).to be_zero
end end
it 'logs the error' do
expect(Gitlab::AppLogger).to receive(:error).with('foobar')
subject
end
it 'returns the error' do it 'returns the error' do
response = subject response = subject
......
...@@ -167,13 +167,23 @@ describe Snippets::UpdateService do ...@@ -167,13 +167,23 @@ describe Snippets::UpdateService do
expect(blob.data).to eq(options[:content]) expect(blob.data).to eq(options[:content])
end end
it 'returns error when the commit action fails' do context 'when an error is raised' do
allow(snippet.snippet_repository).to receive(:multi_files_action).and_raise(SnippetRepository::CommitError) before do
allow(snippet.snippet_repository).to receive(:multi_files_action).and_raise(SnippetRepository::CommitError, 'foobar')
end
response = subject it 'logs the error' do
expect(Gitlab::AppLogger).to receive(:error).with('foobar')
expect(response).to be_error subject
expect(response.payload[:snippet].errors.full_messages).to eq ['Repository Error updating the snippet'] end
it 'returns error with generic error message' do
response = subject
expect(response).to be_error
expect(response.payload[:snippet].errors.full_messages).to eq ['Repository Error updating the snippet']
end
end end
it 'returns error if snippet does not have a snippet_repository' do it 'returns error if snippet does not have a snippet_repository' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment