Class: Error

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/error.rb

Class Method Summary collapse

Class Method Details

.find_or_create_for_exception(exception) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/error.rb', line 6

def self.find_or_create_for_exception(exception)
  message = exception.message
  backtrace = exception.backtrace.join("\n")
  sha = Digest::SHA1.hexdigest([message, backtrace].join)
  error = create_with(
    type: exception.class.name,
    message: message,
    backtrace: backtrace
  ).find_or_create_by(sha: sha)

  # TODO: we'll move this to `create_with` and incorporate it into the SHA;
  # but for now we'll try to collect types for errors
  error.update_column :type, exception.class.name unless error.type
  error
rescue ActiveRecord::RecordNotUnique
  find_by(sha: sha)
end