Commit 7bcd0dc1 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'bvl-move-wrapped-gitaly-errors' into 'master'

Move Repository#wrapped_gitaly_errors into concern

See merge request gitlab-org/gitlab-ce!22691
parents 908a1957 81f5955e
...@@ -5,6 +5,7 @@ module Gitlab ...@@ -5,6 +5,7 @@ module Gitlab
class Blob class Blob
include Gitlab::BlobHelper include Gitlab::BlobHelper
include Gitlab::EncodingHelper include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
# This number is the maximum amount of data that we want to display to # This number is the maximum amount of data that we want to display to
# the user. We load as much as we can for encoding detection and LFS # the user. We load as much as we can for encoding detection and LFS
...@@ -75,7 +76,7 @@ module Gitlab ...@@ -75,7 +76,7 @@ module Gitlab
# Returns array of Gitlab::Git::Blob # Returns array of Gitlab::Git::Blob
# Does not guarantee blob data will be set # Does not guarantee blob data will be set
def batch_lfs_pointers(repository, blob_ids) def batch_lfs_pointers(repository, blob_ids)
repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
repository.gitaly_blob_client.batch_lfs_pointers(blob_ids.to_a) repository.gitaly_blob_client.batch_lfs_pointers(blob_ids.to_a)
end end
end end
......
...@@ -3,6 +3,7 @@ module Gitlab ...@@ -3,6 +3,7 @@ module Gitlab
module Git module Git
class Commit class Commit
include Gitlab::EncodingHelper include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
attr_accessor :raw_commit, :head attr_accessor :raw_commit, :head
...@@ -59,7 +60,7 @@ module Gitlab ...@@ -59,7 +60,7 @@ module Gitlab
# This saves us an RPC round trip. # This saves us an RPC round trip.
return nil if commit_id.include?(':') return nil if commit_id.include?(':')
commit = repo.wrapped_gitaly_errors do commit = wrapped_gitaly_errors do
repo.gitaly_commit_client.find_commit(commit_id) repo.gitaly_commit_client.find_commit(commit_id)
end end
...@@ -100,7 +101,7 @@ module Gitlab ...@@ -100,7 +101,7 @@ module Gitlab
# Commit.between(repo, '29eda46b', 'master') # Commit.between(repo, '29eda46b', 'master')
# #
def between(repo, base, head) def between(repo, base, head)
repo.wrapped_gitaly_errors do wrapped_gitaly_errors do
repo.gitaly_commit_client.between(base, head) repo.gitaly_commit_client.between(base, head)
end end
end end
...@@ -125,7 +126,7 @@ module Gitlab ...@@ -125,7 +126,7 @@ module Gitlab
# are documented here: # are documented here:
# http://www.rubydoc.info/github/libgit2/rugged/Rugged#SORT_NONE-constant) # http://www.rubydoc.info/github/libgit2/rugged/Rugged#SORT_NONE-constant)
def find_all(repo, options = {}) def find_all(repo, options = {})
repo.wrapped_gitaly_errors do wrapped_gitaly_errors do
Gitlab::GitalyClient::CommitService.new(repo).find_all_commits(options) Gitlab::GitalyClient::CommitService.new(repo).find_all_commits(options)
end end
end end
...@@ -142,7 +143,7 @@ module Gitlab ...@@ -142,7 +143,7 @@ module Gitlab
# relation to each other. The last 10 commits for a branch for example, # relation to each other. The last 10 commits for a branch for example,
# should go through .where # should go through .where
def batch_by_oid(repo, oids) def batch_by_oid(repo, oids)
repo.wrapped_gitaly_errors do wrapped_gitaly_errors do
repo.gitaly_commit_client.list_commits_by_oid(oids) repo.gitaly_commit_client.list_commits_by_oid(oids)
end end
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module Gitlab module Gitlab
module Git module Git
class CommitStats class CommitStats
include Gitlab::Git::WrapsGitalyErrors
attr_reader :id, :additions, :deletions, :total attr_reader :id, :additions, :deletions, :total
# Instantiate a CommitStats object # Instantiate a CommitStats object
...@@ -14,7 +16,7 @@ module Gitlab ...@@ -14,7 +16,7 @@ module Gitlab
@deletions = 0 @deletions = 0
@total = 0 @total = 0
repo.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_stats(repo, commit) gitaly_stats(repo, commit)
end end
end end
......
...@@ -2,6 +2,8 @@ module Gitlab ...@@ -2,6 +2,8 @@ module Gitlab
module Git module Git
module Conflict module Conflict
class Resolver class Resolver
include Gitlab::Git::WrapsGitalyErrors
ConflictSideMissing = Class.new(StandardError) ConflictSideMissing = Class.new(StandardError)
ResolutionError = Class.new(StandardError) ResolutionError = Class.new(StandardError)
...@@ -12,7 +14,7 @@ module Gitlab ...@@ -12,7 +14,7 @@ module Gitlab
end end
def conflicts def conflicts
@conflicts ||= @target_repository.wrapped_gitaly_errors do @conflicts ||= wrapped_gitaly_errors do
gitaly_conflicts_client(@target_repository).list_conflict_files.to_a gitaly_conflicts_client(@target_repository).list_conflict_files.to_a
end end
rescue GRPC::FailedPrecondition => e rescue GRPC::FailedPrecondition => e
...@@ -22,7 +24,7 @@ module Gitlab ...@@ -22,7 +24,7 @@ module Gitlab
end end
def resolve_conflicts(source_repository, resolution, source_branch:, target_branch:) def resolve_conflicts(source_repository, resolution, source_branch:, target_branch:)
source_repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_conflicts_client(source_repository).resolve_conflicts(@target_repository, resolution, source_branch, target_branch) gitaly_conflicts_client(source_repository).resolve_conflicts(@target_repository, resolution, source_branch, target_branch)
end end
end end
......
module Gitlab module Gitlab
module Git module Git
class RemoteMirror class RemoteMirror
include Gitlab::Git::WrapsGitalyErrors
def initialize(repository, ref_name) def initialize(repository, ref_name)
@repository = repository @repository = repository
@ref_name = ref_name @ref_name = ref_name
end end
def update(only_branches_matching: []) def update(only_branches_matching: [])
@repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
@repository.gitaly_remote_client.update_remote_mirror(@ref_name, only_branches_matching) @repository.gitaly_remote_client.update_remote_mirror(@ref_name, only_branches_matching)
end end
end end
......
...@@ -6,6 +6,7 @@ module Gitlab ...@@ -6,6 +6,7 @@ module Gitlab
module Git module Git
class Repository class Repository
include Gitlab::Git::RepositoryMirroring include Gitlab::Git::RepositoryMirroring
include Gitlab::Git::WrapsGitalyErrors
include Gitlab::EncodingHelper include Gitlab::EncodingHelper
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
...@@ -845,23 +846,9 @@ module Gitlab ...@@ -845,23 +846,9 @@ module Gitlab
end end
def gitaly_migrate(method, status: Gitlab::GitalyClient::MigrationStatus::OPT_IN, &block) def gitaly_migrate(method, status: Gitlab::GitalyClient::MigrationStatus::OPT_IN, &block)
Gitlab::GitalyClient.migrate(method, status: status, &block) wrapped_gitaly_errors do
rescue GRPC::NotFound => e Gitlab::GitalyClient.migrate(method, status: status, &block)
raise NoRepository.new(e) end
rescue GRPC::InvalidArgument => e
raise ArgumentError.new(e)
rescue GRPC::BadStatus => e
raise CommandError.new(e)
end
def wrapped_gitaly_errors(&block)
yield block
rescue GRPC::NotFound => e
raise NoRepository.new(e)
rescue GRPC::InvalidArgument => e
raise ArgumentError.new(e)
rescue GRPC::BadStatus => e
raise CommandError.new(e)
end end
def clean_stale_repository_files def clean_stale_repository_files
......
...@@ -2,6 +2,7 @@ module Gitlab ...@@ -2,6 +2,7 @@ module Gitlab
module Git module Git
class Tree class Tree
include Gitlab::EncodingHelper include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
attr_accessor :id, :root_id, :name, :path, :flat_path, :type, attr_accessor :id, :root_id, :name, :path, :flat_path, :type,
:mode, :commit_id, :submodule_url :mode, :commit_id, :submodule_url
...@@ -15,7 +16,7 @@ module Gitlab ...@@ -15,7 +16,7 @@ module Gitlab
def where(repository, sha, path = nil, recursive = false) def where(repository, sha, path = nil, recursive = false)
path = nil if path == '' || path == '/' path = nil if path == '' || path == '/'
repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
repository.gitaly_commit_client.tree_entries(repository, sha, path, recursive) repository.gitaly_commit_client.tree_entries(repository, sha, path, recursive)
end end
end end
......
module Gitlab module Gitlab
module Git module Git
class Wiki class Wiki
include Gitlab::Git::WrapsGitalyErrors
DuplicatePageError = Class.new(StandardError) DuplicatePageError = Class.new(StandardError)
OperationError = Class.new(StandardError) OperationError = Class.new(StandardError)
...@@ -65,37 +67,37 @@ module Gitlab ...@@ -65,37 +67,37 @@ module Gitlab
end end
def write_page(name, format, content, commit_details) def write_page(name, format, content, commit_details)
@repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_write_page(name, format, content, commit_details) gitaly_write_page(name, format, content, commit_details)
end end
end end
def delete_page(page_path, commit_details) def delete_page(page_path, commit_details)
@repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_delete_page(page_path, commit_details) gitaly_delete_page(page_path, commit_details)
end end
end end
def update_page(page_path, title, format, content, commit_details) def update_page(page_path, title, format, content, commit_details)
@repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_update_page(page_path, title, format, content, commit_details) gitaly_update_page(page_path, title, format, content, commit_details)
end end
end end
def pages(limit: 0) def pages(limit: 0)
@repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_get_all_pages(limit: limit) gitaly_get_all_pages(limit: limit)
end end
end end
def page(title:, version: nil, dir: nil) def page(title:, version: nil, dir: nil)
@repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_find_page(title: title, version: version, dir: dir) gitaly_find_page(title: title, version: version, dir: dir)
end end
end end
def file(name, version) def file(name, version)
@repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_find_file(name, version) gitaly_find_file(name, version)
end end
end end
...@@ -105,7 +107,7 @@ module Gitlab ...@@ -105,7 +107,7 @@ module Gitlab
# :per_page - The number of items per page. # :per_page - The number of items per page.
# :limit - Total number of items to return. # :limit - Total number of items to return.
def page_versions(page_path, options = {}) def page_versions(page_path, options = {})
versions = @repository.wrapped_gitaly_errors do versions = wrapped_gitaly_errors do
gitaly_wiki_client.page_versions(page_path, options) gitaly_wiki_client.page_versions(page_path, options)
end end
...@@ -127,7 +129,7 @@ module Gitlab ...@@ -127,7 +129,7 @@ module Gitlab
def page_formatted_data(title:, dir: nil, version: nil) def page_formatted_data(title:, dir: nil, version: nil)
version = version&.id version = version&.id
@repository.wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_wiki_client.get_formatted_data(title: title, dir: dir, version: version) gitaly_wiki_client.get_formatted_data(title: title, dir: dir, version: version)
end end
end end
......
module Gitlab
module Git
module WrapsGitalyErrors
def wrapped_gitaly_errors(&block)
yield block
rescue GRPC::NotFound => e
raise Gitlab::Git::Repository::NoRepository.new(e)
rescue GRPC::InvalidArgument => e
raise ArgumentError.new(e)
rescue GRPC::BadStatus => e
raise Gitlab::Git::CommandError.new(e)
end
end
end
end
require 'spec_helper'
describe Gitlab::Git::WrapsGitalyErrors do
subject(:wrapper) do
klazz = Class.new { include Gitlab::Git::WrapsGitalyErrors }
klazz.new
end
describe "#wrapped_gitaly_errors" do
mapping = {
GRPC::NotFound => Gitlab::Git::Repository::NoRepository,
GRPC::InvalidArgument => ArgumentError,
GRPC::BadStatus => Gitlab::Git::CommandError
}
mapping.each do |grpc_error, error|
it "wraps #{grpc_error} in a #{error}" do
expect { wrapper.wrapped_gitaly_errors { raise grpc_error.new('wrapped') } }
.to raise_error(error)
end
end
it 'does not swallow other errors' do
expect { wrapper.wrapped_gitaly_errors { raise 'raised' } }
.to raise_error(RuntimeError)
end
end
end
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