Skip to content

Commit

Permalink
Merge pull request #7 from flowdock/better-capistrano-error-handling
Browse files Browse the repository at this point in the history
Better capistrano error handling
  • Loading branch information
Mikael Roos committed Feb 10, 2012
2 parents c33c302 + 8236c05 commit df62fd6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions lib/flowdock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ApiError < StandardError; end
def initialize(options = {})
@api_token = options[:api_token]
raise InvalidParameterError, "Flow must have :api_token attribute" if blank?(@api_token)

@source = options[:source]
raise InvalidParameterError, "Flow must have valid :source attribute, only alphanumeric characters and underscores can be used" if blank?(@source) || !@source.match(/^[a-z0-9\-_ ]+$/i)

Expand All @@ -25,13 +25,13 @@ def initialize(options = {})

def send_message(params)
raise InvalidParameterError, "Message must have both :subject and :content" if blank?(params[:subject]) || blank?(params[:content])

from = (params[:from].kind_of?(Hash)) ? params[:from] : @from
raise InvalidParameterError, "Flow's :from attribute must have :address attribute" if blank?(from[:address])

tags = (params[:tags].kind_of?(Array)) ? params[:tags] : []
tags.reject! { |tag| !tag.kind_of?(String) || blank?(tag) }

link = (!blank?(params[:link])) ? params[:link] : nil

params = {
Expand All @@ -48,16 +48,16 @@ def send_message(params)

# Send the request
resp = self.class.post(get_flowdock_api_url, :body => params)
raise ApiError, (resp.code == 500 ? "Flowdock API returned error: #{resp.body}" : "HTTP Error #{resp.code}") unless resp.code == 200
raise ApiError, "Flowdock API returned error: Status: #{resp.code} Body: #{resp.body}" unless resp.code == 200
true
end

private

def blank?(var)
var.nil? || var.respond_to?(:length) && var.length == 0
end

def get_flowdock_api_url
"#{FLOWDOCK_API_URL}/#{@api_token}"
end
Expand Down
8 changes: 4 additions & 4 deletions lib/flowdock/capistrano.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
begin
run "echo '#{source.head}' > #{current_path}/BRANCH"
rescue => e
puts "Flowdock: error in saving deployed branch information: " + e
puts "Flowdock: error in saving deployed branch information: #{e.to_s}"
end
end

Expand All @@ -25,15 +25,15 @@
set :repo, Grit::Repo.new(".")
config = Grit::Config.new(repo)
rescue => e
puts "Flowdock: error in fetching your git repository information: " + e
puts "Flowdock: error in fetching your git repository information: #{e.to_s}"
end

begin
set :flowdock_api, Flowdock::Flow.new(:api_token => flowdock_api_token,
:source => "Capistrano deployment", :project => flowdock_project_name,
:from => {:name => config["user.name"], :address => config["user.email"]})
rescue => e
puts "Flowdock: error in configuring Flowdock API: " + e
puts "Flowdock: error in configuring Flowdock API: #{e.to_s}"
end
end

Expand All @@ -45,7 +45,7 @@
:content => notification_message,
:tags => ["deploy", "#{rails_env}"] | flowdock_deploy_tags)
rescue => e
puts "Flowdock: error in sending notification to your flow: " + e
puts "Flowdock: error in sending notification to your flow: #{e.to_s}"
end
end

Expand Down

0 comments on commit df62fd6

Please sign in to comment.