Skip to content

Instantly share code, notes, and snippets.

@endel
Forked from thibaudgg/hoptoad.rb
Created September 8, 2011 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endel/1202597 to your computer and use it in GitHub Desktop.
Save endel/1202597 to your computer and use it in GitHub Desktop.
Custom Goliath Rack middleware for Toadhopper / HoptoadNotifier
require 'toadhopper'
require 'em-synchrony/em-http'
class Toadhopper
def post_document(document, headers={})
uri = URI.parse(@error_url)
begin
response = EM::HttpRequest.new(uri).post({
:body => document,
:head => {'Content-type' => 'text/xml', 'Accept' => 'text/xml, application/xml'}.merge(headers)
})
parse_response(response)
rescue
Response.new(500, '', ['Timeout error'])
end
end
end
module Goliath
module Rack
class Hoptoad
include Goliath::Constants
def initialize(app)
@app = app
end
def call(env)
response = @app.call(env)
if env[RACK_EXCEPTION]
Toadhopper('YOUR_API_KEY').post!(env[RACK_EXCEPTION], {:environment => env})
end
response
rescue Exception => e
Toadhopper('YOUR_API_KEY').post!(env[RACK_EXCEPTION])
end
end
end
end
require 'goliath'
require 'hoptoad' # custom middleware
class Server < Goliath::API
use Goliath::Rack::Hoptoad
def response(env)
raise 'Notify hoptoad'
[200, {}, "ouch"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment