Skip to content

Commit

Permalink
Make the text presented to the user customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Jan 24, 2011
1 parent 68f51a4 commit d65531c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
21 changes: 21 additions & 0 deletions features/user_informer.feature
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,4 +18,25 @@ Feature: Inform the user of the hoptoad notice that was just created
""" """
And I route "/test/index" to "test#index" And I route "/test/index" to "test#index"
And I perform a request to "http://example.com:123/test/index?param=value" And I perform a request to "http://example.com:123/test/index?param=value"
Then I should see "Hoptoad Error 3799307"

Scenario: Rescue an exception in a controller with a custom error string
When I generate a new Rails application
And I configure the Hoptoad shim
And I configure my application to require the "hoptoad_notifier" gem
And I configure the notifier to use the following configuration lines:
"""
config.user_information = "Error #{{ error_id }}"
"""
And I run the hoptoad generator with "-k myapikey"
And I define a response for "TestController#index":
"""
raise RuntimeError, "some message"
"""
And the response page for a "500" error is
"""
<!-- HOPTOAD ERROR -->
"""
And I route "/test/index" to "test#index"
And I perform a request to "http://example.com:123/test/index?param=value"
Then I should see "Error #3799307" Then I should see "Error #3799307"
7 changes: 6 additions & 1 deletion lib/hoptoad_notifier/configuration.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Configuration
:http_open_timeout, :http_read_timeout, :ignore, :ignore_by_filters, :http_open_timeout, :http_read_timeout, :ignore, :ignore_by_filters,
:ignore_user_agent, :notifier_name, :notifier_url, :notifier_version, :ignore_user_agent, :notifier_name, :notifier_url, :notifier_version,
:params_filters, :project_root, :port, :protocol, :proxy_host, :params_filters, :project_root, :port, :protocol, :proxy_host,
:proxy_pass, :proxy_port, :proxy_user, :secure, :framework, :js_notifier].freeze :proxy_pass, :proxy_port, :proxy_user, :secure, :framework,
:js_notifier, :user_information].freeze


# The API key for your project, found on the project edit form. # The API key for your project, found on the project edit form.
attr_accessor :api_key attr_accessor :api_key
Expand Down Expand Up @@ -83,6 +84,9 @@ class Configuration
# The logger used by HoptoadNotifier # The logger used by HoptoadNotifier
attr_accessor :logger attr_accessor :logger


# The text that the placeholder is replaced with. {{error_id}} is the actual error number.
attr_accessor :user_information

# The framework HoptoadNotifier is configured to use # The framework HoptoadNotifier is configured to use
attr_accessor :framework attr_accessor :framework


Expand Down Expand Up @@ -133,6 +137,7 @@ def initialize
@notifier_version = VERSION @notifier_version = VERSION
@notifier_url = 'http://hoptoadapp.com' @notifier_url = 'http://hoptoadapp.com'
@framework = 'Standalone' @framework = 'Standalone'
@user_information = 'Hoptoad Error {{error_id}}'
end end


# Takes a block and adds it to the list of backtrace filters. When the filters # Takes a block and adds it to the list of backtrace filters. When the filters
Expand Down
6 changes: 5 additions & 1 deletion lib/hoptoad_notifier/user_informer.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def initialize(app)
@app = app @app = app
end end


def replacement(with)
@replacement ||= HoptoadNotifier.configuration.user_information.gsub(/\{\{\s*error_id\s*\}\}/, with.to_s)
end

def call(env) def call(env)
response = @app.call(env) response = @app.call(env)
if env['hoptoad.error_id'] if env['hoptoad.error_id']
Expand All @@ -12,7 +16,7 @@ def call(env)
modified_content_length = 0 modified_content_length = 0
response[2].each do |chunk| response[2].each do |chunk|
original_content_length += chunk.length original_content_length += chunk.length
new_response << chunk.to_s.gsub("<!-- HOPTOAD ERROR -->", "Error ##{env["hoptoad.error_id"].to_s}") new_response << chunk.to_s.gsub("<!-- HOPTOAD ERROR -->", replacement(env['hoptoad.error_id']))
modified_content_length += new_response.last.length modified_content_length += new_response.last.length
end end
response[1]['Content-Length'] = modified_content_length.to_s response[1]['Content-Length'] = modified_content_length.to_s
Expand Down
4 changes: 2 additions & 2 deletions test/user_informer_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class UserInformerTest < Test::Unit::TestCase
ShamRack.mount(informer_app, "example.com") ShamRack.mount(informer_app, "example.com")


response = Net::HTTP.get_response(URI.parse("http://example.com/")) response = Net::HTTP.get_response(URI.parse("http://example.com/"))
assert_equal "Error #1", response.body assert_equal "Hoptoad Error 1", response.body
assert_equal 8, response["Content-Length"].to_i assert_equal 15, response["Content-Length"].to_i
end end


should "not modify output if there is no hoptoad id" do should "not modify output if there is no hoptoad id" do
Expand Down

0 comments on commit d65531c

Please sign in to comment.