Sha256: 46ce6a628fb6f05ae9d8a55d64f13218ca17a1e38d46b06e9898e818268ed04e

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'socket'

class Flail
  class Exception
    def initialize(env, exception)
      @exception = exception
      @env = env
    end

    #
    # Helpers
    #
    def request
      @request ||= ActionDispatch::Request.new(@env)
    end

    def controller
      @controller ||= @env['action_controller.instance']
    end

    def user
      if controller.respond_to?(:current_user)
        current_user = controller.current_user

        {:id => current_user.id, :name => current_user.to_s}
      else
        {}
      end
    end


    #
    # Handling the exception
    #
    def handle!
      Flail.swing(self.extract.to_json) unless self.ignore?
    end

    def extract
      @extract ||= {}.tap do |info|
        info[:class_name]  = @exception.class.to_s        # @exception class
        info[:message]     = @exception.to_s              # error message
        info[:trace]       = @exception.backtrace.to_json # backtrace of error
        info[:target_url]  = request.url                  # url of request
        info[:referer_url] = request.referer              # referer
        info[:params]      = request.params.to_json       # request parameters
        info[:user_agent]  = request.user_agent           # user agent
        info[:user]        = self.user.to_json            # current user

        # special variables
        info[:environment] = Flail.configuration.env
        info[:hostname]    = Flail.configuration.hostname
        info[:api_key]    = Flail.configuration.api_key
      end
    end

    def ignore?
      # Ignore requests with user agent string matching
      # this regxp as they are surely made by bots
      if @request.user_agent =~ /\b(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg|Yandex|Jyxobot|Huaweisymantecspider|ApptusBot)\b/i
        return true
      end

      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flail-0.0.1 lib/flail/exception.rb