Git Product home page Git Product logo

hoptoad_notifier's Introduction

HoptoadNotifier
===============

This is the notifier plugin for integrating apps with Hoptoad.

When an uncaught exception occurs, HoptoadNotifier will POST the relevant data 
to the Hoptoad server specified in your environment.

INSTALLATION
------------

REMOVE EXCEPTION_NOTIFIER

In your ApplicationController, REMOVE this line:

  include ExceptionNotifiable

In your config/environment* files, remove all references to ExceptionNotifier

Remove the vendor/plugins/exception_notifier directory.

INSTALL HOPTOAD_NOTIFIER

From your project's RAILS_ROOT, run:

  script/plugin install git://github.com/thoughtbot/hoptoad_notifier.git

CONFIGURATION

You should have something like this in config/initializers/hoptoad.rb.

  HoptoadNotifier.configure do |config|
    config.api_key = '1234567890abcdef'
  end

(Please note that this configuration should be in a global configuration, and
is *not* environment-specific. Hoptoad is smart enough to know what errors are
caused by what environments, so your staging errors don't get mixed in with
your production errors.)

That should be it! Now all exceptions will be logged to Hoptoad where they can
be aggregated, filtered, sorted, analyzed, massaged, and searched. In previous
releases you had to include HoptoadNotifier::Catcher into your
ApplicationController, but the plugin takes care of that now.

** NOTE FOR RAILS 1.2.* USERS: **
You will need to copy the hoptoad_notifier_tasks.rake file into your
RAILS_ROOT/lib/tasks directory in order for the following to work:

You can test that hoptoad is working in your production environment by using 
this rake task (from RAILS_ROOT):

  rake hoptoad:test

If everything is configured properly, that task will send a notice to hoptoad 
which will be visible immediately.

USAGE

For the most part, hoptoad works for itself.  Once you've included the notifier 
in your ApplicationController (which is now done automatically by the plugin), 
all errors will be rescued by the #rescue_action_in_public provided by the plugin.

If you want to log arbitrary things which you've rescued yourself from a 
controller, you can do something like this:

  ...
  rescue => ex
    notify_hoptoad(ex)
    flash[:failure] = 'Encryptions could not be rerouted, try again.'
  end
  ...

The #notify_hoptoad call will send the notice over to hoptoad for later 
analysis.

GOING BEYOND EXCEPTIONS

You can also pass a hash to notify_hoptoad method and store whatever you want, not just an exception. And you can also use it anywhere, not just in controllers:

  begin 
    params = { 
      # params that you pass to a method that can throw an exception 
    }
    my_unpredicable_method(params) 
  rescue => e
    HoptoadNotifier.notify(
      :error_class => "Special Error", 
      :error_message => "Special Error: #{e.message}", 
      :request => { :params => params }
    )
  end

While in your controllers you use the notify_hoptoad method, anywhere else in your code, use HoptoadNotifier.notify. Hoptoad will get all the information about the error itself. As for a hash, these are the keys you should pass:

  * :error_class – Use this to group similar errors together. When Hoptoad catches an exception it sends the class name of that exception object.
  * :error_message – This is the title of the error you see in the errors list. For exceptions it is "#{exception.class.name}: #{exception.message}"
  * :request – While there are several ways to send additional data to Hoptoad, passing a Hash with :params key as :request as in the example above is the most common use case. When Hoptoad catches an exception in a controller, the actual HTTP client request is being sent using this key.

Hoptoad merges the hash you pass with these default options:

  def default_notice_options
    { 
      :api_key => HoptoadNotifier.api_key, 
      :error_message => 'Notification', 
      :backtrace => caller, 
      :request => {}, 
      :session => {}, 
      :environment => ENV.to_hash 
    } 
  end

You can override any of those parameters.

FILTERING

You can specify a whitelist of errors, that Hoptoad will not report on.  Use 
this feature when you are so apathetic to certain errors that you don't want 
them even logged.

This filter will only be applied to automatic notifications, not manual 
notifications (when #notify is called directly).

Hoptoad ignores the following exceptions by default:
  ActiveRecord::RecordNotFound
  ActionController::RoutingError
  ActionController::InvalidAuthenticityToken
  CGI::Session::CookieStore::TamperedWithCookie
  
To ignore errors in addition to those, specify their names in your Hoptoad 
configuration block.

  HoptoadNotifier.configure do |config|
    config.api_key      = '1234567890abcdef'
    config.ignore       << ActiveRecord::IgnoreThisError
  end

To ignore *only* certain errors (and override the defaults), use the 
#ignore_only attribute.

  HoptoadNotifier.configure do |config|
    config.api_key      = '1234567890abcdef'
    config.ignore_only  = [ActiveRecord::IgnoreThisError]
  end

To ignore certain user agents, add in the #ignore_user_agent attribute as a
string or regexp:

  HoptoadNotifier.configure do |config|
    config.api_key      = '1234567890abcdef'
    config.ignore_user_agent  << /Ignored/
    config.ignore_user_agent << 'IgnoredUserAgent'
  end

TESTING

When you run your tests, you might notice that the hoptoad service is recording
notices generated using #notify when you don't expect it to.  You can 
use code like this in your test_helper.rb to redefine that method so those 
errors are not reported while running tests.

  module HoptoadNotifier::Catcher
    def notify(thing)
      # do nothing.
    end
  end

THANKS

Thanks to Eugene Bolshakov for the excellent write-up on GOING BEYOND EXCEPTIONS, which we have included above.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.