Class: ErrorMessageTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/error-message-tracker.rb

Class Method Summary collapse

Class Method Details

.setup(appName, token) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/error-message-tracker.rb', line 2

def self.setup(appName, token)
  ErrorMessageTracker.const_set("APP_NAME", appName)
  ErrorMessageTracker.const_set("TOKEN", token)
  klass = Class.new do

  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      @app.call(env)
    rescue Exception => ex
      bc = ActiveSupport::BacktraceCleaner.new
      bc.add_filter   { |line| line.gsub(Rails.root.to_s, '') } # strip the Rails.root prefix
      bc.add_silencer { |line| line =~ /rbenv|error-message-tracker/ } # skip any lines from mongrel or rubygems
      # perform the cleanup


      files = {}
      bc.clean(ex.backtrace).each do | line |
        file_name = Rails.root.join(line.split(":")[0].gsub(/\A\//, ''))
        file = File.open(file_name.to_s)
        files[line.split(":")[0]] = file.read
        file.close
      end

      HTTParty.post("https://error-message-tracker.herokuapp.com/mistakes", {body: {
        mistake: {
          token: TOKEN,
          app_name: APP_NAME,
          title: ex.message,
          stack_trace: bc.clean(ex.backtrace).to_json,
          file_data: files.to_json
        }
        }}
      )
      raise ex
      end
    end

  end
  ErrorMessageTracker.const_set("Application", klass)

end