I am not seeing hoptoad messages. Now I know why.

Neeraj Singh

By Neeraj Singh

on April 23, 2010

Following code has been tested with Rails 2.3.5 .

Every one knows for sure that hoptoad notifier sends exception messages to server in production environment. Between 'development' and 'production' there could be a number of environments. Some of these would have settings closer to 'development' environment and some would have setting closely matching the settings of 'production' environment.

When you have many environments and when an exception occurs, one is not really sure if that message is getting logged at hoptoad or not. Here is a run down of which messages will get logged and why.

It alls starts with rails

When an exception occurs while rendering a page then action_controller catches the exception. Following logic is evaluated to decide if user should see an error page with full stack trace or 'we are sorry something went wrong' message.

1if consider_all_requests_local || local_request?
2  rescue_action_locally(exception)
4  rescue_action_in_public(exception)

Let's look at first part consider_all_requests_local . Open ~/config/environments/development.rb and ~/config/environments/production.rb .

1# ~/config/environments/development.rb
2config.action_controller.consider_all_requests_local = true
4# ~/config/environments/production.rb
5config.action_controller.consider_all_requests_local = false

As you can see in development mode all requests are local. Be careful with what you put in your intermediary environments.

If you want to override that value then you can do like this.

2ActionController::Base.consider_all_requests_local = true

The second part of the equation was local_request? .

Rails has following code for that method.

1LOCALHOST = ''.freeze
3def local_request?
4  request.remote_addr == LOCALHOST && request.remote_ip == LOCALHOST

As you can see all requests coming from are considered local even if RAILS_ENV is 'production'. For testing purpose you can override this value like this.

3def local_request?
4 false

Hoptoad has access to exception now what

If consider_all_request_local is false and if request is not local then hoptoad will get access to exception thanks to alias_method_chain.

1def self.included(base)
2  base.send(:alias_method, :rescue_action_in_public_without_hoptoad, :rescue_action_in_public)
3  base.send(:alias_method, :rescue_action_in_public, :rescue_action_in_public_with_hoptoad)

In rescue_action_in_public_with_hoptoad there is a call to notify_or_ignore like this.

1unless hoptoad_ignore_user_agent?
2  HoptoadNotifier.notify_or_ignore(exception, hoptoad_request_data)

For majority of us there is no special handling for a particular user_agent .

1def notify_or_ignore(exception, opts = {})
2  notice = build_notice_for(exception, opts)
3  send_notice(notice) unless notice.ignore?

Hoptoad defines following methods as ignorable by default and you won't get notifications for following types of exceptions.

1IGNORE_DEFAULT = ['ActiveRecord::RecordNotFound',
2                   'ActionController::RoutingError',
3                   'ActionController::InvalidAuthenticityToken',
4                   'CGI::Session::CookieStore::TamperedWithCookie',
5                   'ActionController::UnknownAction']

Next hop is method send_notice .

1def send_notice(notice)
2  if configuration.public?
3    sender.send_to_hoptoad(notice.to_xml)
4  end

configuration.public? is defined like this.

1@development_environments = %w(development test cucumber)
2def public?
3  !development_environments.include?(environment_name)

As you can see if the Rails.env is development or test or cucumber the exception will not be reported to hoptoad server.

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.