Skip to content

Instantly share code, notes, and snippets.

@elvuel
Forked from bdotdub/error_handler.rb
Created November 4, 2011 09:32
Show Gist options
  • Save elvuel/1338997 to your computer and use it in GitHub Desktop.
Save elvuel/1338997 to your computer and use it in GitHub Desktop.
Hoptoad notifier for Sinatra
error do
exception = request.env['sinatra.error']
Hoptoad::Notifier.send_error exception, params, request.env if %w(staging production).include?(ENV['RACK_ENV'])
erb :five_hundred
end
module Hoptoad
class Notifier
def self.send_error(exception, params, env)
data = {
'api_key' => ENV['HOPTOAD_API_KEY'],
'error_class' => exception.class.name,
'error_message' => "#{exception.class.name}: #{exception.message}",
'backtrace' => exception.backtrace,
'environment' => env,
'request' => {
'params' => params,
'path' => env['REQUEST_PATH']
},
'session' => {
'key' => env['rack.session'],
'data' => env['rack.session']
}
}
data = { 'notice' => clean_non_serializable_data(data) }
url = URI.parse( 'http://hoptoadapp.com:80/notices/' )
Net::HTTP.start( url.host, url.port ) do |http|
headers = {
'Content-type' => 'application/x-yaml',
'Accept' => 'text/xml, application/xml'
}
http.read_timeout = 5 # seconds
http.open_timeout = 2 # seconds
# http.use_ssl = HoptoadNotifier.secure
response = begin
http.post( url.path, data.to_yaml, headers )
rescue TimeoutError => e
puts "Timeout while contacting the Hoptoad server."
nil
end
case response
when Net::HTTPSuccess
puts "Hoptoad notification sent."
else
puts "Hoptoad notification failure: #{response.class}"
if response.respond_to? :body
puts response.body
end
end
end
end
def self.serializable?(value) #:nodoc:
value.is_a?(Fixnum) ||
value.is_a?(Array) ||
value.is_a?(String) ||
value.is_a?(Hash) ||
value.is_a?(Bignum)
end
def self.clean_non_serializable_data(data) #:nodoc:
data.select{|k,v| serializable?(v) }.inject({}) do |h, pair|
h[pair.first] = pair.last.is_a?(Hash) ? clean_non_serializable_data(pair.last) : pair.last
h
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment