Commit 6e27fc37 authored by Douwe Maan's avatar Douwe Maan
Browse files

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

Move Repository#wrapped_gitaly_errors into concern

See merge request gitlab-org/gitlab-ce!22691
......@@ -5,6 +5,7 @@ module Git
class Blob
include Gitlab::BlobHelper
include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
# 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
......@@ -75,7 +76,7 @@ def batch_metadata(repository, blob_references)
# Returns array of Gitlab::Git::Blob
# Does not guarantee blob data will be set
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)
end
end
......
......@@ -3,6 +3,7 @@ module Gitlab
module Git
class Commit
include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
attr_accessor :raw_commit, :head
......@@ -59,7 +60,7 @@ def find(repo, commit_id = "HEAD")
# This saves us an RPC round trip.
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)
end
......@@ -100,7 +101,7 @@ def last_for_path(repo, ref, path = nil)
# Commit.between(repo, '29eda46b', 'master')
#
def between(repo, base, head)
repo.wrapped_gitaly_errors do
wrapped_gitaly_errors do
repo.gitaly_commit_client.between(base, head)
end
end
......@@ -125,7 +126,7 @@ def between(repo, base, head)
# are documented here:
# http://www.rubydoc.info/github/libgit2/rugged/Rugged#SORT_NONE-constant)
def find_all(repo, options = {})
repo.wrapped_gitaly_errors do
wrapped_gitaly_errors do
Gitlab::GitalyClient::CommitService.new(repo).find_all_commits(options)
end
end
......@@ -142,7 +143,7 @@ def shas_with_signatures(repository, shas)
# relation to each other. The last 10 commits for a branch for example,
# should go through .where
def batch_by_oid(repo, oids)
repo.wrapped_gitaly_errors do
wrapped_gitaly_errors do
repo.gitaly_commit_client.list_commits_by_oid(oids)
end
end
......
......@@ -3,6 +3,8 @@
module Gitlab
module Git
class CommitStats
include Gitlab::Git::WrapsGitalyErrors
attr_reader :id, :additions, :deletions, :total
# Instantiate a CommitStats object
......@@ -14,7 +16,7 @@ def initialize(repo, commit)
@deletions = 0
@total = 0
repo.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_stats(repo, commit)
end
end
......
......@@ -2,6 +2,8 @@ module Gitlab
module Git
module Conflict
class Resolver
include Gitlab::Git::WrapsGitalyErrors
ConflictSideMissing = Class.new(StandardError)
ResolutionError = Class.new(StandardError)
......@@ -12,7 +14,7 @@ def initialize(target_repository, our_commit_oid, their_commit_oid)
end
def conflicts
@conflicts ||= @target_repository.wrapped_gitaly_errors do
@conflicts ||= wrapped_gitaly_errors do
gitaly_conflicts_client(@target_repository).list_conflict_files.to_a
end
rescue GRPC::FailedPrecondition => e
......@@ -22,7 +24,7 @@ def conflicts
end
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)
end
end
......
module Gitlab
module Git
class RemoteMirror
include Gitlab::Git::WrapsGitalyErrors
def initialize(repository, ref_name)
@repository = repository
@ref_name = ref_name
end
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)
end
end
......
......@@ -6,6 +6,7 @@ module Gitlab
module Git
class Repository
include Gitlab::Git::RepositoryMirroring
include Gitlab::Git::WrapsGitalyErrors
include Gitlab::EncodingHelper
include Gitlab::Utils::StrongMemoize
......@@ -845,23 +846,9 @@ def gitaly_conflicts_client(our_commit_oid, their_commit_oid)
end
def gitaly_migrate(method, status: Gitlab::GitalyClient::MigrationStatus::OPT_IN, &block)
Gitlab::GitalyClient.migrate(method, status: status, &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
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)
wrapped_gitaly_errors do
Gitlab::GitalyClient.migrate(method, status: status, &block)
end
end
def clean_stale_repository_files
......
......@@ -2,6 +2,7 @@ module Gitlab
module Git
class Tree
include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
attr_accessor :id, :root_id, :name, :path, :flat_path, :type,
:mode, :commit_id, :submodule_url
......@@ -15,7 +16,7 @@ class << self
def where(repository, sha, path = nil, recursive = false)
path = nil if path == '' || path == '/'
repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
repository.gitaly_commit_client.tree_entries(repository, sha, path, recursive)
end
end
......
module Gitlab
module Git
class Wiki
include Gitlab::Git::WrapsGitalyErrors
DuplicatePageError = Class.new(StandardError)
OperationError = Class.new(StandardError)
......@@ -65,37 +67,37 @@ def repository_exists?
end
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)
end
end
def delete_page(page_path, commit_details)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_delete_page(page_path, commit_details)
end
end
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)
end
end
def pages(limit: 0)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_get_all_pages(limit: limit)
end
end
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)
end
end
def file(name, version)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_find_file(name, version)
end
end
......@@ -105,7 +107,7 @@ def file(name, version)
# :per_page - The number of items per page.
# :limit - Total number of items to return.
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)
end
......@@ -127,7 +129,7 @@ def preview_slug(title, format)
def page_formatted_data(title:, dir: nil, version: nil)
version = version&.id
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_wiki_client.get_formatted_data(title: title, dir: dir, version: version)
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