Class: BugHunter::Error

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/bug_hunter/models.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_from(env, exception) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/bug_hunter/models.rb', line 112

def self.build_from(env, exception)
  doc = self.new
  doc[:message] = exception.message
  doc[:backtrace] = exception.backtrace
  doc[:exception_type] = exception.class.to_s

  new_env = {}
  env.each do |k,v|
    next if k =~ /^action_/

    new_env[k.gsub(".", "_")] = v.inspect
  end
  doc[:request_env] = new_env

  scheme = if env['HTTP_VERSION'] =~ /^HTTPS/i
    "https://"
  else
    "http://"
  end

  url = "#{scheme}#{env["HTTP_HOST"]}#{env["REQUEST_PATH"]}"
  params = {}
  if env["QUERY_STRING"] && !env["QUERY_STRING"].empty?
    url << "?#{env["QUERY_STRING"]}"

    env["QUERY_STRING"].split("&").each do |e|
      k,v = e.split("=")
      params[k] = v
    end
  end
  doc[:url] = url
  if defined?(Rails)
    doc[:is_rails] = true
    doc[:action] = params[:action]
    doc[:controller] = params[:controller]
  end

  doc[:params] = params

  exception.backtrace.each do |line|
    if self.highlight_line?(line) && line =~ /^(.+):(\d+):in `(.+)'/
      next if !File.exist?($1)

      doc[:file] = $1
      doc[:line] = $2.to_i
      doc[:method] = $3

      doc[:line_content] = File.open(doc[:file]).readlines[doc[:line]-1]

      break
    end
  end


  doc
end

.highlight_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/bug_hunter/models.rb', line 174

def self.highlight_line?(line)
  line !~ /\/(usr|vendor|bundle)\//
end

.minimalObject



47
48
49
# File 'lib/bug_hunter/models.rb', line 47

def self.minimal
  without(:comments, :request_env, :backtrace)
end

Instance Method Details

#add_comment(from, message, ip) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bug_hunter/models.rb', line 55

def add_comment(from, message, ip)
  comment = {:from => from,
             :message => message,
             :created_at => Time.now.utc,
             :ip => ip}

  self.collection.update({:_id => self.id},
                         {:$push => {:comments => comment},
                          :$inc => {:comments_count => 1}},
                         {:multi => true})
end

#partial_message(regex = true) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bug_hunter/models.rb', line 93

def partial_message(regex = true)
  msg = self[:message]
  if msg.match(/#<.+>/) && $`.length > 10
    if regex
      msg = /^#{Regexp.escape($`)}/
    else
      msg = $`
    end
  elsif msg.match(/\{.+\}/) && $`.length > 10
    if regex
      msg = /^#{Regexp.escape($`)}/
    else
      msg = $`
    end
  end

  msg
end

#resolve!Object



67
68
69
70
71
72
73
# File 'lib/bug_hunter/models.rb', line 67

def resolve!
  self.collection.update({:_id => self.id},
                         {:$set => {:resolved => true, :updated_at => Time.now.utc}},
                         {:multi => true})
  BugHunter::Project.collection.update({:_id => BugHunter::Project.instance.id},
                                       {:$inc => {:errors_resolved_count => 1}})
end

#similar_errorsObject



51
52
53
# File 'lib/bug_hunter/models.rb', line 51

def similar_errors
  self.class.where(:message => partial_message, :_id.ne => self.id)
end

#unique_error_selectorObject



83
84
85
86
87
88
89
90
91
# File 'lib/bug_hunter/models.rb', line 83

def unique_error_selector
  {
    :resolved => false,
    :message => partial_message,
    :file => self.file,
    :line => self.line,
    :method => self.method
  }
end

#unresolve!Object



75
76
77
78
79
80
81
# File 'lib/bug_hunter/models.rb', line 75

def unresolve!
  self.collection.update({:_id => self.id},
                         {:$set => {:resolved => false, :updated_at => Time.now.utc}},
                         {:multi => true})
  BugHunter::Project.collection.update({:_id => BugHunter::Project.instance.id},
                                       {:$inc => {:errors_resolved_count => -1}})
end

#update_projectObject



169
170
171
172
# File 'lib/bug_hunter/models.rb', line 169

def update_project
  BugHunter::Project.collection.update({:_id => BugHunter::Project.instance.id},
                                       {:$inc => {:errors_count => 1}})
end