Skip to content

Commit

Permalink
Added catch_errors to reduce duplication in error handling around the…
Browse files Browse the repository at this point in the history
… overall client operation
  • Loading branch information
ricardochimal committed Oct 13, 2010
1 parent 297cf26 commit 27dcc85
Showing 1 changed file with 26 additions and 37 deletions.
63 changes: 26 additions & 37 deletions lib/taps/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,30 @@ def verify_server
end
end

def catch_errors(&blk)
verify_server

begin
blk.call
close_session
rescue RestClient::Exception, Taps::BaseError => e
store_session
if e.kind_of?(Taps::BaseError)
puts "!!! Caught Server Exception"
puts "#{e.class}: #{e.message}"
puts "\n#{e.original_backtrace}" if e.original_backtrace
exit(1)
elsif e.respond_to?(:response)
puts "!!! Caught Server Exception"
puts "HTTP CODE: #{e.http_code}"
puts "#{e.response.to_s}"
exit(1)
else
raise
end
end
end

def self.factory(type, database_url, remote_url, opts)
type = :resume if opts[:resume]
klass = case type
Expand All @@ -210,9 +234,7 @@ def to_hash
end

def run
verify_server

begin
catch_errors do
unless resuming?
pull_schema
pull_indexes if indexes_first?
Expand All @@ -222,22 +244,6 @@ def run
pull_data
pull_indexes unless indexes_first?
pull_reset_sequences
close_session
rescue (RestClient::Exception, Taps::Base) => e
store_session
if e.kind_of?(Taps::Base)
puts "!!! Caught Server Exception"
puts "#{e.class}: #{e.message}"
puts "\n#{e.original_backtrace}" if e.original_backtrace
exit(1)
elsif e.respond_to?(:response)
puts "!!! Caught Server Exception"
puts "HTTP CODE: #{e.http_code}"
puts "#{e.response.to_s}"
exit(1)
else
raise
end
end
end

Expand Down Expand Up @@ -387,8 +393,7 @@ def to_hash
end

def run
verify_server
begin
catch_errors do
unless resuming?
push_schema
push_indexes if indexes_first?
Expand All @@ -398,22 +403,6 @@ def run
push_data
push_indexes unless indexes_first?
push_reset_sequences
close_session
rescue (RestClient::Exception, Taps::Base) => e
store_session
if e.kind_of?(Taps::Base)
puts "!!! Caught Server Exception"
puts "#{e.class}: #{e.message}"
puts "\n#{e.original_backtrace}" if e.original_backtrace
exit(1)
elsif e.respond_to?(:response)
puts "!!! Caught Server Exception"
puts "HTTP CODE: #{e.http_code}"
puts "#{e.response.to_s}"
exit(1)
else
raise
end
end
end

Expand Down

0 comments on commit 27dcc85

Please sign in to comment.