search: use a queue to collect errors

The threading in the tap search code makes handling errors difficult. If
an API-related error is raised in one thread, it is likely to be raised
in each of the rest as well. This results in duplicated error messages,
which is ugly and bad UX.

This patch adds a synchronized queue to collect these exceptions. The
first one added to the queue is re-raised after all operations are
complete.

It's not ideal, but it's minimally invasive and I don't have the energy
or time to do a rewrite.
This commit is contained in:
Jack Nagel 2014-02-16 22:24:33 -05:00
parent ea7415237c
commit 7bdaa7ffe1

View File

@ -1,9 +1,12 @@
require 'formula'
require 'blacklist'
require 'utils'
require 'thread'
module Homebrew extend self
SEARCH_ERROR_QUEUE = Queue.new
# A regular expession to capture the username (one or more char but no `/`,
# which has to be escaped like `\/`), repository, followed by an optional `/`
# and an optional query.
@ -64,10 +67,12 @@ module Homebrew extend self
begin
GitHub.print_pull_requests_matching(query)
rescue GitHub::Error => e
opoo e.message
SEARCH_ERROR_QUEUE << e
end
end
end
raise SEARCH_ERROR_QUEUE.pop unless SEARCH_ERROR_QUEUE.empty?
end
SEARCHABLE_TAPS = [
@ -115,12 +120,14 @@ module Homebrew extend self
end
end
end
results
rescue GitHub::RateLimitExceededError => e
[]
rescue GitHub::Error => e
rescue GitHub::HTTPNotFoundError => e
opoo "Failed to search tap: #{user}/#{repo}. Please run `brew update`"
[]
rescue GitHub::Error => e
SEARCH_ERROR_QUEUE << e
[]
else
results
end
def search_formulae rx