lib/percy/cli/snapshot.rb in percy-cli-0.2.6 vs lib/percy/cli/snapshot.rb in percy-cli-0.3.0

- old
+ new

@@ -37,41 +37,66 @@ if root_resources.empty? say "No root resource files found. Are there HTML files in the given directory?" exit(-1) end - say 'Creating build...' - build = Percy.create_build(repo, resources: related_resources) + build = rescue_connection_failures do + say 'Creating build...' + build = Percy.create_build(repo, resources: related_resources) - say 'Uploading build resources...' - upload_missing_resources(build, build, all_resources, {num_threads: num_threads}) + say 'Uploading build resources...' + upload_missing_resources(build, build, all_resources, {num_threads: num_threads}) + build + end + return if failed? + # Upload a snapshot for every root resource, and associate the related_resources. output_lock = Mutex.new snapshot_thread_pool = Thread.pool(num_threads) total = snapshot_limit ? [root_resources.length, snapshot_limit].min : root_resources.length root_resources.each_with_index do |root_resource, i| break if snapshot_limit && i + 1 > snapshot_limit snapshot_thread_pool.process do output_lock.synchronize do say "Uploading snapshot (#{i+1}/#{total}): #{root_resource.resource_url}" end - snapshot = Percy.create_snapshot(build['data']['id'], [root_resource]) - upload_missing_resources(build, snapshot, all_resources, {num_threads: num_threads}) - Percy.finalize_snapshot(snapshot['data']['id']) + rescue_connection_failures do + snapshot = Percy.create_snapshot(build['data']['id'], [root_resource]) + upload_missing_resources(build, snapshot, all_resources, {num_threads: num_threads}) + Percy.finalize_snapshot(snapshot['data']['id']) + end end end snapshot_thread_pool.wait snapshot_thread_pool.shutdown # Finalize the build. say 'Finalizing build...' - Percy.finalize_build(build['data']['id']) + rescue_connection_failures { Percy.finalize_build(build['data']['id']) } + return if failed? say "Done! Percy is now processing, you can view the visual diffs here:" say build['data']['attributes']['web-url'] end private + + def failed? + !!@failed + end + + def rescue_connection_failures(&block) + raise ArgumentError.new('block is requried') if !block_given? + begin + block.call + rescue Percy::Client::HttpError, + Percy::Client::ConnectionFailed, + Percy::Client::TimeoutError => e + Percy.logger.error(e) + @failed = true + nil + end + end def find_root_paths(dir_path, options = {}) snapshots_regex = options[:snapshots_regex] || DEFAULT_SNAPSHOTS_REGEX file_paths = []